Permalink
Browse files

MRC-compliant

  • Loading branch information...
1 parent cd1d817 commit 2012751fb9bc89ae740f97046d007bf6809dbf02 @martinh martinh committed Sep 21, 2011
Showing with 90 additions and 78 deletions.
  1. +9 −7 app/MHSysTree.h
  2. +81 −71 app/MHSysTree.m
View
@@ -27,9 +27,9 @@
struct rb_entry
{
- RB_ENTRY(rb_entry) entry;
- id obj;
- SEL compareSelector;
+ RB_ENTRY(rb_entry) entry;
+ id obj;
+ SEL compareSelector;
};
/* This is an objective-c wrapper around sys_tree. sys_tree is a red-black tree
@@ -38,10 +38,12 @@ struct rb_entry
@interface MHSysTree : NSObject
{
- unsigned nitems;
- RB_HEAD(id_tree, rb_entry) root;
- SEL compareSelector;
- NSMutableSet *hack;
+ unsigned _nitems;
+ RB_HEAD(id_tree, rb_entry) _root;
+ SEL _compareSelector;
+#ifdef __OBJC_GC__
+ NSMutableSet *_strongRefs;
+#endif
}
int id_cmp(struct rb_entry *a, struct rb_entry *b);
View
@@ -33,154 +33,164 @@ @implementation MHSysTree
int
id_cmp(struct rb_entry *a, struct rb_entry *b)
{
- return (int)((pointer_t)[a->obj performSelector:a->compareSelector withObject:b->obj]);
+ return (int)((pointer_t)[a->obj performSelector:a->compareSelector withObject:b->obj]);
}
- (id)initWithCompareSelector:(SEL)aSelector
{
- if ((self = [super init]) != nil) {
- RB_INIT(&root);
- nitems = 0;
- compareSelector = aSelector;
- hack = [[NSMutableSet alloc] init];
- }
-
- return self;
+ if ((self = [super init]) != nil) {
+ RB_INIT(&_root);
+ _nitems = 0;
+ _compareSelector = aSelector;
+#ifdef __OBJC_GC__
+ _strongRefs = [[NSMutableSet alloc] init];
+#endif
+ }
+
+ return self;
}
- (id)init
{
- return [self initWithCompareSelector:@selector(compare:)];
+ return [self initWithCompareSelector:@selector(compare:)];
+}
+
+- (void)dealloc
+{
+ [self removeAllObjects];
+ [super dealloc];
}
- (void)finalize
{
- [self removeAllObjects];
- [super finalize];
+ [self removeAllObjects];
+ [super finalize];
}
- (void)addObject:(id)anObject
{
- struct rb_entry *e = malloc(sizeof(*e));
- e->obj = anObject;
- [hack addObject:anObject];
- e->compareSelector = compareSelector;
- struct rb_entry *duplicate = RB_INSERT(id_tree, &root, e);
- if (duplicate) {
- INFO(@"*********** got duplicate object %@ for new object %@", duplicate->obj, anObject);
- [self removeEntry:duplicate];
- duplicate = RB_INSERT(id_tree, &root, e);
- if (duplicate) {
- INFO(@"!!!!!!!!!!! got second duplicate object %p for new object %p", duplicate->obj, anObject);
- }
- }
- nitems++;
+ struct rb_entry *e = malloc(sizeof(*e));
+ e->obj = [anObject retain];
+#ifdef __OBJC_GC__
+ [_strongRefs addObject:anObject];
+#endif
+ e->compareSelector = _compareSelector;
+ struct rb_entry *duplicate = RB_INSERT(id_tree, &_root, e);
+ if (duplicate) {
+ INFO(@"*********** got duplicate object %@ for new object %@", duplicate->obj, anObject);
+ [self removeEntry:duplicate];
+ duplicate = RB_INSERT(id_tree, &_root, e);
+ if (duplicate) {
+ INFO(@"!!!!!!!!!!! got second duplicate object %p for new object %p", duplicate->obj, anObject);
+ }
+ }
+ _nitems++;
}
- (void)addObjectsFromArray:(NSArray *)anArray
{
- id obj;
- for (obj in anArray)
- [self addObject:obj];
+ for (id obj in anArray)
+ [self addObject:obj];
}
- (void)removeEntry:(struct rb_entry *)anEntry
{
- RB_REMOVE(id_tree, &root, anEntry);
- [hack removeObject:anEntry->obj];
- free(anEntry);
- nitems--;
+ RB_REMOVE(id_tree, &_root, anEntry);
+#ifdef __OBJC_GC__
+ [_strongRefs removeObject:anEntry->obj];
+#endif
+ [anEntry->obj release];
+ free(anEntry);
+ _nitems--;
}
- (void)removeObject:(id)anObject
{
- struct rb_entry *e = [self lookup:anObject];
- if (e)
- [self removeEntry:e];
+ struct rb_entry *e = [self lookup:anObject];
+ if (e)
+ [self removeEntry:e];
}
- (unsigned)count
{
- return nitems;
+ return _nitems;
}
- (BOOL)containsObject:(id)anObject
{
- return [self lookup:anObject] == nil ? NO : YES;
+ return [self lookup:anObject] == nil ? NO : YES;
}
- (id)find:(id)anObject
{
- struct rb_entry *e = [self lookup:anObject];
-
- return e ? e->obj : nil;
+ struct rb_entry *e = [self lookup:anObject];
+ return e ? e->obj : nil;
}
- (struct rb_entry *)lookup:(id)anObject
{
- struct rb_entry find;
- find.obj = anObject;
- find.compareSelector = compareSelector;
-
- return RB_FIND(id_tree, &root, &find);
+ struct rb_entry find;
+ find.obj = anObject;
+ find.compareSelector = _compareSelector;
+
+ return RB_FIND(id_tree, &_root, &find);
}
- (NSArray *)allObjects
{
- NSMutableArray *array = [NSMutableArray arrayWithCapacity:nitems];
- struct rb_entry *e;
- RB_FOREACH(e, id_tree, &root) {
- [array addObject:e->obj];
- }
+ NSMutableArray *array = [NSMutableArray arrayWithCapacity:_nitems];
+ struct rb_entry *e;
+ RB_FOREACH(e, id_tree, &_root) {
+ [array addObject:e->obj];
+ }
- return array;
+ return array;
}
- (void)removeAllObjects
{
- struct rb_entry *e;
- while ((e = RB_MIN(id_tree, &root)) != NULL) {
- [self removeEntry:e];
- }
+ struct rb_entry *e;
+ while ((e = RB_MIN(id_tree, &_root)) != NULL) {
+ [self removeEntry:e];
+ }
}
- (MHSysTree *)copy
{
- MHSysTree *cp = [[MHSysTree alloc] initWithCompareSelector:compareSelector];
- struct rb_entry *e;
- RB_FOREACH(e, id_tree, &root) {
- [cp addObject:e->obj];
- }
- return cp;
-
+ MHSysTree *cp = [[MHSysTree alloc] initWithCompareSelector:_compareSelector];
+ struct rb_entry *e;
+ RB_FOREACH(e, id_tree, &_root) {
+ [cp addObject:e->obj];
+ }
+ return cp;
}
- (void)makeObjectsPerformSelector:(SEL)aSelector target:(id)aTarget context:(id)context
{
- struct rb_entry *e;
- RB_FOREACH(e, id_tree, &root) {
- [aTarget performSelector:aSelector withObject:e->obj withObject:context];
- }
+ struct rb_entry *e;
+ RB_FOREACH(e, id_tree, &_root) {
+ [aTarget performSelector:aSelector withObject:e->obj withObject:context];
+ }
}
- (struct rb_entry *)root
{
- return RB_ROOT(&root);
+ return RB_ROOT(&_root);
}
- (struct rb_entry *)first
{
- return RB_MIN(id_tree, &root);
+ return RB_MIN(id_tree, &_root);
}
- (struct rb_entry *)last
{
- return RB_MAX(id_tree, &root);
+ return RB_MAX(id_tree, &_root);
}
- (struct rb_entry *)next:(struct rb_entry *)current
{
- return RB_NEXT(id_tree, &root, current);
+ return RB_NEXT(id_tree, &_root, current);
}
- (struct rb_entry *)left:(struct rb_entry *)current

0 comments on commit 2012751

Please sign in to comment.