This repository has been archived by the owner on Jul 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
COCopier.m
88 lines (74 loc) · 2.68 KB
/
COCopier.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#import "COCopier.h"
#import <EtoileFoundation/ETUUID.h>
@implementation COCopier
- (void) collectItemAndAllDescendents: (ETUUID *)aUUID
inSet: (NSMutableSet *)dest
fromGraph: (id<COItemGraph>)source
{
[dest addObject: aUUID];
for (ETUUID *child in [[source itemForUUID: aUUID] embeddedItemUUIDs])
{
if (![dest containsObject: child])
{
[self collectItemAndAllDescendents: child
inSet: dest
fromGraph: source];
}
else
{
[NSException raise: NSInvalidArgumentException
format: @"Cycle detected"];
}
}
}
- (NSSet*) itemAndAllDescendents: (ETUUID *)aUUID
fromGraph: (id<COItemGraph>)source
{
NSMutableSet *result = [NSMutableSet set];
[self collectItemAndAllDescendents: aUUID inSet: result fromGraph: source];
return result;
}
- (NSSet *) itemUUIDsToCopyForItemItemWithUUID: (ETUUID*)aUUID
fromGraph: (id<COItemGraph>)source
toGraph: (id<COItemGraph>)dest
{
NSSet *compositeObjectCopySet = [self itemAndAllDescendents: aUUID fromGraph: source];
NSMutableSet *result = [NSMutableSet setWithSet: compositeObjectCopySet];
for (ETUUID *uuid in compositeObjectCopySet)
{
for (ETUUID *referenced in [[source itemForUUID: uuid] referencedItemUUIDs])
{
if (![compositeObjectCopySet containsObject: referenced])
{
if ([dest itemForUUID: referenced] == nil)
{
// If not in dest, copy it
[result addObject: referenced];
}
}
}
}
return result;
}
- (ETUUID*) copyItemWithUUID: (ETUUID*)aUUID
fromGraph: (id<COItemGraph>)source
toGraph: (id<COItemGraph>)dest
{
NSSet *uuidsToCopy = [self itemUUIDsToCopyForItemItemWithUUID: aUUID
fromGraph: source
toGraph: dest];
NSMutableDictionary *mapping = [NSMutableDictionary dictionary];
for (ETUUID *oldUUID in uuidsToCopy)
{
[mapping setObject: [ETUUID UUID]
forKey: oldUUID];
}
for (ETUUID *uuid in uuidsToCopy)
{
COItem *oldItem = [source itemForUUID: uuid];
COItem *newItem = [[oldItem mutableCopyWithNameMapping: mapping] autorelease];
[dest addItem: newItem];
}
return [mapping objectForKey: aUUID];
}
@end