September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,56 +0,0 @@
|
|||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface NSArray (FlattenExt)
|
||||
-(NSArray *)flatten;
|
||||
@end
|
||||
|
||||
@implementation NSArray (FlattenExt)
|
||||
-(NSArray *)flatten
|
||||
{
|
||||
NSMutableArray *r = [NSMutableArray array];
|
||||
NSEnumerator *en = [self objectEnumerator];
|
||||
id o;
|
||||
while ( (o = [en nextObject]) ) {
|
||||
if ( [o isKindOfClass: [NSArray class]] )
|
||||
[r addObjectsFromArray: [o flatten]];
|
||||
else
|
||||
[r addObject: o];
|
||||
}
|
||||
return [NSArray arrayWithArray: r];
|
||||
}
|
||||
@end
|
||||
|
||||
int main()
|
||||
{
|
||||
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
||||
|
||||
NSArray *p = [NSArray
|
||||
arrayWithObjects:
|
||||
[NSArray arrayWithObjects: [NSNumber numberWithInt: 1], nil],
|
||||
[NSNumber numberWithInt: 2],
|
||||
[NSArray arrayWithObjects:
|
||||
[NSArray arrayWithObjects: [NSNumber numberWithInt: 3],
|
||||
[NSNumber numberWithInt: 4], nil],
|
||||
[NSNumber numberWithInt: 5], nil],
|
||||
[NSArray arrayWithObjects:
|
||||
[NSArray arrayWithObjects:
|
||||
[NSArray arrayWithObjects: nil], nil], nil],
|
||||
[NSArray arrayWithObjects:
|
||||
[NSArray arrayWithObjects:
|
||||
[NSArray arrayWithObjects:
|
||||
[NSNumber numberWithInt: 6], nil], nil], nil],
|
||||
[NSNumber numberWithInt: 7],
|
||||
[NSNumber numberWithInt: 8],
|
||||
[NSArray arrayWithObjects: nil], nil];
|
||||
NSArray *f = [p flatten];
|
||||
NSEnumerator *e = [f objectEnumerator];
|
||||
id o;
|
||||
while( (o = [e nextObject]) )
|
||||
{
|
||||
NSLog(@"%@", o);
|
||||
}
|
||||
|
||||
|
||||
[pool drain];
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,40 +0,0 @@
|
|||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface NSArray (FlattenExt)
|
||||
@property (nonatomic, readonly) NSArray *flattened;
|
||||
@end
|
||||
|
||||
@implementation NSArray (FlattenExt)
|
||||
-(NSArray *) flattened {
|
||||
NSMutableArray *flattened = [[NSMutableArray alloc] initWithCapacity:self.count];
|
||||
|
||||
for (id object in self) {
|
||||
if ([object isKindOfClass:[NSArray class]])
|
||||
[flattened addObjectsFromArray:((NSArray *)object).flattened];
|
||||
else
|
||||
[flattened addObject:object];
|
||||
}
|
||||
|
||||
return [flattened autorelease];
|
||||
}
|
||||
@end
|
||||
|
||||
int main() {
|
||||
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
||||
NSArray *unflattened = [NSArray arrayWithObjects:[NSArray arrayWithObject:[NSNumber numberWithInteger:1]],
|
||||
[NSNumber numberWithInteger:2],
|
||||
[NSArray arrayWithObjects:[NSArray arrayWithObjects:[NSNumber numberWithInteger:3], [NSNumber numberWithInteger:4], nil],
|
||||
[NSNumber numberWithInteger:5], nil],
|
||||
[NSArray arrayWithObject:[NSArray arrayWithObject:[NSArray array]]],
|
||||
[NSArray arrayWithObject:[NSArray arrayWithObject:[NSArray arrayWithObject:[NSNumber numberWithInteger:6]]]],
|
||||
[NSNumber numberWithInteger:7],
|
||||
[NSNumber numberWithInteger:8],
|
||||
[NSArray array], nil];
|
||||
|
||||
for (id object in unflattened.flattened)
|
||||
NSLog(@"%@", object);
|
||||
|
||||
[pool drain];
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue