GithubHelp home page GithubHelp logo

lawrencelomax / llbinaryoperators Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 792 KB

Binary Enumeration Operators on NSArray. Fully unit tested

License: MIT License

Ruby 0.12% Objective-C 92.91% C 6.19% C++ 0.13% Shell 0.64%

llbinaryoperators's Introduction

LLBinaryOperators

Binary Enumeration Operators on NSArray. Fully unit tested.

Rationale

The NSArray methods for binary enumeration only work when dealing with object equality:

- (NSUInteger)indexOfObject:(id)obj inSortedRange:(NSRange)r options:(NSBinarySearchingOptions)opts usingComparator:(NSComparator)cmp

Sometimes, you may wish to get the performance gain of a binary enumeration within a sorted array, without requiring object equality. It isn't always possible or desirable to override isEqual: to allow for object equality. For example in a hashed collection, you cannot express intersection via a hashCode

Usage

You may have a object that contains some layout information for a large number of items ordered by position. To obtain the layout information for a given query rectangle, you could use linear enumeration, however it would be much more efficient to use a Binary Search. As you cannot use the standard NSArray methods due to requiring object equality, you can use LLBinaryOperators to obtain the layout information that intersects with the query rectangle.

Use the category method. method:

- (void) ll_binaryEnumerate:(LLBinaryEnumerationBlock)block;

You can do this to obtain the rectangles that intersect with the lowest value in the x dimension:

NSArray * arrayOfCGRects = ....;
CGRect intersectionRect = ....;
__block NSUInteger intersectionIndex = NSNotFound;
[arrayOfCGRects ll_binaryEnumerate:NSComparisonResult^(NSUInteger index, LayoutInformation * object, NSRange range, BOOL * stop) {
  CGRect * rect = [object bounds];
  if(CGRectIntersectsRect(rect, intersectionRect) {
    intersectionIndex = index;
    // We have an index that intersects, but it could be anywhere in a range
    // we should go downwards to find the lowest index that intersects
    // When we have this index we can enumerate linearly up to find the highest index
    return NSOrderedDescending;
  } else if (CGRectGetMinX(rect) > CGRectGetMaxX(intersectionRect)) {
    // Move right (up) the array
    return NSOrderedAscending;
  } else if (CGRectGetMaxX(rect) < CGRectGetMinX(intersectionRect)) {
    // Move left (down) along the array
    return NSOrderedDescending;
  }
  
  *stop = YES;
  return NSOrderedSame;
}];

We have the leftmost index, now linearly enumerate upwards until we hit the rightmost index

NSMutableArray * intersectionObjects = [NSMutableArray array];
if(intersectionIndex != NSNotFound) {
    [arrayOfCGRects enumerateObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(intersectionIndex, [array count] - indersectionIndex)] options:0 usingBlock:^(SomeObject * obj, NSUInteger idx, BOOL *stop) {
        if(CGRectIntersectsRect([obj bounds], intersectionRect)) {
            [intersectionObjects addObject:obj];
            return;
        }
        *stop = YES;
    }];
    
    NSLog(@"Objects %@ intersect rect %@", intersectionObjects, NSStringFromCGRect(intersectionRect));
}

llbinaryoperators's People

Contributors

lawrencelomax avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.