<commit_before>/*
*   GFLineString.mm
*
*   Copyright 2015 The Climate Corporation
*   Copyright 2015 <NAME>
*
*   Licensed under the Apache License, Version 2.0 (the "License");
*   you may not use this file except in compliance with the License.
*   You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
*   Unless required by applicable law or agreed to in writing, software
*   distributed under the License is distributed on an "AS IS" BASIS,
*   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*   See the License for the specific language governing permissions and
*   limitations under the License.
*
*   Created by <NAME> on 6/4/15.
*
*   MODIFIED 2015 BY <NAME>. Modifications licensed under Apache License, Version 2.0.
*
*/

#include "GFLineString+Protected.hpp"
#include "GFLineString+Primitives.hpp"
#include "GFPoint+Protected.hpp"

#include "internal/geofeatures/Point.hpp"
#include "internal/geofeatures/LineString.hpp"
#include "internal/geofeatures/GeometryVariant.hpp"

#include <boost/geometry/strategies/strategies.hpp>
#include <boost/geometry/algorithms/correct.hpp>
#include <boost/geometry/io/wkt/wkt.hpp>

namespace gf = geofeatures;

@implementation GFLineString {
    @protected
        gf::LineString _lineString;
    }

#pragma mark - Construction

    - (instancetype) initWithWKT:(NSString *)wkt {
        NSParameterAssert(wkt != nil);

        if (self = [super init]) {
            try {
                boost::geometry::read_wkt([wkt cStringUsingEncoding: NSUTF8StringEncoding], _lineString);
                
            } catch (std::exception & e) {
                @throw [NSException exceptionWithName: NSInvalidArgumentException reason:[NSString stringWithUTF8String:e.what()] userInfo:nil];
            }
        }
        return self;
    }

    - (instancetype) initWithGeoJSONGeometry:(NSDictionary *)jsonDictionary {
        NSParameterAssert(jsonDictionary != nil);

        if (self = [super init]) {
            id coordinates = jsonDictionary[@"coordinates"];
            
            if (!coordinates || ![coordinates isKindOfClass:[NSArray class]]) {
                @throw [NSException exceptionWithName: NSInvalidArgumentException reason:@"Invalid GeoJSON Geometry Object, no coordinates found or coordinates of an invalid type." userInfo:nil];
            }
            
            _lineString = gf::GFLineString::lineStringWithGeoJSONCoordinates(coordinates);
        }
        return self;
    }

#pragma mark - NSCopying

    - (id) copyWithZone:(struct _NSZone *)zone {
        return [(GFLineString *) [[GFLineString class] allocWithZone: zone] initWithCPPLineString: _lineString];
    }

#pragma mark - NSMutableCopying

    - (id) mutableCopyWithZone: (NSZone *) zone {
        return [(GFMutableLineString *) [[GFMutableLineString class] allocWithZone: zone] initWithCPPLineString: _lineString];
    }

#pragma mark - Querying a GFLineSting

    - (NSUInteger) count {
        return _lineString.size();
    }

    - (GFPoint *) pointAtIndex: (NSUInteger) index {

        auto size = _lineString.size();
        
        if (size == 0 || index > (size -1)) {
            [NSException raise:NSRangeException format:@"Index %li is beyond bounds [0, %li].", (unsigned long) index, (unsigned long) _lineString.size()];
        }
        //
        // Note: We use operator[] below because we've
        //       already checked the bounds above.
        //
        //       Operator[] is also unchecked, will not throw,
        //       and faster than at().
        //
        return [[GFPoint alloc] initWithCPPPoint: _lineString[index]];
    }

    - (GFPoint *) firstPoint {

        if (_lineString.empty()) {
            return nil;
        }
        return [[GFPoint alloc] initWithCPPPoint: _lineString.front()];
    }

    - (GFPoint *) lastPoint {

        if (_lineString.empty()) {
            return nil;
        }
        return [[GFPoint alloc] initWithCPPPoint: _lineString.back()];
    }

