This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[ios, macos] Use std::vector for MGLMultiPoint coordinate storage #6890
Merged
boundsj
merged 5 commits into
release-ios-v3.4.0
from
boundsj-use-vector-for-multipoint-coords
Nov 14, 2016
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1857548
[ios, macos] Use std::vector for MGLMultiPoint coordinate storage
boundsj 05f50a9
[ios, macos] Make replace true and introduce append coordinates
boundsj 2474cb2
[ios, macos] Fixes for CR
boundsj d40c6dd
Refactor point collection to use std::vector
boundsj ccc14b8
[ios, macos] Update docs for multipoint
boundsj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,8 +14,8 @@ | |
|
||
@implementation MGLMultiPoint | ||
{ | ||
size_t _count; | ||
MGLCoordinateBounds _bounds; | ||
std::vector<CLLocationCoordinate2D> _coordinates; | ||
} | ||
|
||
- (instancetype)initWithCoordinates:(CLLocationCoordinate2D *)coords count:(NSUInteger)count | ||
|
@@ -24,105 +24,83 @@ - (instancetype)initWithCoordinates:(CLLocationCoordinate2D *)coords count:(NSUI | |
|
||
if (self) | ||
{ | ||
[self setupWithCoordinates:coords count:count]; | ||
NSAssert(count > 0, @"A multipoint must have coordinates"); | ||
_coordinates = { coords, coords + count }; | ||
[self computeBounds]; | ||
} | ||
|
||
return self; | ||
} | ||
|
||
- (void)setupWithCoordinates:(CLLocationCoordinate2D *)coords count:(NSUInteger)count | ||
{ | ||
if (_coordinates) free(_coordinates); | ||
|
||
_count = count; | ||
_coordinates = (CLLocationCoordinate2D *)malloc(_count * sizeof(CLLocationCoordinate2D)); | ||
|
||
mbgl::LatLngBounds bounds = mbgl::LatLngBounds::empty(); | ||
|
||
for (NSUInteger i = 0; i < _count; i++) | ||
{ | ||
_coordinates[i] = coords[i]; | ||
bounds.extend(mbgl::LatLng(coords[i].latitude, coords[i].longitude)); | ||
} | ||
|
||
_bounds = MGLCoordinateBoundsFromLatLngBounds(bounds); | ||
} | ||
|
||
- (void)dealloc | ||
{ | ||
free(_coordinates); | ||
} | ||
|
||
- (CLLocationCoordinate2D)coordinate | ||
{ | ||
assert(_count > 0); | ||
|
||
return CLLocationCoordinate2DMake(_coordinates[0].latitude, _coordinates[0].longitude); | ||
NSAssert([self pointCount] > 0, @"A multipoint must have coordinates"); | ||
return _coordinates.at(0); | ||
} | ||
|
||
- (NSUInteger)pointCount | ||
{ | ||
return _count; | ||
return _coordinates.size(); | ||
} | ||
|
||
+ (NS_SET_OF(NSString *) *)keyPathsForValuesAffectingPointCount | ||
{ | ||
return [NSSet setWithObjects:@"coordinates", nil]; | ||
} | ||
|
||
- (CLLocationCoordinate2D *)coordinates | ||
{ | ||
return _coordinates.data(); | ||
} | ||
|
||
- (void)getCoordinates:(CLLocationCoordinate2D *)coords range:(NSRange)range | ||
{ | ||
if (range.location + range.length > _count) | ||
if (range.location + range.length > [self pointCount]) | ||
{ | ||
[NSException raise:NSRangeException | ||
format:@"Invalid coordinate range %@ extends beyond current coordinate count of %zu", | ||
NSStringFromRange(range), _count]; | ||
NSStringFromRange(range), [self pointCount]]; | ||
} | ||
|
||
NSUInteger index = 0; | ||
std::copy(_coordinates.begin() + range.location, _coordinates.begin() + NSMaxRange(range), coords); | ||
} | ||
|
||
for (NSUInteger i = range.location; i < range.location + range.length; i++) | ||
{ | ||
coords[index] = _coordinates[i]; | ||
index++; | ||
} | ||
- (void)appendCoordinates:(CLLocationCoordinate2D *)coords count:(NSUInteger)count | ||
{ | ||
[self willChangeValueForKey:@"coordinates"]; | ||
_coordinates.insert(_coordinates.end(), count, *coords); | ||
[self computeBounds]; | ||
[self didChangeValueForKey:@"coordinates"]; | ||
} | ||
|
||
- (void)replaceCoordinatesInRange:(NSRange)range withCoordinates:(CLLocationCoordinate2D *)coords | ||
{ | ||
if ((coords >= _coordinates && coords < _coordinates + _count) || | ||
(coords + range.length >= _coordinates && coords + range.length < _coordinates + _count)) | ||
{ | ||
[NSException raise:NSRangeException format:@"Reuse of existing coordinates array %p not supported", coords]; | ||
} | ||
else if (range.length == 0) | ||
if (range.length == 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the developer tries to replace no coordinates, this method should be a no-op instead of raising an exception. That may help to minimize the number of special cases the developer would need to implement. |
||
{ | ||
[NSException raise:NSRangeException format:@"Empty coordinate range %@", NSStringFromRange(range)]; | ||
return; | ||
} | ||
else if (range.location > _count) | ||
|
||
if (NSMaxRange(range) > _coordinates.size()) | ||
{ | ||
[NSException raise:NSRangeException | ||
format:@"Invalid range %@ for existing coordinate count %zu", | ||
NSStringFromRange(range), _count]; | ||
NSStringFromRange(range), [self pointCount]]; | ||
} | ||
|
||
[self willChangeValueForKey:@"coordinates"]; | ||
if (NSMaxRange(range) <= _count) | ||
{ | ||
// replacing existing coordinate(s) | ||
memcpy(_coordinates + range.location, coords, range.length * sizeof(CLLocationCoordinate2D)); | ||
} | ||
else | ||
std::copy(coords, coords + range.length, _coordinates.begin() + range.location); | ||
[self computeBounds]; | ||
[self didChangeValueForKey:@"coordinates"]; | ||
} | ||
|
||
- (void)computeBounds | ||
{ | ||
mbgl::LatLngBounds bounds = mbgl::LatLngBounds::empty(); | ||
for (auto coordinate : _coordinates) | ||
{ | ||
// appending new coordinate(s) | ||
NSUInteger newCount = NSMaxRange(range); | ||
CLLocationCoordinate2D *newCoordinates = (CLLocationCoordinate2D *)malloc(newCount * sizeof(CLLocationCoordinate2D)); | ||
memcpy(newCoordinates, _coordinates, fmin(_count, range.location) * sizeof(CLLocationCoordinate2D)); | ||
memcpy(newCoordinates + range.location, coords, range.length * sizeof(CLLocationCoordinate2D)); | ||
[self setupWithCoordinates:newCoordinates count:newCount]; | ||
free(newCoordinates); | ||
bounds.extend(mbgl::LatLng(coordinate.latitude, coordinate.longitude)); | ||
} | ||
[self didChangeValueForKey:@"coordinates"]; | ||
_bounds = MGLCoordinateBoundsFromLatLngBounds(bounds); | ||
} | ||
|
||
- (MGLCoordinateBounds)overlayBounds | ||
|
@@ -144,7 +122,7 @@ - (BOOL)intersectsOverlayBounds:(MGLCoordinateBounds)overlayBounds | |
- (NSString *)description | ||
{ | ||
return [NSString stringWithFormat:@"<%@: %p; count = %lu; bounds = %@>", | ||
NSStringFromClass([self class]), (void *)self, (unsigned long)_count, MGLStringFromCoordinateBounds(_bounds)]; | ||
NSStringFromClass([self class]), (void *)self, (unsigned long)[self pointCount], MGLStringFromCoordinateBounds(_bounds)]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It’s pretty meaningless to have a multipoint of zero points. In fact, we assert as much below. So we should detect that case early, in here, asserting if
count
is zero.