/CDTopoSortNode.m

http://i386codedump.googlecode.com/ · Objective C · 108 lines · 82 code · 23 blank · 3 comment · 8 complexity · c3f6ada97673f5115e0edee3c7ff9ffe MD5 · raw file

  1. // This file is part of class-dump, a utility for examining the Objective-C segment of Mach-O files.
  2. // Copyright (C) 1997-1998, 2000-2001, 2004-2006 Steve Nygard
  3. #import "CDTopoSortNode.h"
  4. #import <Foundation/Foundation.h>
  5. #import "NSObject-CDExtensions.h"
  6. @implementation CDTopoSortNode
  7. - (id)initWithObject:(id <CDTopologicalSort>)anObject;
  8. {
  9. if ([super init] == nil)
  10. return nil;
  11. representedObject = [anObject retain];
  12. dependancies = [[NSMutableSet alloc] init];
  13. color = CDWhiteNodeColor;
  14. [self addDependanciesFromArray:[representedObject dependancies]];
  15. return self;
  16. }
  17. - (void)dealloc;
  18. {
  19. [representedObject release];
  20. [dependancies release];
  21. [super dealloc];
  22. }
  23. - (NSString *)identifier;
  24. {
  25. return [representedObject identifier];
  26. }
  27. - (id <CDTopologicalSort>)representedObject;
  28. {
  29. return representedObject;
  30. }
  31. - (NSArray *)dependancies;
  32. {
  33. return [dependancies allObjects];
  34. }
  35. - (void)addDependancy:(NSString *)anIdentifier;
  36. {
  37. [dependancies addObject:anIdentifier];
  38. }
  39. - (void)removeDependancy:(NSString *)anIdentifier;
  40. {
  41. [dependancies removeObject:anIdentifier];
  42. }
  43. - (void)addDependanciesFromArray:(NSArray *)identifiers;
  44. {
  45. [self performSelector:@selector(addDependancy:) withObjectsFromArray:identifiers];
  46. //[identifiers makeObject:self performSelector:@selector(addDependancy:)];
  47. }
  48. - (CDNodeColor)color;
  49. {
  50. return color;
  51. }
  52. - (void)setColor:(CDNodeColor)newColor;
  53. {
  54. color = newColor;
  55. }
  56. - (NSString *)description;
  57. {
  58. return [NSString stringWithFormat:@"%@ (%d) depends on %@", [self identifier], color, [[dependancies allObjects] componentsJoinedByString:@", "]];
  59. }
  60. - (NSComparisonResult)ascendingCompareByIdentifier:(id)otherNode;
  61. {
  62. return [[self identifier] compare:[otherNode identifier]];
  63. }
  64. - (void)topologicallySortNodes:(NSDictionary *)nodesByIdentifier intoArray:(NSMutableArray *)sortedArray;
  65. {
  66. NSArray *dependantIdentifiers;
  67. int count, index;
  68. NSString *anIdentifier;
  69. CDTopoSortNode *aNode;
  70. dependantIdentifiers = [self dependancies];
  71. count = [dependantIdentifiers count];
  72. for (index = 0; index < count; index++) {
  73. anIdentifier = [dependantIdentifiers objectAtIndex:index];
  74. aNode = [nodesByIdentifier objectForKey:anIdentifier];
  75. if ([aNode color] == CDWhiteNodeColor) {
  76. [aNode setColor:CDGrayNodeColor];
  77. [aNode topologicallySortNodes:nodesByIdentifier intoArray:sortedArray];
  78. } else if ([aNode color] == CDGrayNodeColor) {
  79. NSLog(@"Warning: Possible circular reference? %@ -> %@", [self identifier], [aNode identifier]);
  80. }
  81. }
  82. [sortedArray addObject:[self representedObject]];
  83. [self setColor:CDBlackNodeColor];
  84. }
  85. @end