#pragma mark - Indexed Subscripting

    - (GFPoint *) objectAtIndexedSubscript: (NSUInteger) index {

        auto size = _lineString.size();

        if (size == 0 || index > (size -1)) {<commit_msg>Update GFLineString.mm - Simplified -pointAtIndex range check to index >= size(). - Simplified -objectAtIndexSubscript: range check to using index >= size(). - Change -firstPoint from using front() to *<type>.begin() to gain the noexcept guarantee and avoid the extra call to empty(). - Change -lastPoint from using back() to *(<type>.end() -1) to gain the noexcept guarantee and avoid the extra call to empty().<commit_after>/*
*   GFLineString.mm
*
*   Copyright 2015 The Climate Corporation
*   Copyright 2015 <NAME>
*
*   Licensed under the Apache License, Version 2.0 (the "License");
*   you may not use this file except in compliance with the License.
*   You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
*   Unless required by applicable law or agreed to in writing, software
*   distributed under the License is distributed on an "AS IS" BASIS,
*   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*   See the License for the specific language governing permissions and
*   limitations under the License.
*
*   Created by <NAME> on 6/4/15.
*
*   MODIFIED 2015 BY <NAME>. Modifications licensed under Apache License, Version 2.0.
*
*/

#include "GFLineString+Protected.hpp"
#include "GFLineString+Primitives.hpp"
#include "GFPoint+Protected.hpp"

#include "internal/geofeatures/Point.hpp"
#include "internal/geofeatures/LineString.hpp"
#include "internal/geofeatures/GeometryVariant.hpp"

#include <boost/geometry/strategies/strategies.hpp>
#include <boost/geometry/algorithms/correct.hpp>
#include <boost/geometry/io/wkt/wkt.hpp>

namespace gf = geofeatures;

@implementation GFLineString {
    @protected
        gf::LineString _lineString;
    }

#pragma mark - Construction

    - (instancetype) initWithWKT:(NSString *)wkt {
        NSParameterAssert(wkt != nil);

        if (self = [super init]) {
            try {
                boost::geometry::read_wkt([wkt cStringUsingEncoding: NSUTF8StringEncoding], _lineString);
                
            } catch (std::exception & e) {
                @throw [NSException exceptionWithName: NSInvalidArgumentException reason:[NSString stringWithUTF8String:e.what()] userInfo:nil];
            }
        }
        return self;
    }

    - (instancetype) initWithGeoJSONGeometry:(NSDictionary *)jsonDictionary {
        NSParameterAssert(jsonDictionary != nil);

        if (self = [super init]) {
            id coordinates = jsonDictionary[@"coordinates"];
            
            if (!coordinates || ![coordinates isKindOfClass:[NSArray class]]) {
                @throw [NSException exceptionWithName: NSInvalidArgumentException reason:@"Invalid GeoJSON Geometry Object, no coordinates found or coordinates of an invalid type." userInfo:nil];
            }
            
            _lineString = gf::GFLineString::lineStringWithGeoJSONCoordinates(coordinates);
        }
        return self;
    }

#pragma mark - NSCopying

    - (id) copyWithZone:(struct _NSZone *)zone {
        return [(GFLineString *) [[GFLineString class] allocWithZone: zone] initWithCPPLineString: _lineString];
    }

#pragma mark - NSMutableCopying

    - (id) mutableCopyWithZone: (NSZone *) zone {
        return [(GFMutableLineString *) [[GFMutableLineString class] allocWithZone: zone] initWithCPPLineString: _lineString];
    }

#pragma mark - Querying a GFLineSting

    - (NSUInteger) count {
        return _lineString.size();
    }

    - (GFPoint *) pointAtIndex: (NSUInteger) index {

        if (index >= _lineString.size()) {
            [NSException raise:NSRangeException format:@"Index %li is beyond bounds [0, %li].", (unsigned long) index, (unsigned long) _lineString.size()];
        }
        //
        // Note: We use operator[] below because we've
        //       already checked the bounds above.
        //
        //       Operator[] is also unchecked, will not throw,
        //       and faster than at().
        //
        return [[GFPoint alloc] initWithCPPPoint: _lineString[index]];
    }

    - (GFPoint *) firstPoint {

        if (_lineString.empty()) {
            return nil;
        }
        return [[GFPoint alloc] initWithCPPPoint: *_lineString.begin()];
    }

    - (GFPoint *) lastPoint {

        if (_lineString.empty()) {
            return nil;
        }
        return [[GFPoint alloc] initWithCPPPoint: *(_lineString.end() - 1)];
    }

#pragma mark - Indexed Subscripting

    - (GFPoint *) objectAtIndexedSubscript: (NSUInteger) index {

        if (index >= _lineString.size()) {
{'fraction_non_alphanumeric': 0.10895828584454069, 'fraction_numerical': 0.0063824937314793705, 'mean_word_length': 2.7937743190661477, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5294851', 'n_tokens_mistral': 2593, 'n_tokens_neox': 2416, 'n_words': 708}