This is Set.m in view mode; [Download] [Up]
/* Implementation for Objective-C Set collection object Copyright (C) 1993 R. Andrew McCallum <mccallum@cs.rochester.edu> Dept. of Computer Science, U. of Rochester, Rochester, NY 14627 This file is part of the GNU Objective-C Collection library. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include <coll/Set.h> #include <coll/CollectionPrivate.h> #include <objc/objc-api.h> #define DEFAULT_SET_CAPACITY 32 @implementation Set + initialize { if (self == [Set class]) [self setVersion:-1]; /* alpha release */ return self; } // MANAGING CAPACITY; /* Eventually we will want to have better capacity management, potentially keep default capacity as a class variable. */ + (unsigned) defaultCapacity { return DEFAULT_SET_CAPACITY; } // INITIALIZING AND FREEING; /* This is the designated initializer of this class */ - initDescription:(const char *)type capacity: (unsigned)aCapacity { hash_func_type hash_func; [super initDescription:type]; /* Need to make sure that aCapacity is power of 2 */ /* I don't think this is very portable... Change this switch. */ switch (*_description) { case _C_CHARPTR: case _C_ATOM: hash_func = (hash_func_type)coll_hash_string; break; case _C_ID: case _C_CLASS: /* I can send classes isEqual:? */ hash_func = (hash_func_type)coll_hash_object; break; case _C_PTR: case _C_SEL: hash_func = (hash_func_type)coll_hash_ptr; break; case _C_INT: case _C_UINT: case _C_FLT: /* Yipes */ hash_func = (hash_func_type)coll_hash_int; break; case _C_LNG: case _C_ULNG: hash_func = (hash_func_type)coll_hash_long_int; break; default : [self error:"Element type (%s) not supported.\n", _description]; } _contents_hash = hash_new(aCapacity, (hash_func_type)hash_func, (compare_func_type)_compare_func); return self; } - initCapacity: (unsigned)aCapacity { return [self initDescription:"@" capacity:aCapacity]; } - initDescription: (const char *)type { return [self initDescription:type capacity:[[self class] defaultCapacity]]; } - free { hash_delete(_contents_hash); return [super free]; } // SET OPERATIONS; - intersectWith: (id <Collecting>)aCollection { [self removeContentsNotIn:aCollection]; return self; } - unionWith: (id <Collecting>)aCollection { [self addContentsOfIfAbsent:aCollection]; return self; } - differenceWith: (id <Collecting>)aCollection { [self removeContentsIn:aCollection]; return self; } - shallowCopyIntersectWith: (id <Collecting>)aCollection { id newColl = [self emptyCopyAs:[self species]]; void doIt(elt e) { if ([aCollection includesElement:e]) [newColl addElement:e]; } [self withElementsCall:doIt]; return newColl; } - shallowCopyUnionWith: (id <Collecting>)aCollection { id newColl = [self shallowCopy]; [newColl addContentsOf:aCollection]; return newColl; } - shallowCopyDifferenceWith: (id <Collecting>)aCollection { id newColl = [self emptyCopyAs:[self species]]; void doIt(elt e) { if (![aCollection includesElement:e]) [newColl addElement:e]; } [self withElementsCall:doIt]; return newColl; } // ADDING; - addElement: (elt)anElement { if (!hash_value_for_key(_contents_hash, anElement.void_ptr_t)) hash_add(&_contents_hash, anElement.void_ptr_t, (void*)1); return self; } // REMOVING AND REPLACING; - (elt) removeElement: (elt)oldElement { if (!hash_value_for_key(_contents_hash, oldElement.void_ptr_t)) hash_remove(_contents_hash, oldElement.void_ptr_t); else [self errorElementNotFound:oldElement inMethod:_cmd]; return oldElement; } - empty { node_ptr node; while (node = hash_next(_contents_hash, 0)) hash_remove(_contents_hash, node->key); return self; } - uniqueContents { return self; } // TESTING; - (BOOL) includesElement: (elt)anElement { if (hash_value_for_key(_contents_hash, anElement.void_ptr_t)) return YES; else return NO; } - (unsigned) count { return _contents_hash->used; } - (unsigned) occurrencesOfElement: (elt)anElement { if ([self includesElement:anElement]) return 1; else return 0; } // ENUMERATING; - (BOOL) getNextElement:(elt *)anElementPtr withEnumState: (void**)enumState { *enumState = hash_next(_contents_hash, *enumState); if (*enumState) { *anElementPtr = ((node_ptr)(*enumState))->key; return YES; } return NO; } - withElementsCall: (void(*)(elt))aFunc whileTrue:(BOOL *)flag { node_ptr node = 0; while (*flag && (node = hash_next(_contents_hash, node))) { aFunc(node->key); } return self; } - withElementsCall: (void(*)(elt))aFunc { node_ptr node = 0; while (node = hash_next(_contents_hash, node)) { aFunc(node->key); } return self; } @end
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.