From 2cb6969c79e89d66268712679bc24dce56efe4fe Mon Sep 17 00:00:00 2001 From: Levi McCallum Date: Fri, 8 Apr 2016 09:30:52 -0700 Subject: [PATCH] Use unsigned integers for findNodes vectors Summary: Also refector logic and naming to be simpler and more descriptive Reviewers: schneider, chris, scottg Reviewed By: schneider, chris, scottg Subscribers: chris, jenkins Differential Revision: https://phabricator.pinadmin.com/D83911 --- .../Private/ASDisplayNodeLayoutContext.mm | 55 ++++++++----------- 1 file changed, 22 insertions(+), 33 deletions(-) diff --git a/AsyncDisplayKit/Private/ASDisplayNodeLayoutContext.mm b/AsyncDisplayKit/Private/ASDisplayNodeLayoutContext.mm index 446bdb2439..128b0177c6 100644 --- a/AsyncDisplayKit/Private/ASDisplayNodeLayoutContext.mm +++ b/AsyncDisplayKit/Private/ASDisplayNodeLayoutContext.mm @@ -23,8 +23,8 @@ BOOL _calculatedSubnodeOperations; NSArray *_insertedSubnodes; NSArray *_removedSubnodes; - std::vector _insertedSubnodePositions; - std::vector _removedSubnodePositions; + std::vector _insertedSubnodePositions; + std::vector _removedSubnodePositions; } - (instancetype)initWithNode:(ASDisplayNode *)node @@ -48,8 +48,8 @@ { ASDN::MutexLocker l(_propertyLock); [self calculateSubnodeOperationsIfNeeded]; - for (NSInteger i = 0; i < [_insertedSubnodes count]; i++) { - NSInteger p = _insertedSubnodePositions[i]; + for (NSUInteger i = 0; i < [_insertedSubnodes count]; i++) { + NSUInteger p = _insertedSubnodePositions[i]; [_node insertSubnode:_insertedSubnodes[i] atIndex:p]; } } @@ -58,7 +58,7 @@ { ASDN::MutexLocker l(_propertyLock); [self calculateSubnodeOperationsIfNeeded]; - for (NSInteger i = 0; i < [_removedSubnodes count]; i++) { + for (NSUInteger i = 0; i < [_removedSubnodes count]; i++) { [_removedSubnodes[i] removeFromSupernode]; } } @@ -77,15 +77,15 @@ compareBlock:^BOOL(ASLayout *lhs, ASLayout *rhs) { return ASObjectIsEqual(lhs.layoutableObject, rhs.layoutableObject); }]; - filterNodesInLayoutAtIndexes(_pendingLayout, insertions, &_insertedSubnodes, &_insertedSubnodePositions); - filterNodesInLayoutAtIndexesWithIntersectingNodes(_previousLayout, + findNodesInLayoutAtIndexes(_pendingLayout, insertions, &_insertedSubnodes, &_insertedSubnodePositions); + findNodesInLayoutAtIndexesWithFilteredNodes(_previousLayout, deletions, _insertedSubnodes, &_removedSubnodes, &_removedSubnodePositions); } else { NSIndexSet *indexes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [_pendingLayout.immediateSublayouts count])]; - filterNodesInLayoutAtIndexes(_pendingLayout, indexes, &_insertedSubnodes, &_insertedSubnodePositions); + findNodesInLayoutAtIndexes(_pendingLayout, indexes, &_insertedSubnodes, &_insertedSubnodePositions); _removedSubnodes = nil; } _calculatedSubnodeOperations = YES; @@ -142,42 +142,31 @@ /** * @abstract Stores the nodes at the given indexes in the `storedNodes` array, storing indexes in a `storedPositions` c++ vector. */ -static inline void filterNodesInLayoutAtIndexes( - ASLayout *layout, - NSIndexSet *indexes, - NSArray * __strong *storedNodes, - std::vector *storedPositions - ) +static inline void findNodesInLayoutAtIndexes(ASLayout *layout, + NSIndexSet *indexes, + NSArray * __strong *storedNodes, + std::vector *storedPositions) { - filterNodesInLayoutAtIndexesWithIntersectingNodes(layout, indexes, nil, storedNodes, storedPositions); + findNodesInLayoutAtIndexesWithFilteredNodes(layout, indexes, nil, storedNodes, storedPositions); } /** * @abstract Stores the nodes at the given indexes in the `storedNodes` array, storing indexes in a `storedPositions` c++ vector. - * @discussion If the node exists in the `intersectingNodes` array, the node is not added to `storedNodes`. + * @discussion If the node exists in the `filteredNodes` array, the node is not added to `storedNodes`. */ -static inline void filterNodesInLayoutAtIndexesWithIntersectingNodes( - ASLayout *layout, - NSIndexSet *indexes, - NSArray *intersectingNodes, - NSArray * __strong *storedNodes, - std::vector *storedPositions - ) +static inline void findNodesInLayoutAtIndexesWithFilteredNodes(ASLayout *layout, + NSIndexSet *indexes, + NSArray *filteredNodes, + NSArray * __strong *storedNodes, + std::vector *storedPositions) { NSMutableArray *nodes = [NSMutableArray array]; - std::vector positions = std::vector(); - NSInteger idx = [indexes firstIndex]; + std::vector positions = std::vector(); + NSUInteger idx = [indexes firstIndex]; while (idx != NSNotFound) { - BOOL skip = NO; ASDisplayNode *node = (ASDisplayNode *)layout.immediateSublayouts[idx].layoutableObject; ASDisplayNodeCAssert(node, @"A flattened layout must consist exclusively of node sublayouts"); - for (ASDisplayNode *i in intersectingNodes) { - if (node == i) { - skip = YES; - break; - } - } - if (!skip) { + if (node != nil && [filteredNodes indexOfObjectIdenticalTo:node] != NSNotFound) { [nodes addObject:node]; positions.push_back(idx); }