summaryrefslogtreecommitdiff
path: root/tikzit/src/common/Graph.m
diff options
context:
space:
mode:
authorrandomguy3 <randomguy3@7c02a99a-9b00-45e3-bf44-6f3dd7fddb64>2012-03-10 13:37:44 +0000
committerrandomguy3 <randomguy3@7c02a99a-9b00-45e3-bf44-6f3dd7fddb64>2012-03-10 13:37:44 +0000
commite6636042890e1dbfc442dfa0fedd9797c7cb8eff (patch)
tree20ce19e671bdd0e43fceed86172a41724c94926f /tikzit/src/common/Graph.m
parent0cd05a572e48b1e649ecdc8b24920c497e40466e (diff)
Make common code KVC compliant, and implement NSCopying where relevant
git-svn-id: https://tikzit.svn.sourceforge.net/svnroot/tikzit/trunk@420 7c02a99a-9b00-45e3-bf44-6f3dd7fddb64
Diffstat (limited to 'tikzit/src/common/Graph.m')
-rw-r--r--tikzit/src/common/Graph.m123
1 files changed, 70 insertions, 53 deletions
diff --git a/tikzit/src/common/Graph.m b/tikzit/src/common/Graph.m
index 398b562..9b20948 100644
--- a/tikzit/src/common/Graph.m
+++ b/tikzit/src/common/Graph.m
@@ -26,17 +26,36 @@
@implementation Graph
- (Graph*)init {
- [super init];
- data = [[GraphElementData alloc] init];
- boundingBox = NSMakeRect(0, 0, 0, 0);
- graphLock = [[NSRecursiveLock alloc] init];
+ self = [super init];
+ if (self != nil) {
+ data = [[GraphElementData alloc] init];
+ boundingBox = NSMakeRect(0, 0, 0, 0);
+ graphLock = [[NSRecursiveLock alloc] init];
+ nodes = [[NSMutableArray alloc] initWithCapacity:10];
+ edges = [[NSMutableArray alloc] initWithCapacity:10];
+ inEdges = nil;
+ outEdges = nil;
+ }
+ return self;
+}
+
+- (id) copyWithZone:(NSZone*)zone {
+ Graph *newGraph = [self copyOfSubgraphWithNodes:[NSSet setWithArray:nodes] zone:zone];
+ [newGraph setData:[self data]];
+ return newGraph;
+}
+
+- (void)dealloc {
[graphLock lock];
- nodes = [[NSMutableArray alloc] initWithCapacity:10];
- edges = [[NSMutableArray alloc] initWithCapacity:10];
- inEdges = nil;
- outEdges = nil;
+ [nodes release];
+ [edges release];
+ [data release];
+ [inEdges release];
+ [outEdges release];
[graphLock unlock];
- return self;
+ [graphLock release];
+
+ [super dealloc];
}
- (void)sync {
@@ -75,13 +94,23 @@
[graphLock unlock];
}
-- (NSArray*)nodes {
- return nodes;
-}
+@synthesize nodes;
+@synthesize edges;
-- (NSArray*)edges {
- return edges;
+@synthesize data;
+- (void) insertObject:(GraphElementProperty*)gep
+ inDataAtIndex:(NSUInteger)index {
+ [data insertObject:gep atIndex:index];
+}
+- (void) removeObjectFromDataAtIndex:(NSUInteger)index {
+ [data removeObjectAtIndex:index];
}
+- (void) replaceObjectInDataAtIndex:(NSUInteger)index
+ withObject:(GraphElementProperty*)gep {
+ [data replaceObjectAtIndex:index withObject:gep];
+}
+
+@synthesize boundingBox;
- (NSRect)bounds {
[graphLock lock];
@@ -90,19 +119,6 @@
return b;
}
-- (GraphElementData*)data { return data; }
-- (void)setData:(GraphElementData *)dt {
- if (data != dt) {
- [data release];
- data = [dt copy];
- }
-}
-
-- (NSRect)boundingBox { return boundingBox; }
-- (void)setBoundingBox:(NSRect)r {
- boundingBox = r;
-}
-
- (BOOL)hasBoundingBox {
return !(
boundingBox.size.width == 0 &&
@@ -504,22 +520,22 @@
}
- (Graph*)copyOfSubgraphWithNodes:(NSSet*)nds {
+ return [self copyOfSubgraphWithNodes:nds zone:NSDefaultMallocZone()];
+}
+
+- (Graph*)copyOfSubgraphWithNodes:(NSSet*)nds zone:(NSZone*)zone {
[graphLock lock];
- NSMapTable *newNds = [Graph nodeTableForNodes:nds];
- Graph* newGraph = [[Graph graph] retain];
+ NSMapTable *newNds = [Graph nodeTableForNodes:nds withZone:zone];
+ Graph* newGraph = [[Graph allocWithZone:zone] init];
- NSEnumerator *en = [newNds objectEnumerator];
- Node *nd;
- while ((nd = [en nextObject])) {
+ for (Node *nd in newNds) {
[newGraph addNode:nd];
}
- en = [edges objectEnumerator];
- Edge *e;
- while ((e = [en nextObject])) {
+ for (Edge *e in edges) {
if ([nds containsObject:[e source]] && [nds containsObject:[e target]]) {
- Edge *e1 = [e copy];
+ Edge *e1 = [e copyWithZone:zone];
[e1 setSource:[newNds objectForKey:[e source]]];
[e1 setTarget:[newNds objectForKey:[e target]]];
[newGraph addEdge:e1];
@@ -721,27 +737,21 @@
return code;
}
-- (void)dealloc {
- [graphLock lock];
- [nodes release];
- [edges release];
- [data release];
- [inEdges release];
- [outEdges release];
- [graphLock unlock];
- [graphLock release];
-
- [super dealloc];
-}
-
+ (Graph*)graph {
return [[[self alloc] init] autorelease];
}
+ (NSMapTable*)nodeTableForNodes:(NSSet*)nds {
- NSMapTable *tab = [NSMapTable mapTableWithStrongToStrongObjects];
+ return [self nodeTableForNodes:nds withZone:NSDefaultMallocZone()];
+}
+
++ (NSMapTable*)nodeTableForNodes:(NSSet*)nds withZone:(NSZone*)zone {
+ NSMapTable *tab = [[NSMapTable allocWithZone:zone]
+ initWithKeyOptions:NSMapTableStrongMemory
+ valueOptions:NSMapTableStrongMemory
+ capacity:[nds count]];
for (Node *n in nds) {
- Node *ncopy = [n copy];
+ Node *ncopy = [n copyWithZone:zone];
[tab setObject:ncopy forKey:n];
[ncopy release]; // tab should still retain ncopy.
}
@@ -749,9 +759,16 @@
}
+ (NSMapTable*)edgeTableForEdges:(NSSet*)es {
- NSMapTable *tab = [NSMapTable mapTableWithStrongToStrongObjects];
+ return [self edgeTableForEdges:es withZone:NSDefaultMallocZone()];
+}
+
++ (NSMapTable*)edgeTableForEdges:(NSSet*)es withZone:(NSZone*)zone {
+ NSMapTable *tab = [[NSMapTable allocWithZone:zone]
+ initWithKeyOptions:NSMapTableStrongMemory
+ valueOptions:NSMapTableStrongMemory
+ capacity:[es count]];
for (Edge *e in es) {
- Edge *ecopy = [e copy];
+ Edge *ecopy = [e copyWithZone:zone];
[tab setObject:ecopy forKey:e];
[ecopy release]; // tab should still retain ecopy.
}