88// of patent rights can be found in the PATENTS file in the same directory.
99//
1010
11- #import " ASLayoutSpec+Private .h"
11+ #import " ASLayoutSpec.h"
1212
1313#import " ASAssert.h"
1414#import " ASEnvironmentInternal.h"
1717#import " ASThread.h"
1818#import " ASTraitCollection.h"
1919
20+ #import < objc/runtime.h>
21+ #import < map>
2022#import < vector>
2123
24+ typedef std::map<unsigned long , id <ASLayoutable>, std::less<unsigned long >> ASChildMap;
25+
2226@interface ASLayoutSpec () {
2327 ASEnvironmentState _environmentState;
2428 ASDN::RecursiveMutex __instanceLock__;
25- ASChildrenMap _childrenMap ;
29+ ASChildMap _children ;
2630}
2731@end
2832
@@ -31,7 +35,6 @@ @implementation ASLayoutSpec
3135// these dynamic properties all defined in ASLayoutOptionsPrivate.m
3236@dynamic spacingAfter, spacingBefore, flexGrow, flexShrink, flexBasis,
3337 alignSelf, ascender, descender, sizeRange, layoutPosition, layoutableType;
34- @synthesize parent = _parent;
3538@synthesize isFinalLayoutable = _isFinalLayoutable;
3639
3740- (instancetype )init
@@ -102,35 +105,27 @@ - (ASLayout *)measureWithSizeRange:(ASSizeRange)constrainedSize
102105 return child;
103106}
104107
105- #pragma mark - Parent
106-
107108- (void )setParent : (id <ASLayoutable>)parent
108109{
110+ // FIXME: Locking should be evaluated here. _parent is not widely used yet, though.
109111 _parent = parent;
110112
111113 if ([parent supportsUpwardPropagation ]) {
112114 ASEnvironmentStatePropagateUp (parent, self.environmentState .layoutOptionsState );
113115 }
114116}
115117
116- - (id <ASLayoutable>)parent
117- {
118- return _parent;
119- }
120-
121- #pragma mark - Children
122-
123118- (void )setChild : (id <ASLayoutable>)child
124119{
125120 ASDisplayNodeAssert (self.isMutable , @" Cannot set properties when layout spec is not mutable" );
126121 if (child) {
127122 id <ASLayoutable> finalLayoutable = [self layoutableToAddFromLayoutable: child];
128123 if (finalLayoutable) {
129- _childrenMap [0 ] = finalLayoutable;
124+ _children [0 ] = finalLayoutable;
130125 [self propagateUpLayoutable: finalLayoutable];
131126 }
132127 } else {
133- _childrenMap .erase (0 );
128+ _children .erase (0 );
134129 }
135130}
136131
@@ -139,9 +134,9 @@ - (void)setChild:(id<ASLayoutable>)child forIndex:(NSUInteger)index
139134 ASDisplayNodeAssert (self.isMutable , @" Cannot set properties when layout spec is not mutable" );
140135 if (child) {
141136 id <ASLayoutable> finalLayoutable = [self layoutableToAddFromLayoutable: child];
142- _childrenMap [index] = finalLayoutable;
137+ _children [index] = finalLayoutable;
143138 } else {
144- _childrenMap .erase (index);
139+ _children .erase (index);
145140 }
146141 // TODO: Should we propagate up the layoutable at it could happen that multiple children will propagated up their
147142 // layout options and one child will overwrite values from another child
@@ -152,67 +147,35 @@ - (void)setChildren:(NSArray<id<ASLayoutable>> *)children
152147{
153148 ASDisplayNodeAssert (self.isMutable , @" Cannot set properties when layout spec is not mutable" );
154149
155- _childrenMap .clear ();
150+ _children .clear ();
156151 NSUInteger i = 0 ;
157152 for (id <ASLayoutable> child in children) {
158- _childrenMap [i] = [self layoutableToAddFromLayoutable: child];
153+ _children [i] = [self layoutableToAddFromLayoutable: child];
159154 i += 1 ;
160155 }
161156}
162157
163158- (id <ASLayoutable>)childForIndex : (NSUInteger )index
164159{
165- if (index < _childrenMap .size ()) {
166- return _childrenMap [index];
160+ if (index < _children .size ()) {
161+ return _children [index];
167162 }
168163 return nil ;
169164}
170165
171166- (id <ASLayoutable>)child
172167{
173- return _childrenMap [0 ];
168+ return _children [0 ];
174169}
175170
176171- (NSArray *)children
177172{
178- // If used inside ASDK, the childrenMap property should be preferred over the children array to prevent unecessary
179- // boxing
180173 std::vector<ASLayout *> children;
181- for (auto const &entry : _childrenMap) {
182- children.push_back (entry.second );
183- }
184-
185- return [NSArray arrayWithObjects: &children[0 ] count: children.size ()];
186- }
187-
188- #pragma mark - NSFastEnumeration
189-
190- - (NSUInteger )countByEnumeratingWithState : (NSFastEnumerationState *)state
191- objects : (id __unsafe_unretained [])stackbuf
192- count : (NSUInteger )stackbufLength
193- {
194- NSUInteger count = 0 ;
195- unsigned long countOfItemsAlreadyEnumerated = state->state ;
196-
197- if (countOfItemsAlreadyEnumerated == 0 ) {
198- state->mutationsPtr = &state->extra [0 ];
199- }
200-
201- if (countOfItemsAlreadyEnumerated < _childrenMap.size ()) {
202- state->itemsPtr = stackbuf;
203-
204- while ((countOfItemsAlreadyEnumerated < _childrenMap.size ()) && (count < stackbufLength)) {
205- stackbuf[count] = _childrenMap[countOfItemsAlreadyEnumerated];
206- countOfItemsAlreadyEnumerated++;
207- count++;
208- }
209- } else {
210- count = 0 ;
174+ for (ASChildMap::iterator it = _children.begin (); it != _children.end (); ++it ) {
175+ children.push_back (it->second );
211176 }
212177
213- state->state = countOfItemsAlreadyEnumerated;
214-
215- return count;
178+ return [NSArray arrayWithObjects: &children[0 ] count: children.size ()];
216179}
217180
218181#pragma mark - ASEnvironment
@@ -270,15 +233,6 @@ - (ASTraitCollection *)asyncTraitCollection
270233
271234@end
272235
273- @implementation ASLayoutSpec (Private)
274-
275- - (ASChildrenMap)childrenMap
276- {
277- return _childrenMap;
278- }
279-
280- @end
281-
282236@implementation ASLayoutSpec (Debugging)
283237
284238#pragma mark - ASLayoutableAsciiArtProtocol
0 commit comments