OLD | NEW |
(Empty) | |
| 1 // -*- mode: ObjC -*- |
| 2 |
| 3 // This file is part of class-dump, a utility for examining the Objective-C seg
ment of Mach-O files. |
| 4 // Copyright (C) 1997-1998, 2000-2001, 2004-2010 Steve Nygard. |
| 5 |
| 6 #import "NSArray-Extensions.h" |
| 7 |
| 8 #import "CDTopoSortNode.h" |
| 9 |
| 10 @implementation NSArray (CDExtensions) |
| 11 |
| 12 - (NSArray *)reversedArray; |
| 13 { |
| 14 return [[self reverseObjectEnumerator] allObjects]; |
| 15 } |
| 16 |
| 17 - (NSArray *)arrayByMappingSelector:(SEL)aSelector; |
| 18 { |
| 19 NSMutableArray *newArray; |
| 20 |
| 21 newArray = [NSMutableArray array]; |
| 22 for (id object in self) { |
| 23 id value = [object performSelector:aSelector]; |
| 24 if (value != nil) |
| 25 [newArray addObject:value]; |
| 26 // TODO (2004-01-28): Or we could add NSNull. |
| 27 } |
| 28 |
| 29 return newArray; |
| 30 } |
| 31 |
| 32 @end |
| 33 |
| 34 @implementation NSArray (CDTopoSort) |
| 35 |
| 36 - (NSArray *)topologicallySortedArray; |
| 37 { |
| 38 NSMutableDictionary *nodesByName; |
| 39 NSMutableArray *sortedArray; |
| 40 NSArray *allNodes; |
| 41 |
| 42 nodesByName = [[NSMutableDictionary alloc] init]; |
| 43 |
| 44 for (id <CDTopologicalSort> anObject in self) { |
| 45 NSString *identifier; |
| 46 CDTopoSortNode *aNode; |
| 47 |
| 48 aNode = [[CDTopoSortNode alloc] initWithObject:anObject]; |
| 49 [aNode addDependanciesFromArray:[anObject dependancies]]; |
| 50 |
| 51 identifier = [aNode identifier]; |
| 52 if ([nodesByName objectForKey:identifier] != nil) |
| 53 NSLog(@"Warning: Duplicate identifier (%@) in %s", identifier, _cmd)
; |
| 54 [nodesByName setObject:aNode forKey:identifier]; |
| 55 [aNode release]; |
| 56 } |
| 57 |
| 58 sortedArray = [NSMutableArray array]; |
| 59 |
| 60 allNodes = [[nodesByName allValues] sortedArrayUsingSelector:@selector(ascen
dingCompareByIdentifier:)]; |
| 61 for (CDTopoSortNode *aNode in allNodes) { |
| 62 if ([aNode color] == CDWhiteNodeColor) |
| 63 [aNode topologicallySortNodes:nodesByName intoArray:sortedArray]; |
| 64 } |
| 65 |
| 66 [nodesByName release]; |
| 67 |
| 68 return sortedArray; |
| 69 } |
| 70 |
| 71 @end |
| 72 |
| 73 |
| 74 @implementation NSMutableArray (CDTopoSort) |
| 75 |
| 76 - (void)sortTopologically; |
| 77 { |
| 78 NSArray *sortedArray; |
| 79 |
| 80 sortedArray = [self topologicallySortedArray]; |
| 81 assert([self count] == [sortedArray count]); |
| 82 |
| 83 [self removeAllObjects]; |
| 84 [self addObjectsFromArray:sortedArray]; |
| 85 } |
| 86 |
| 87 @end |
OLD | NEW |