Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @interface GraphNode : NSObject
- @property (nonatomic, copy) NSArray<GraphNode *> *neighbors;
- @property (nonatomic, copy) NSString *name;
- @end
- @interface Graph : NSObject<NSCopying>
- @property (nonatomic, copy) NSArray<GraphNode *> *nodes;
- @end
- @implementation GraphNode
- - (instancetype)init {
- self = [super init];
- if(self) {
- _neighbors = [NSMutableArray new];
- }
- return self;
- }
- @end
- @implementation Graph
- - (instancetype)init {
- self = [super init];
- if(self) {
- _nodes = [NSMutableArray new];
- }
- return self;
- }
- - (id)copyWithZone:(NSZone *)zone {
- NSMutableArray<GraphNode *> *copiedNodes = [NSMutableArray new];
- NSMutableDictionary<NSString *, GraphNode *> *copies = [NSMutableDictionary new];
- for(GraphNode *node in self.nodes) {
- GraphNode *copied = [GraphNode new];
- copied.name = node.name;
- copied.neighbors = [node.neighbors copy];
- [copiedNodes addObject:copied];
- copies[node.name] = copied;
- }
- for(GraphNode *node in self.nodes) {
- NSMutableArray<GraphNode *> *copiedNeighbors = [NSMutableArray new];
- for(GraphNode *neighbor in node.neighbors) {
- [copiedNeighbors addObject:copies[neighbor.name]];
- }
- copies[node.name].neighbors = copiedNeighbors;
- }
- Graph *copy = [Graph new];
- copy.nodes = copiedNodes;
- return copy;
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement