diff --git a/Headers/gnustep/base/Array.h b/Headers/gnustep/base/Array.h index 6f49482d0..63e1ff8c8 100644 --- a/Headers/gnustep/base/Array.h +++ b/Headers/gnustep/base/Array.h @@ -1,5 +1,5 @@ /* Interface for Objective-C Array collection object - Copyright (C) 1993,1994 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum Date: May 1993 @@ -26,13 +26,19 @@ #include #include +#include -@interface Array : IndexedCollection +@interface ConstantArray : IndexedCollection { @public - int (*_comparison_function)(elt,elt); - elt *_contents_array; + id *_contents_array; unsigned int _count; +} +@end + +@interface Array : ConstantArray +{ + @public unsigned int _capacity; unsigned int _grow_factor; } @@ -40,16 +46,18 @@ + (unsigned) defaultCapacity; + (unsigned) defaultGrowFactor; -- initWithType: (const char *)contentEncoding - capacity: (unsigned)aCapacity; - initWithCapacity: (unsigned) aCapacity; -- setCapacity: (unsigned)newCapacity; +- (void) setCapacity: (unsigned)newCapacity; - (unsigned) growFactor; -- setGrowFactor: (unsigned)aNum; +- (void) setGrowFactor: (unsigned)aNum; @end +/* Put this on category instead of class to avoid bogus complaint from gcc */ +@interface Array (Ordering) +@end + #define FOR_ARRAY(ARRAY, ELEMENT_VAR) \ { \ unsigned _FOR_ARRAY_i; \ diff --git a/Headers/gnustep/base/Bag.h b/Headers/gnustep/base/Bag.h index 4e6a6e679..03142030f 100644 --- a/Headers/gnustep/base/Bag.h +++ b/Headers/gnustep/base/Bag.h @@ -1,8 +1,8 @@ /* Interface for Objective-C Bag collection object - Copyright (C) 1993,1994 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum - Date: May 1993 + Created: May 1993 This file is part of the GNU Objective C Class Library. @@ -25,39 +25,27 @@ #define __Bag_h_INCLUDE_GNU #include -#include +#include +#include -@interface Bag : Set +@interface Bag : Collection { + NSMapTable *_contents_map; unsigned int _count; // the number of elements; } +// INITIALIZING AND FREEING; +- initWithCapacity: (unsigned)aCapacity; + // ADDING; -- addObject: newObject withOccurrences: (unsigned)count; +- (void) addObject: newObject withOccurrences: (unsigned)count; // REMOVING AND REPLACING; -- removeObject: oldObject occurrences: (unsigned)count; -- removeObject: oldObject occurrences: (unsigned)count - ifAbsentCall: (id(*)(arglist_t))excFunc; +- (void) removeObject: oldObject occurrences: (unsigned)count; // TESTING; - (unsigned) uniqueCount; - -// NON-OBJECT ELEMENT METHOD NAMES; - -// INITIALIZING AND FREEING; -- initWithType: (const char *)contentEncoding - capacity: (unsigned)aCapacity; - -// ADDING; -- addElement: (elt)newElement withOccurrences: (unsigned)count; - -// REMOVING AND REPLACING; -- (elt) removeElement:(elt)oldElement occurrences: (unsigned)count; -- (elt) removeElement:(elt)oldElement occurrences: (unsigned)count - ifAbsentCall: (elt(*)(arglist_t))excFunc; - @end #endif /* __Bag_h_INCLUDE_GNU */ diff --git a/Headers/gnustep/base/BinaryTree.h b/Headers/gnustep/base/BinaryTree.h index ef35e54cd..168be9fed 100644 --- a/Headers/gnustep/base/BinaryTree.h +++ b/Headers/gnustep/base/BinaryTree.h @@ -1,8 +1,8 @@ /* Interface for Objective-C BinaryTree collection object - Copyright (C) 1993,1994 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum - Date: May 1993 + Created: May 1993 This file is part of the GNU Objective C Class Library. @@ -35,13 +35,15 @@ /* The protocol defines the interface to an object that may be an element in a BinaryTree. */ -@protocol BinaryTreeComprising +@protocol BinaryTreeComprising - leftNode; - rightNode; - parentNode; -- setLeftNode: (id )aNode; -- setRightNode: (id )aNode; -- setParentNode: (id )aNode; +- (void) setLeftNode: (id )aNode; +- (void) setRightNode: (id )aNode; +- (void) setParentNode: (id )aNode; +- binaryTree; +- (void) setBinaryTree: anObject; @end #define NODE_IS_RIGHTCHILD(NODE) (NODE == [[NODE parentNode] rightNode]) diff --git a/Headers/gnustep/base/BinaryTreeNode.h b/Headers/gnustep/base/BinaryTreeNode.h index 0dfab985c..a04708fee 100644 --- a/Headers/gnustep/base/BinaryTreeNode.h +++ b/Headers/gnustep/base/BinaryTreeNode.h @@ -1,8 +1,8 @@ /* Interface for Objective-C BinaryTreeNode object - Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1995, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum - Date: May 1993 + Created: May 1993 This file is part of the GNU Objective C Class Library. @@ -33,6 +33,7 @@ id _left; id _right; id _parent; + id _binary_tree; } @end diff --git a/Headers/gnustep/base/Collecting.h b/Headers/gnustep/base/Collecting.h index 1e726627f..0c723ddc7 100644 --- a/Headers/gnustep/base/Collecting.h +++ b/Headers/gnustep/base/Collecting.h @@ -1,8 +1,8 @@ /* Protocol for Objective-C objects that hold collections of elements. - Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1995, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum - Date: May 1993 + Created: May 1993 This file is part of the GNU Objective C Class Library. @@ -35,54 +35,61 @@ #define __Collecting_h_INCLUDE_GNU #include -#include -#include +#include +#include +#include @protocol ConstantCollecting // INITIALIZING; - init; +- initWithObjects: (id*)objc count: (unsigned)c; +- initWithObjects: firstObject, ...; +- initWithObjects: firstObject rest: (va_list)ap; - initWithContentsOf: (id )aCollection; -// TESTING; +// QUERYING COUNTS; - (BOOL) isEmpty; -- (BOOL) includesObject: anObject; +- (unsigned) count; +- (BOOL) containsObject: anObject; +- (unsigned) occurrencesOfObject: anObject; + +// COMPARISON WITH OTHER COLLECTIONS; - (BOOL) isSubsetOf: (id )aCollection; - (BOOL) isDisjointFrom: (id )aCollection; -- (int) compare: anObject; - (BOOL) isEqual: anObject; +- (int) compare: anObject; - (BOOL) contentsEqual: (id )aCollection; -- (unsigned) count; -- (unsigned) occurrencesOfObject: anObject; -- (BOOL) trueForAllObjectsByCalling: (BOOL(*)(id))aFunc; -- (BOOL) trueForAnyObjectsByCalling: (BOOL(*)(id))aFunc; -- detectObjectByCalling: (BOOL(*)(id))aFunc; -- detectObjectByCalling: (BOOL(*)(id))aFunc - ifNoneCall: (id(*)(arglist_t))excFunc; + +// PROPERTIES OF CONTENTS; +- (BOOL) trueForAllObjectsByInvoking: (id )anInvocation; +- (BOOL) trueForAnyObjectsByInvoking: (id )anInvocation; +- detectObjectByInvoking: (id )anInvocation; - maxObject; -- maxObjectByCalling: (int(*)(id,id))aFunc; - minObject; -- minObjectByCalling: (int(*)(id,id))aFunc; // ENUMERATING -- (void*) newEnumState; -- (BOOL) getNextObject:(id *)anObjectPtr withEnumState: (void**)enumState; -- freeEnumState: (void**)enumState; -- withObjectsCall: (void(*)(id))aFunc; -- withObjectsCall: (void(*)(id))aFunc whileTrue:(BOOL *)flag; -- injectObject: initialArgObject byCalling:(id(*)(id,id))aFunc; -- makeObjectsPerform: (SEL)aSel; -- makeObjectsPerform: (SEL)aSel with: argObject; +- (id ) objectEnumerator; +- (void) withObjectsInvoke: (id )anInvocation; +- (void) withObjectsInvoke: (id )anInvocation whileTrue:(BOOL *)flag; +- (void) makeObjectsPerform: (SEL)aSel; +- (void) makeObjectsPerform: (SEL)aSel withObject: argObject; // FILTERED ENUMERATING; -- withObjectsTrueByCalling: (BOOL(*)(id))testFunc - call: (void(*)(id))destFunc; -- withObjectsFalseByCalling: (BOOL(*)(id))testFunc - call: (void(*)(id))destFunc; -- withObjectsTransformedByCalling: (id(*)(id))transFunc - call: (void(*)(id))destFunc; +- (void) withObjectsTrueByInvoking: (id )testInvocation + invoke: (id )anInvocation; +- (void) withObjectsFalseByInvoking: (id )testInvocation + invoke: (id )anInvocation; +- (void) withObjectsTransformedByInvoking: (id )transInvocation + invoke: (id )anInvocation; -// COPYING +// LOW-LEVEL ENUMERATING; +- (void*) newEnumState; +- nextObjectWithEnumState: (void**)enumState; +- (void) freeEnumState: (void**)enumState; + +// COPYING; +- allocCopy; - emptyCopy; - emptyCopyAs: (Class)aCollectionClass; - shallowCopy; @@ -91,98 +98,34 @@ - copyAs: (Class)aCollectionClass; - species; -// NON-OBJECT ELEMENT METHOD NAMES; - -// INITIALIZING; -- initWithType:(const char *)contentEncoding; - -// TESTING; -- (BOOL) includesElement: (elt)anElement; -- (unsigned) occurrencesOfElement: (elt)anElement; -- (elt) detectElementByCalling: (BOOL(*)(elt))aFunc; -- (elt) detectElementByCalling: (BOOL(*)(elt))aFunc - ifNoneCall: (elt(*)(arglist_t))excFunc; -- (elt) maxElement; -- (elt) maxElementByCalling: (int(*)(elt,elt))aFunc; -- (elt) minElement; -- (elt) minElementByCalling: (int(*)(elt,elt))aFunc; -- (BOOL) trueForAllElementsByCalling: (BOOL(*)(elt))aFunc; -- (BOOL) trueForAnyElementsByCalling: (BOOL(*)(elt))aFunc; -- (const char *) contentType; -- (BOOL) contentsAreObjects; -- (int(*)(elt,elt)) comparisonFunction; - -// ENUMERATING; -- (BOOL) getNextElement:(elt *)anElementPtr withEnumState: (void**)enumState; -- withElementsCall: (void(*)(elt))aFunc; -- withElementsCall: (void(*)(elt))aFunc whileTrue: (BOOL*)flag; -- (elt) injectElement: (elt)initialElement byCalling: (elt(*)(elt,elt))aFunc; - -// FILTERED ENUMERATING; -- withElementsTrueByCalling: (BOOL(*)(elt))testFunc - call: (void(*)(elt))destFunc; -- withElementsFalseByCalling: (BOOL(*)(elt))testFunc - call: (void(*)(elt))destFunc; -- withElementsTransformedByCalling: (elt(*)(elt))transFunc - call: (void(*)(elt))destFunc; - @end @protocol Collecting // ADDING; -- addObject: newObject; -- addObjectIfAbsent: newObject; -- addContentsOf: (id )aCollection; -- addContentsOfIfAbsent: (id )aCollection; -- addObjectsCount: (unsigned)count, ...; +- (void) addObject: newObject; +- (void) addObjectIfAbsent: newObject; +- (void) addContentsOf: (id )aCollection; +- (void) addContentsIfAbsentOf: (id )aCollection; +- (void) addWithObjects: (id*)objc count: (unsigned)c; +- (void) addObjects: firstObject, ...; +- (void) addObjects: firstObject rest: (va_list)ap; // REMOVING; -- removeObject: oldObject; -- removeObject: oldObject ifAbsentCall: (id(*)(arglist_t))excFunc; -- removeAllOccurrencesOfObject: oldObject; -- removeContentsIn: (id )aCollection; -- removeContentsNotIn: (id )aCollection; -- uniqueContents; -- empty; +- (void) removeObject: oldObject; +- (void) removeAllOccurrencesOfObject: oldObject; +- (void) removeContentsIn: (id )aCollection; +- (void) removeContentsNotIn: (id )aCollection; +- (void) uniqueContents; +- (void) empty; // REPLACING; -- replaceObject: oldObject with: newObject; -- replaceObject: oldObject with: newObject - ifAbsentCall:(id(*)(arglist_t))excFunc; -- replaceAllOccurrencesOfObject: oldObject with: newObject; - -// ENUMERATING WHILE CHANGING CONTENTS; -- safeMakeObjectsPerform: (SEL)aSel; -- safeMakeObjectsPerform: (SEL)aSel with: argObject; -- safeWithObjectsCall: (void(*)(id))aFunc; -- safeWithObjectsCall: (void(*)(id))aFunc whileTrue:(BOOL *)flag; - - -// NON-OBJECT ELEMENT METHOD NAMES; - -// ADDING; -- addElement: (elt)newElement; -- addElementIfAbsent: (elt)newElement; -- addElementsCount: (unsigned)count, ...; - -// REMOVING; -- (elt) removeElement: (elt)oldElement; -- (elt) removeElement: (elt)oldElement - ifAbsentCall: (elt(*)(arglist_t))excFunc; -- removeAllOccurrencesOfElement: (elt)oldElement; - -// REPLACING; -- (elt) replaceElement: (elt)oldElement with: (elt)newElement; -- (elt) replaceElement: (elt)oldElement with: (elt)newElement - ifAbsentCall: (elt(*)(arglist_t))excFunc; -- replaceAllOccurrencesOfElement: (elt)oldElement with: (elt)newElement; - -// ENUMERATING WHILE CHANGING CONTENTS; -- safeWithElementsCall: (void(*)(elt))aFunc; -- safeWithElementsCall: (void(*)(elt))aFunc whileTrue: (BOOL*)flag; +- (void) replaceObject: oldObject withObject: newObject; +- (void) replaceAllOccurrencesOfObject: oldObject withObject: newObject; @end +#define NO_OBJECT nil + #endif /* __Collecting_h_INCLUDE_GNU */ diff --git a/Headers/gnustep/base/Collection.h b/Headers/gnustep/base/Collection.h index 8c6986991..774041c39 100644 --- a/Headers/gnustep/base/Collection.h +++ b/Headers/gnustep/base/Collection.h @@ -32,29 +32,42 @@ #include #include #include -#include #include -@interface Collection : NSObject -{ -} - -- (void) withObjectsInvoke: anInvocation; -- printElement: (elt)anElement; -- printForDebugger; - +@interface ConstantCollection : NSObject +- printForDebugger; /* This method will disappear later. */ @end -// #warning fix this macro -#define FOR_COLL(ACOLL, ELT) \ +@interface Collection : ConstantCollection +@end + +@interface Enumerator : NSObject +{ + id collection; + void *enum_state; +} +@end + +#define FOR_COLLECTION(ACOLL, ELT) \ { \ - void *_es = [ACOLL initEnumState]; \ - while ([ACOLL getNextElement:&(ELT) withEnumState:&_es]) \ + void *_es = [ACOLL newEnumState]; \ + while ((ELT = [ACOLL nextObjectWithEnumState: &_es])) \ { -#define FOR_COLL_END \ +#define END_FOR_COLLECTION(ACOLL) \ } \ - [ACOLL freeEnumState:_es]; \ + [ACOLL freeEnumState: &_es]; \ +} + +#define FOR_COLLECTION_WHILE_TRUE(ACOLL, ELT, FLAG) \ +{ \ + void *_es = [ACOLL newEnumState]; \ + while (FLAG && (ELT = [ACOLL nextObjectWithEnumState: &_es])) \ + { + +#define END_FOR_COLLECTION_WHILE_TRUE(ACOLL) \ + } \ + [ACOLL freeEnumState: &_es]; \ } /* The only subclassResponsibilities in Collection are: diff --git a/Headers/gnustep/base/CollectionPrivate.h b/Headers/gnustep/base/CollectionPrivate.h index 1b7183022..27e9bc611 100644 --- a/Headers/gnustep/base/CollectionPrivate.h +++ b/Headers/gnustep/base/CollectionPrivate.h @@ -1,8 +1,8 @@ /* Collection definitions for the use of subclass implementations only - Copyright (C) 1993,1994, 1995 Free Software Foundation, Inc. + Copyright (C) 1993,1994, 1995, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum - Date: May 1993 + Created: May 1993 This file is part of the GNU Objective C Class Library. @@ -25,28 +25,23 @@ #define __CollectionPrivate_h_INCLUDE_GNU #include -#include -@interface Collection (ArchivingHelpers) -/* These methods should never be called except in order inside - -write: and -read: */ -- _writeInit: (TypedStream*)aStream; -- _readInit: (TypedStream*)aStream; -- _writeContents: (TypedStream*)aStream; -- _readContents: (TypedStream*)aStream; - -/* The Coding versions of the above */ +@interface ConstantCollection (ArchivingHelpers) +/* These methods should never be called except in order, and inside + -encodeWithCoder: and -decodeWithCoder: */ - (void) _encodeCollectionWithCoder: (id )aCoder; - _initCollectionWithCoder: (id )aCoder; - (void) _encodeContentsWithCoder: (id )aCoder; - (void) _decodeContentsWithCoder: (id )aCoder; @end -@interface Collection (DeallocationHelpers) +@interface ConstantCollection (DeallocationHelpers) /* Empty the internals of a collection after the contents have already been released. */ -- _empty; +- (void) _collectionEmpty; + +- (void) _collectionReleaseContents; /* Deallocate the internals of a collection after the contents have already been released. */ @@ -54,63 +49,4 @@ @end - -/* To be used inside methods for getting the element comparison function. - This macro could be redefined when the comparison function is an - instance variable or is fixed. - I'm wondering if I should put _comparison_function back as an instance - variable in Collection. */ -#define COMPARISON_FUNCTION [self comparisonFunction] - -/* Use this for comparing elements in your implementation. */ -#define COMPARE_ELEMENTS(ELT1, ELT2) \ - ((*COMPARISON_FUNCTION)(ELT1, ELT2)) - -#define ELEMENTS_EQUAL(ELT1, ELT2) \ - (COMPARE_ELEMENTS(ELT1, ELT2) == 0) - -#define ENCODING_IS_OBJECT(ENCODING) \ - ((*(ENCODING) == _C_ID) || (*(ENCODING) == _C_CLASS)) - -/* To be used inside a method for determining if the contents are objects */ -#define CONTAINS_OBJECTS \ - (ENCODING_IS_OBJECT([self contentType])) - -/* Used inside a method for sending "-retain" if necessary */ -#define RETAIN_ELT(ELT) \ - if (CONTAINS_OBJECTS) [ELT.id_u retain] - -/* Used inside a method for sending "-release" if necessary */ -#define RELEASE_ELT(ELT) \ - if (CONTAINS_OBJECTS) [ELT.id_u release] - -/* Used inside a method for sending "-autorelease" if necessary */ -#define AUTORELEASE_ELT(ELT) \ - ((CONTAINS_OBJECTS) ? ((elt)[ELT.id_u autorelease]) : ELT) - - -/* Error Handling */ - -#define RETURN_BY_CALLING_EXCEPTION_FUNCTION(FUNC) \ -return (*FUNC)(__builtin_apply_args()) - - -/* To be used inside a method for making sure the contents are objects. - typeof(DEFAULT_ERROR_RETURN) must be the same type as the method - returns. */ -#define CHECK_CONTAINS_OBJECTS_ERROR() \ -({if (!(CONTAINS_OBJECTS)) \ -{ \ - [self error:"in %s, requires object contents", sel_get_name(_cmd)]; \ -}}) - -/* To be used inside a method whenever a particular element isn't found */ -#define ELEMENT_NOT_FOUND_ERROR(AN_ELEMENT) \ -([self error:"in %s, element not found.", sel_get_name(_cmd)]) - -/* To be used inside a method whenever there is no element matching the - needed criteria */ -#define NO_ELEMENT_FOUND_ERROR() \ -([self error:"in %s, no element found.", sel_get_name(_cmd)]) - #endif /* __CollectionPrivate_h_INCLUDE_GNU */ diff --git a/Headers/gnustep/base/Dictionary.h b/Headers/gnustep/base/Dictionary.h index 5bea6fb7f..9f6a67cfa 100644 --- a/Headers/gnustep/base/Dictionary.h +++ b/Headers/gnustep/base/Dictionary.h @@ -1,5 +1,5 @@ /* Interface for Objective-C Dictionary collection object - Copyright (C) 1993,1994, 1995 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1995, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum Date: May 1993 @@ -26,18 +26,14 @@ #include #include +#include @interface Dictionary : KeyedCollection { - coll_cache_ptr _contents_hash; // a hashtable to hold the contents; - int (*_comparison_function)(elt,elt); + NSMapTable *_contents_hash; } -- initWithType: (const char *)contentEncoding - keyType: (const char *)keyEncoding - capacity: (unsigned)aCapacity; -- initWithType: (const char *)contentEncoding - capacity: (unsigned)aCapacity; +- initWithCapacity: (unsigned)aCapacity; @end diff --git a/Headers/gnustep/base/Heap.h b/Headers/gnustep/base/Heap.h index d3592c4e3..44d427ec4 100644 --- a/Headers/gnustep/base/Heap.h +++ b/Headers/gnustep/base/Heap.h @@ -1,8 +1,8 @@ /* Interface for Objective-C Heap collection object - Copyright (C) 1993,1994 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum - Date: May 1993 + Created: May 1993 This file is part of the GNU Objective C Class Library. @@ -28,14 +28,9 @@ #include @interface Heap : Array -{ -} -- addElement: (elt)anElement; -- (elt) removeFirstElement; - -- heapifyFromIndex: (unsigned)index; -- heapify; +- (void) heapifyFromIndex: (unsigned)index; +- (void) heapify; @end diff --git a/Headers/gnustep/base/IndexedCollecting.h b/Headers/gnustep/base/IndexedCollecting.h index cb7bfc742..89ef892d4 100644 --- a/Headers/gnustep/base/IndexedCollecting.h +++ b/Headers/gnustep/base/IndexedCollecting.h @@ -1,5 +1,5 @@ /* Protocol for Objective-C objects that hold elements accessible by index - Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1995, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum Date: May 1993 @@ -36,7 +36,7 @@ #define __IndexedCollecting_h_OBJECTS_INCLUDE #include -#include +#include typedef struct _IndexRange { unsigned location; @@ -47,8 +47,8 @@ typedef struct _IndexRange { ({IndexRange __a=(RANGE1), __b=(RANGE2); \ __a.start<=__b.start && __a.end>=__b.end;}) - -@protocol ConstantIndexedCollecting + +@protocol ConstantIndexedCollecting // GETTING MEMBERS BY INDEX; - objectAtIndex: (unsigned)index; @@ -61,115 +61,53 @@ typedef struct _IndexRange { // GETTING INDICES BY MEMBER; - (unsigned) indexOfObject: anObject; -- (unsigned) indexOfObject: anObject - ifAbsentCall: (unsigned(*)(arglist_t))excFunc; - (unsigned) indexOfObject: anObject inRange: (IndexRange)aRange; -- (unsigned) indexOfObject: anObject inRange: (IndexRange)aRange - ifAbsentCall: (unsigned(*)(arglist_t))excFunc; // TESTING; -- (BOOL) includesIndex: (unsigned)index; - (BOOL) contentsEqualInOrder: (id )aColl; +- (int) compareInOrderContentsOf: (id )aCollection; - (unsigned) indexOfFirstDifference: (id )aColl; - (unsigned) indexOfFirstIn: (id )aColl; - (unsigned) indexOfFirstNotIn: (id )aColl; // ENUMERATING; -- (BOOL) getPrevObject: (id*)anIdPtr withEnumState: (void**)enumState; -- withObjectsInRange: (IndexRange)aRange call:(void(*)(id))aFunc; -- withObjectsInReverseCall: (void(*)(id))aFunc; -- withObjectsInReverseCall: (void(*)(id))aFunc whileTrue:(BOOL *)flag; +- (id ) reverseObjectEnumerator; +- (void) withObjectsInRange: (IndexRange)aRange + invoke: (id )anInvocation; +- (void) withObjectsInReverseInvoke: (id )anInvocation; +- (void) withObjectsInReverseInvoke: (id )anInvocation + whileTrue:(BOOL *)flag; +- (void) makeObjectsPerformInReverse: (SEL)aSel; +- (void) makeObjectsPerformInReverse: (SEL)aSel withObject: argObject; -// NON-OBJECT MESSAGE NAMES; - -// GETTING ELEMENTS BY INDEX; -- (elt) elementAtIndex: (unsigned)index; -- (elt) firstElement; -- (elt) lastElement; - -// GETTING MEMBERS BY NEIGHBOR; -- (elt) successorOfElement: (elt)anElement; -- (elt) predecessorOfElement: (elt)anElement; - -// GETTING INDICES BY MEMBER; -- (unsigned) indexOfElement: (elt)anElement; -- (unsigned) indexOfElement: (elt)anElement - ifAbsentCall: (unsigned(*)(arglist_t))excFunc; -- (unsigned) indexOfElement: (elt)anElement inRange: (IndexRange)aRange; -- (unsigned) indexOfElement: (elt)anElement inRange: (IndexRange)aRange - ifAbsentCall: (unsigned(*)(arglist_t))excFunc; - -// ENUMERATING; -- (BOOL) getPrevElement:(elt*)anElementPtr withEnumState: (void**)enumState; -- withElementsInRange: (IndexRange)aRange call:(void(*)(elt))aFunc; -- withElementsInReverseCall: (void(*)(elt))aFunc; -- withElementsInReverseCall: (void(*)(elt))aFunc whileTrue:(BOOL *)flag; +// LOW-LEVEL ENUMERATING; +- prevObjectWithEnumState: (void**)enumState; @end -@protocol IndexedCollecting + +@protocol IndexedCollecting -// ADDING; -- insertObject: newObject atIndex: (unsigned)index; -- insertObject: newObject before: oldObject; -- insertObject: newObject after: oldObject; -- insertContentsOf: (id )aCollection - atIndex: (unsigned)index; -- appendObject: newObject; -- prependObject: newObject; -- appendContentsOf: (id )aCollection; -- prependContentsOf: (id )aCollection; +// REPLACING; +- (void) replaceObjectAtIndex: (unsigned)index with: newObject; -// REPLACING AND SWAPPING -- replaceObjectAtIndex: (unsigned)index with: newObject; -- replaceRange: (IndexRange)aRange with: (id )aCollection; -- replaceRange: (IndexRange)aRange using: (id )aCollection; -- swapAtIndeces: (unsigned)index1 : (unsigned)index2; - -// REMOVING -- removeObjectAtIndex: (unsigned)index; -- removeFirstObject; -- removeLastObject; -- removeRange: (IndexRange)aRange; - -// ENUMERATING WHILE CHANGING CONTENTS; -- safeWithObjectsInReverseCall: (void(*)(id))aFunc; -- safeWithObjectsInReverseCall: (void(*)(id))aFunc whileTrue:(BOOL *)flag; +// REMOVING; +- (void) removeObjectAtIndex: (unsigned)index; +- (void) removeFirstObject; +- (void) removeLastObject; +- (void) removeRange: (IndexRange)aRange; // SORTING; -- sortContents; -- sortObjectsByCalling: (int(*)(id,id))aFunc; -- sortAddObject: newObject; -- sortAddObject: newObject byCalling: (int(*)(id,id))aFunc; - - -// NON-OBJECT MESSAGE NAMES; - -// ADDING; -- appendElement: (elt)newElement; -- prependElement: (elt)newElement; -- insertElement: (elt)newElement atIndex: (unsigned)index; -- insertElement: (elt)newElement before: (elt)oldElement; -- insertElement: (elt)newElement after: (elt)oldElement; - -// REMOVING AND REPLACING; -- (elt) removeElementAtIndex: (unsigned)index; -- (elt) removeFirstElement; -- (elt) removeLastElement; -- (elt) replaceElementAtIndex: (unsigned)index with: (elt)newElement; - -// ENUMERATING WHILE CHANGING CONTENTS; -- safeWithElementsInRange: (IndexRange)aRange call:(void(*)(elt))aFunc; -- safeWithElementsInReverseCall: (void(*)(elt))aFunc; -- safeWithElementsInReverseCall: (void(*)(elt))aFunc whileTrue:(BOOL *)flag; - -// SORTING; -- sortElementsByCalling: (int(*)(elt,elt))aFunc; -- sortAddElement: (elt)newElement; -- sortAddElement: (elt)newElement byCalling: (int(*)(elt,elt))aFunc; +- (void) sortContents; +- (void) sortAddObject: newObject; @end +#include +#define NO_INDEX UINT_MAX + +/* xxx Fix this comment: */ + /* Most methods in the KeyedCollecting protocol that mention a key are duplicated in the IndexedCollecting protocol, with their names modified to reflect that the "key" now must be an unsigned integer, diff --git a/Headers/gnustep/base/IndexedCollection.h b/Headers/gnustep/base/IndexedCollection.h index 6d7337c0c..c5df4e78b 100644 --- a/Headers/gnustep/base/IndexedCollection.h +++ b/Headers/gnustep/base/IndexedCollection.h @@ -1,5 +1,5 @@ /* Interface for Objective-C Sequential Collection object. - Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1995, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum Date: May 1993 @@ -28,12 +28,54 @@ #include #include -@interface IndexedCollection : KeyedCollection +@interface ConstantIndexedCollection : ConstantCollection +@end + +@interface IndexedCollection : ConstantIndexedCollection +@end + +@interface ReverseEnumerator : Enumerator @end /* Put this on category instead of class to avoid bogus complaint from gcc */ -@interface IndexedCollection (IndexedCollectionProtocol) +@interface ConstantIndexedCollection (Protocol) @end +@interface IndexedCollection (Protocol) +@end + +#define FOR_INDEXED_COLLECTION(ACOLL, ELT) \ +{ \ + void *_es = [ACOLL newEnumState]; \ + while ((ELT = [ACOLL nextObjectWithEnumState: &_es])) \ + { + +#define END_FOR_INDEXED_COLLECTION(ACOLL) \ + } \ + [ACOLL freeEnumState: &_es]; \ +} + +#define FOR_INDEXED_COLLECTION_REVERSE(ACOLL, ELT) \ +{ \ + void *_es = [ACOLL newEnumState]; \ + while ((ELT = [ACOLL prevObjectWithEnumState: &_es])) \ + { + +#define END_FOR_INDEXED_COLLECTION_REVERSE(ACOLL) \ + } \ + [ACOLL freeEnumState: &_es]; \ +} + +#define FOR_INDEXED_COLLECTION_WHILE_TRUE(ACOLL, ELT, FLAG) \ +{ \ + void *_es = [ACOLL newEnumState]; \ + while (FLAG && (ELT = [ACOLL nextObjectWithEnumState: &_es])) \ + { + +#define END_FOR_INDEXED_COLLECTION_WHILE_TRUE(ACOLL) \ + } \ + [ACOLL freeEnumState: &_es]; \ +} + /* The only subclassResponsibilities in IndexedCollection are: diff --git a/Headers/gnustep/base/KeyedCollecting.h b/Headers/gnustep/base/KeyedCollecting.h index 6f8a51cf6..f50dbfe2c 100644 --- a/Headers/gnustep/base/KeyedCollecting.h +++ b/Headers/gnustep/base/KeyedCollecting.h @@ -1,5 +1,5 @@ /* Protocol for Objective-C objects holding (keyElement,contentElement) pairs. - Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1995, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum Date: May 1993 @@ -38,84 +38,44 @@ @protocol ConstantKeyedCollecting +// INITIALIZING; +- initWithObjects: (id*)objects forKeys: (id*)keys count: (unsigned)c; + // GETTING ELEMENTS AND KEYS; -- objectAtKey: (elt)aKey; -- keyObjectOfObject: aContentObject; +- objectAtKey: aKey; +- keyOfObject: aContentObject; // TESTING; -- (BOOL) includesKey: (elt)aKey; +- (BOOL) containsKey: aKey; // ENUMERATIONS; -- withKeyObjectsCall: (void(*)(id))aFunc; -- withKeyObjectsAndContentObjectsCall: (void(*)(id,id))aFunc; -- withKeyObjectsAndContentObjectsCall: (void(*)(id,id))aFunc +- (id ) keyEnumerator; +- withKeyObjectsInvoke: (id )anInvocation; +- withKeyObjectsInvoke: (id )anInvocation whileTrue: (BOOL *)flag; -// NON-OBJECT ELEMENT METHOD NAMES; +// LOW-LEVEL ENUMERATING; +- nextObjectAndKey: (id*)keyPtr withEnumState: (void**)enumState; -// INITIALIZING; -- initWithType: (const char *)contentsEncoding - keyType: (const char *)keyEncoding; -- initKeyType: (const char *)keyEncoding; - -// GETTING ELEMENTS AND KEYS; -- (elt) elementAtKey: (elt)aKey; -- (elt) elementAtKey: (elt)aKey ifAbsentCall: (elt(*)(arglist_t))excFunc; -- (elt) keyElementOfElement: (elt)aContentObject; -- (elt) keyElementOfElement: (elt)aContentObject - ifAbsentCall: (elt(*)(arglist_t))excFunc; - -// TESTING; -- (const char *) keyType; - -// ENUMERATING; -- (BOOL) getNextKey: (elt*)aKeyPtr content: (elt*)anElementPtr - withEnumState: (void**)enumState; -- withKeyElementsCall: (void(*)(elt))aFunc; -- withKeyElementsAndContentElementsCall: (void(*)(elt,elt))aFunc; -- withKeyElementsAndContentElementsCall: (void(*)(elt,elt))aFunc - whileTrue: (BOOL *)flag; +// COPYING; +- shallowCopyValuesAs: (Class)aCollectingClass; +- shallowCopyKeysAs: (Class)aCollectingClass; +- copyValuesAs: (Class)aCollectingClass; +- copyKeysAs: (Class)aCollectingClass; @end @protocol KeyedCollecting // ADDING; -- putObject: newContentObject atKey: (elt)aKey; +- (void) putObject: newContentObject atKey: aKey; // REPLACING AND SWAPPING; -- replaceObjectAtKey: (elt)aKey with: newContentObject; -- swapAtKeys: (elt)key1 : (elt)key2; +- (void) replaceObjectAtKey: aKey with: newContentObject; +- (void) swapObjectsAtKeys: key1 : key2; // REMOVING; -- removeObjectAtKey: (elt)aKey; - -// ENUMERATING WHILE CHANGING CONTENTS; -- safeWithKeyObjectsCall: (void(*)(id))aFunc; -- safeWithKeyObjectsAndContentObjectsCall: (void(*)(id,id))aFunc; -- safeWithKeyObjectsAndContentObjectsCall: (void(*)(id,id))aFunc - whileTrue: (BOOL *)flag; - - -// NON-OBJECT ELEMENT METHOD NAMES; - -// ADDING; -- putElement: (elt)newContentElement atKey: (elt)aKey; - -// REPLACING; -- (elt) replaceElementAtKey: (elt)aKey with: (elt)newContentElement; -- (elt) replaceElementAtKey: (elt)aKey with: (elt)newContentElement - ifAbsentCall: (elt(*)(arglist_t))excFunc; - -// REMOVING; -- (elt) removeElementAtKey: (elt)aKey; -- (elt) removeElementAtKey: (elt)aKey ifAbsentCall: (elt(*)(arglist_t))excFunc; - -// ENUMERATING WHILE CHANGING CONTENTS; -- safeWithKeyElementsCall: (void(*)(elt))aFunc; -- safeWithKeyElementsAndContentElementsCall: (void(*)(elt,elt))aFunc; -- safeWithKeyElementsAndContentElementsCall: (void(*)(elt,elt))aFunc - whileTrue: (BOOL *)flag; +- (void) removeObjectAtKey: aKey; @end diff --git a/Headers/gnustep/base/KeyedCollection.h b/Headers/gnustep/base/KeyedCollection.h index 5f1af6558..a9d6d32ba 100644 --- a/Headers/gnustep/base/KeyedCollection.h +++ b/Headers/gnustep/base/KeyedCollection.h @@ -1,5 +1,5 @@ /* Interface for Objective-C KeyedCollection collection object - Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1995, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum Date: May 1993 @@ -28,12 +28,44 @@ #include #include -@interface KeyedCollection : Collection +@interface ConstantKeyedCollection : Collection +@end + +@interface KeyedCollection : ConstantKeyedCollection @end /* Put this on category instead of class to avoid bogus complaint from gcc */ -@interface KeyedCollection (KeyedCollectingProtocol) +@interface ConstantKeyedCollection (Protocol) @end +@interface KeyedCollection (Protocol) +@end + +@interface KeyEnumerator : Enumerator +@end + +#define FOR_KEYED_COLLECTION(ACOLL, ELT, KEY) \ +{ \ + void *_es = [ACOLL newEnumState]; \ + while ((ELT = [ACOLL nextObjectAndKey: &(KEY) withEnumState: &_es])) \ + { + +#define END_FOR_KEYED_COLLECTION(ACOLL) \ + } \ + [ACOLL freeEnumState: &_es]; \ +} + +#define FOR_KEYED_COLLECTION_WHILE_TRUE(ACOLL, ELT, KEY, FLAG) \ +{ \ + void *_es = [ACOLL newEnumState]; \ + while (FLAG && (ELT = [ACOLL nextObjectAndKey: &(KEY) \ + withEnumState: &_es])) \ + { + +#define END_FOR_KEYED_COLLECTION_WHILE_TRUE(ACOLL) \ + } \ + [ACOLL freeEnumState: &_es]; \ +} + /* The only subclassResponsibilities in IndexedCollection are: diff --git a/Headers/gnustep/base/LinkedList.h b/Headers/gnustep/base/LinkedList.h index b6a2f7e20..198a21b36 100644 --- a/Headers/gnustep/base/LinkedList.h +++ b/Headers/gnustep/base/LinkedList.h @@ -1,8 +1,8 @@ /* Interface for Objective-C LinkedList collection object - Copyright (C) 1993,1994 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum - Date: May 1993 + Created: May 1993 This file is part of the GNU Objective C Class Library. @@ -25,7 +25,7 @@ #define __LinkedList_h_INCLUDE_GNU #include -#include +#include /* The protocol defines the interface to an object that may be an element in a LinkedList. @@ -33,13 +33,16 @@ @protocol LinkedListComprising - nextLink; - prevLink; -- setNextLink: (id )aLink; -- setPrevLink: (id )aLink; +- (void) setNextLink: (id )aLink; +- (void) setPrevLink: (id )aLink; +- linkedList; +- (void) setLinkedList: aLinkedList; @end -@interface LinkedList : IndexedCollection +@interface LinkedList : OrderedCollection { id _first_link; + id _last_link; unsigned int _count; } diff --git a/Headers/gnustep/base/LinkedListNode.h b/Headers/gnustep/base/LinkedListNode.h index 09c323b7e..c2b9af08a 100644 --- a/Headers/gnustep/base/LinkedListNode.h +++ b/Headers/gnustep/base/LinkedListNode.h @@ -1,8 +1,8 @@ /* Interface for Objective-C LinkedListNode object - Copyright (C) 1993,1994, 1995 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1995, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum - Date: May 1993 + Created: May 1993 This file is part of the GNU Objective C Class Library. @@ -32,8 +32,8 @@ { id _next; id _prev; + id _linked_list; } @end #endif /* __LinkedListNode_h_INCLUDE_GNU */ - diff --git a/Headers/gnustep/base/MappedCollector.h b/Headers/gnustep/base/MappedCollector.h index 6856d6eb3..1c7420aac 100644 --- a/Headers/gnustep/base/MappedCollector.h +++ b/Headers/gnustep/base/MappedCollector.h @@ -1,5 +1,5 @@ /* Interface for Objective-C MappedCollector collection object - Copyright (C) 1993,1994 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum Date: May 1993 @@ -33,7 +33,7 @@ id _domain; } -- initCollection: (id )aDomain +- initWithCollection: (id )aDomain map: (id )aMap; @end diff --git a/Headers/gnustep/base/Queue.h b/Headers/gnustep/base/Queue.h index f845ef99b..2ef5a868b 100644 --- a/Headers/gnustep/base/Queue.h +++ b/Headers/gnustep/base/Queue.h @@ -1,8 +1,8 @@ /* Interface for Objective-C Queue object - Copyright (C) 1993,1994 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum - Date: May 1993 + Created: May 1993 This file is part of the GNU Objective C Class Library. @@ -29,13 +29,9 @@ @interface Queue : CircularArray -- enqueueObject: newObject; +- (void) enqueueObject: newObject; - dequeueObject; -// NON-OBJECT MESSAGE NAMES; -- enqueueElement: (elt)newElement; -- (elt) dequeueElement; - @end #endif /* __Queue_h_INCLUDE_GNU */ diff --git a/Headers/gnustep/base/Set.h b/Headers/gnustep/base/Set.h index 5e7278cb0..b433d5936 100644 --- a/Headers/gnustep/base/Set.h +++ b/Headers/gnustep/base/Set.h @@ -1,5 +1,5 @@ /* Interface for Objective-C Set collection object - Copyright (C) 1993,1994 Free Software Foundation, Inc. + Copyright (C) 1993,1994, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum Date: May 1993 @@ -26,24 +26,23 @@ #include #include +#include @interface Set : Collection { - coll_cache_ptr _contents_hash; // a hashtable to hold the contents; + NSHashTable *_contents_hash; // a hashtable to hold the contents; } // MANAGING CAPACITY; + (unsigned) defaultCapacity; // INITIALIZING AND FREEING; -- initWithType: (const char *)contentEncoding - capacity: (unsigned)aCapacity; - initWithCapacity: (unsigned)aCapacity; // SET OPERATIONS; -- intersectWithCollection: (id )aCollection; -- unionWithCollection: (id )aCollection; -- differenceWithCollection: (id )aCollection; +- (void) intersectWithCollection: (id )aCollection; +- (void) unionWithCollection: (id )aCollection; +- (void) differenceWithCollection: (id )aCollection; - shallowCopyIntersectWithCollection: (id )aCollection; - shallowCopyUnionWithCollection: (id )aCollection; diff --git a/Headers/gnustep/base/SplayTree.h b/Headers/gnustep/base/SplayTree.h index 73e3edfcf..779afb1af 100644 --- a/Headers/gnustep/base/SplayTree.h +++ b/Headers/gnustep/base/SplayTree.h @@ -1,5 +1,5 @@ /* Interface for Objective-C SplayTree collection object - Copyright (C) 1993,1994 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum Date: May 1993 @@ -41,7 +41,7 @@ { } -- splayNode: aNode; +- (void) splayNode: aNode; @end diff --git a/Headers/gnustep/base/Stack.h b/Headers/gnustep/base/Stack.h index bfd7d5d02..2932f9a48 100644 --- a/Headers/gnustep/base/Stack.h +++ b/Headers/gnustep/base/Stack.h @@ -1,8 +1,8 @@ /* Interface for Objective-C Stack object - Copyright (C) 1993,1994 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum - Date: May 1993 + Created: May 1993 This file is part of the GNU Objective C Class Library. @@ -29,17 +29,11 @@ @interface Stack : Array -- pushObject: anObject; +- (void) pushObject: anObject; - popObject; - topObject; -- duplicateTop; -- exchangeTop; - -// NON-OBJECT MESSAGE NAMES; -- pushElement: (elt)anElement; -- (elt) popElement; -- (elt) topElement; - +- (void) duplicateTop; +- (void) exchangeTop; @end diff --git a/Source/BinaryTree.m b/Source/BinaryTree.m index 2df066aca..6ef813f37 100644 --- a/Source/BinaryTree.m +++ b/Source/BinaryTree.m @@ -1,5 +1,5 @@ /* Implementation for Objective-C BinaryTree collection object - Copyright (C) 1993,1994, 1995, 1996 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1995, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum Date: May 1993 @@ -24,11 +24,9 @@ #include #include #include +#include -// do safety checks; -#define SAFE_BinaryTree 1 - -/* sentinal */ +/* the sentinal */ static id nilBinaryTreeNode; @implementation BinaryTree @@ -37,7 +35,6 @@ static id nilBinaryTreeNode; { if (self == [BinaryTree class]) { - [self setVersion:0]; /* beta release */ nilBinaryTreeNode = [[BinaryTreeNode alloc] init]; } } @@ -45,7 +42,6 @@ static id nilBinaryTreeNode; /* This is the designated initializer of this class */ - init { - [super initWithType:@encode(id)]; _count = 0; _contents_root = [self nilNode]; return self; @@ -80,39 +76,6 @@ static id nilBinaryTreeNode; [aCoder finishDecodingInterconnectedObjects]; } -- _readInit: (TypedStream*)aStream -{ - [super _readInit:aStream]; - _count = 0; - _contents_root = [self nilNode]; - return self; -} - -- _writeContents: (TypedStream*)aStream -{ - void archiveElement(elt e) - { - objc_write_object(aStream, e.id_u); - } - objc_write_type(aStream, @encode(unsigned int), &_count); - [self withElementsCall:archiveElement]; - // We rely on the nodes to archive their children and parent ptrs; - objc_write_object_reference(aStream, _contents_root); - return self; -} - -- _readContents: (TypedStream*)aStream -{ - int i; - - objc_read_type(aStream, @encode(unsigned int), &_count); - for (i = 0; i < _count; i++) - objc_read_object(aStream, &_contents_root); - // We rely on the nodes to have archived their children and parent ptrs; - objc_read_object(aStream, &_contents_root); - return self; -} - /* Empty copy must empty an allocCopy'ed version of self */ - emptyCopy { @@ -123,20 +86,10 @@ static id nilBinaryTreeNode; } /* This must work without sending any messages to content objects */ -- _empty +- (void) _empty { _count = 0; _contents_root = [self nilNode]; - return self; -} - -/* Override the designated initializer for our superclass IndexedCollection - to make sure we have object contents */ -- initWithType: (const char *)contentEncoding -{ - if (!ENCODING_IS_OBJECT(contentEncoding)) - [self error:"BinaryTree contents must be objects."]; - return [self init]; } - nilNode @@ -173,64 +126,72 @@ static id nilBinaryTreeNode; return aNode; } -- (elt) firstElement +- firstObject { - return [self leftmostNodeFromNode:_contents_root]; + return [self leftmostNodeFromNode: _contents_root]; } -- (elt) lastElement +- lastObject { - return [self rightmostNodeFromNode:_contents_root]; + return [self rightmostNodeFromNode: _contents_root]; +} + +/* This is correct only if the tree is sorted. How to deal with this? */ +- maxObject +{ + return [self rightmostNodeFromNode: _contents_root]; } /* This is correct only is the tree is sorted. How to deal with this? */ -- (elt) maxElement +- minObject { - return [self rightmostNodeFromNode:_contents_root]; + return [self leftmostNodeFromNode: _contents_root]; } -/* This is correct only is the tree is sorted. How to deal with this? */ -- (elt) minElement -{ - return [self leftmostNodeFromNode:_contents_root]; -} - -// returns [self nilNode] is there is no successor; -- (elt) successorOfElement: (elt)anElement +- successorOfObject: anObject { id tmp; + /* Make sure we actually own the anObject. */ + assert ([anObject binaryTree] == self); + // here tmp is the right node; - if ((tmp = [anElement.id_u rightNode]) != [self nilNode]) - return [self leftmostNodeFromNode:tmp]; + if ((tmp = [anObject rightNode]) != [self nilNode]) + return [self leftmostNodeFromNode: tmp]; // here tmp is the parent; - tmp = [anElement.id_u parentNode]; - while (tmp != [self nilNode] && anElement.id_u == [tmp rightNode]) + tmp = [anObject parentNode]; + while (tmp != [self nilNode] && anObject == [tmp rightNode]) { - anElement.id_u = tmp; + anObject = tmp; tmp = [tmp parentNode]; } + if (tmp == [self nilNode]) + return NO_OBJECT; return tmp; } // I should make sure that [_contents_root parentNode] == [self nilNode]; // Perhaps I should make [_contents_root parentNode] == binaryTreeObj ??; -// returns [self nilNode] is there is no predecessor; -- (elt) predecessorElement: (elt)anElement +- predecessorObject: anObject { id tmp; + /* Make sure we actually own the anObject. */ + assert ([anObject binaryTree] == self); + // here tmp is the left node; - if ((tmp = [anElement.id_u leftNode]) != [self nilNode]) + if ((tmp = [anObject leftNode]) != [self nilNode]) return [self rightmostNodeFromNode:tmp]; // here tmp is the parent; - tmp = [anElement.id_u parentNode]; - while (tmp != [self nilNode] && anElement.id_u == [tmp leftNode]) + tmp = [anObject parentNode]; + while (tmp != [self nilNode] && anObject == [tmp leftNode]) { - anElement.id_u = tmp; + anObject = tmp; tmp = [tmp parentNode]; } + if (tmp == [self nilNode]) + return NO_OBJECT; return tmp; } @@ -238,6 +199,10 @@ static id nilBinaryTreeNode; - rootFromNode: aNode { id parentNode; + + /* Make sure we actually own the aNode. */ + assert ([aNode binaryTree] == self); + while ((parentNode = [aNode parentNode]) != [self nilNode]) aNode = parentNode; return aNode; @@ -248,6 +213,9 @@ static id nilBinaryTreeNode; { unsigned count = 0; + /* Make sure we actually own the aNode. */ + assert ([aNode binaryTree] == self); + if (aNode == nil || aNode == [self nilNode]) [self error:"in %s, Can't find depth of nil node", sel_get_name(_cmd)]; do @@ -264,6 +232,9 @@ static id nilBinaryTreeNode; unsigned leftHeight, rightHeight; id tmpNode; + /* Make sure we actually own the aNode. */ + assert ([aNode binaryTree] == self); + if (aNode == nil || aNode == [self nilNode]) { [self error:"in %s, Can't find height of nil node", sel_get_name(_cmd)]; @@ -288,6 +259,10 @@ static id nilBinaryTreeNode; - (unsigned) nodeCountUnderNode: aNode { unsigned count = 0; + + /* Make sure we actually own the aNode. */ + assert ([aNode binaryTree] == self); + if ([aNode leftNode] != [self nilNode]) count += 1 + [self nodeCountUnderNode:[aNode leftNode]]; if ([aNode rightNode] != [self nilNode]) @@ -299,6 +274,9 @@ static id nilBinaryTreeNode; { id y; + /* Make sure we actually own the aNode. */ + assert ([aNode binaryTree] == self); + y = [aNode rightNode]; if (y == [self nilNode]) return self; @@ -324,6 +302,9 @@ static id nilBinaryTreeNode; { id y; + /* Make sure we actually own the aNode. */ + assert ([aNode binaryTree] == self); + y = [aNode leftNode]; if (y == [self nilNode]) return self; @@ -345,156 +326,73 @@ static id nilBinaryTreeNode; return self; } -- (elt) elementAtIndex: (unsigned)index +- objectAtIndex: (unsigned)index { - elt ret; + id ret; CHECK_INDEX_RANGE_ERROR(index, _count); - ret = [self firstElement]; + ret = [self firstObject]; // Not very efficient; Should be rewritten; while (index--) - ret = [self successorOfElement:ret]; + ret = [self successorOfObject: ret]; return ret; } -- sortAddElement: (elt)newElement byCalling: (int(*)(elt,elt))aFunc +- (void) sortAddObject: newObject { id theParent, tmpChild; - [newElement.id_u setLeftNode:[self nilNode]]; - [newElement.id_u setRightNode:[self nilNode]]; + /* Make sure no one else already owns the newObject. */ + assert ([newObject binaryTree] == NO_OBJECT); + + /* Claim ownership of the newObject. */ + [newObject retain]; + [newObject setBinaryTree: self]; + + [newObject setLeftNode:[self nilNode]]; + [newObject setRightNode:[self nilNode]]; theParent = [self nilNode]; tmpChild = _contents_root; while (tmpChild != [self nilNode]) { theParent = tmpChild; - if ((*aFunc)(newElement,theParent) < 0) + if ([newObject compare: theParent] < 0) tmpChild = [tmpChild leftNode]; else tmpChild = [tmpChild rightNode]; } - [newElement.id_u setParentNode:theParent]; + [newObject setParentNode:theParent]; if (theParent == [self nilNode]) - _contents_root = newElement.id_u; + _contents_root = newObject; else { - if (COMPARE_ELEMENTS(newElement, theParent) < 0) - [theParent setLeftNode:newElement.id_u]; + if ([newObject compare: theParent] < 0) + [theParent setLeftNode:newObject]; else - [theParent setRightNode:newElement.id_u]; + [theParent setRightNode:newObject]; } _count++; - RETAIN_ELT(newElement); - return self; } -- addElement: (elt)newElement +- (void) addObject: newObject { - // By default insert in sorted order. Is this what we want?; - [self sortAddElement:newElement]; - return self; + // By default insert in sorted order. + [self sortAddObject: newObject]; } -// NOTE: This gives you the power to put elements in unsorted order; -- insertElement: (elt)newElement before: (elt)oldElement -{ - id tmp; - - #if SAFE_BinaryTree - if ([self rootFromNode:oldElement.id_u] != _contents_root) - [self error:"in %s, oldElement not in tree!!", sel_get_name(_cmd)]; - #endif - - [newElement.id_u setRightNode:[self nilNode]]; - [newElement.id_u setLeftNode:[self nilNode]]; - if ((tmp = [oldElement.id_u leftNode]) != [self nilNode]) - { - [(tmp = [self rightmostNodeFromNode:tmp]) setRightNode:newElement.id_u]; - [newElement.id_u setParentNode:tmp]; - } - else if (newElement.id_u != [self nilNode]) - { - [oldElement.id_u setLeftNode:newElement.id_u]; - [newElement.id_u setParentNode:oldElement.id_u]; - } - else - { - _contents_root = newElement.id_u; - [newElement.id_u setParentNode:[self nilNode]]; - } - _count++; - RETAIN_ELT(newElement); - return self; -} - -// NOTE: This gives you the power to put elements in unsorted order; -- insertElement: (elt)newElement after: (elt)oldElement -{ - id tmp; - - #if SAFE_BinaryTree - if ([self rootFromNode:oldElement.id_u] != _contents_root) - [self error:"in %s, !!!!!!!!", sel_get_name(_cmd)]; - #endif - - [newElement.id_u setRightNode:[self nilNode]]; - [newElement.id_u setLeftNode:[self nilNode]]; - if ((tmp = [oldElement.id_u rightNode]) != [self nilNode]) - { - [(tmp = [self leftmostNodeFromNode:tmp]) setLeftNode:newElement.id_u]; - [newElement.id_u setParentNode:tmp]; - } - else if (newElement.id_u != [self nilNode]) - { - [oldElement.id_u setRightNode:newElement.id_u]; - [newElement.id_u setParentNode:oldElement.id_u]; - } - else - { - _contents_root = newElement.id_u; - [newElement.id_u setParentNode:[self nilNode]]; - } - _count++; - RETAIN_ELT(newElement); - return self; -} - -// NOTE: This gives you the power to put elements in unsorted order; -- insertElement: (elt)newElement atIndex: (unsigned)index -{ - CHECK_INDEX_RANGE_ERROR(index, _count+1); - if (index == _count) - [self appendElement:newElement]; - else - [self insertElement:newElement before:[self elementAtIndex:index]]; - return self; -} - -// NOTE: This gives you the power to put elements in unsorted order; -- appendElement: (elt)newElement -{ - if (_count == 0) - { - _contents_root = newElement.id_u; - _count = 1; - [newElement.id_u setLeftNode:[self nilNode]]; - [newElement.id_u setRightNode:[self nilNode]]; - [newElement.id_u setParentNode:[self nilNode]]; - } - else - [self insertElement:newElement after:[self lastElement]]; - return self; -} - -- (elt) removeElement: (elt)oldElement +- (void) removeObject: oldObject { id x, y; - if ([oldElement.id_u leftNode] == [self nilNode] - || [oldElement.id_u rightNode] == [self nilNode]) - y = oldElement.id_u; + /* Make sure we actually own the aNode. */ + assert ([oldObject binaryTree] == self); + + /* Extract the oldObject and sew up the cut. */ + if ([oldObject leftNode] == [self nilNode] + || [oldObject rightNode] == [self nilNode]) + y = oldObject; else - y = [self successorOfElement:oldElement].id_u; + y = [self successorOfObject: oldObject]; if ([y leftNode] != [self nilNode]) x = [y leftNode]; @@ -502,88 +400,66 @@ static id nilBinaryTreeNode; x = [y rightNode]; if (x != [self nilNode]) - [x setParentNode:[y parentNode]]; + [x setParentNode: [y parentNode]]; if ([y parentNode] == [self nilNode]) _contents_root = x; else { if (y == [[y parentNode] leftNode]) - [[y parentNode] setLeftNode:x]; + [[y parentNode] setLeftNode: x]; else - [[y parentNode] setRightNode:x]; + [[y parentNode] setRightNode: x]; } - if (y != oldElement.id_u) + if (y != oldObject) { - /* put y in the place of oldElement.id_u */ - [y setParentNode:[oldElement.id_u parentNode]]; - [y setLeftNode:[oldElement.id_u leftNode]]; - [y setRightNode:[oldElement.id_u rightNode]]; - if (oldElement.id_u == [[oldElement.id_u parentNode] leftNode]) - [[oldElement.id_u parentNode] setLeftNode:y]; + /* put y in the place of oldObject */ + [y setParentNode: [oldObject parentNode]]; + [y setLeftNode: [oldObject leftNode]]; + [y setRightNode: [oldObject rightNode]]; + if (oldObject == [[oldObject parentNode] leftNode]) + [[oldObject parentNode] setLeftNode: y]; else - [[oldElement.id_u parentNode] setRightNode:y]; - [[oldElement.id_u leftNode] setParentNode:y]; - [[oldElement.id_u rightNode] setParentNode:y]; + [[oldObject parentNode] setRightNode: y]; + [[oldObject leftNode] setParentNode: y]; + [[oldObject rightNode] setParentNode: y]; } - [oldElement.id_u setRightNode:[self nilNode]]; - [oldElement.id_u setLeftNode:[self nilNode]]; - [oldElement.id_u setParentNode:[self nilNode]]; _count--; - return AUTORELEASE_ELT(oldElement); + + /* Release ownership of the object. */ +#if 0 + [oldObject setRightNode: [self nilNode]]; + [oldObject setLeftNode: [self nilNode]]; + [oldObject setParentNode: [self nilNode]]; +#else + [oldObject setLeftNode: NO_OBJECT]; + [oldObject setRightNode: NO_OBJECT]; + [oldObject setParentNode: NO_OBJECT]; +#endif + [oldObject setBinaryTree: NO_OBJECT]; + [oldObject release]; } -- withElementsCall: (void(*)(elt))aFunc whileTrue: (BOOL*)flag -{ - void traverse(id aNode) - { - if (!(*flag) || aNode == [self nilNode] || !aNode) - return; - traverse([aNode leftNode]); - (*aFunc)(aNode); - traverse([aNode rightNode]); - } - traverse(_contents_root); - return self; -} + +// ENUMERATING; -- withElementsInReverseCall: (void(*)(elt))aFunc whileTrue: (BOOL*)flag -{ - void traverse(id aNode) - { - if (*flag || aNode == [self nilNode] || !aNode) - return; - traverse([aNode rightNode]); - (*aFunc)(aNode); - traverse([aNode leftNode]); - } - traverse(_contents_root); - return self; -} - -- (BOOL) getNextElement:(elt *)anElementPtr withEnumState: (void**)enumState +- nextObjectWithEnumState: (void**)enumState { if (!(*enumState)) *enumState = [self leftmostNodeFromNode:_contents_root]; else - *enumState = [self successorOfElement:*enumState].id_u; - *anElementPtr = *enumState; - if (*enumState) - return YES; - return NO; + *enumState = [self successorOfObject:*enumState]; + return (id) *enumState; } -- (BOOL) getPrevElement:(elt *)anElementPtr withEnumState: (void**)enumState +- prevObjectWithEnumState: (void**)enumState { if (!(*enumState)) *enumState = [self rightmostNodeFromNode:_contents_root]; else - *enumState = [self predecessorElement:*enumState].id_u; - *anElementPtr = *enumState; - if (*enumState) - return YES; - return NO; + *enumState = [self predecessorObject:*enumState]; + return (id) *enumState; } - (unsigned) count @@ -591,14 +467,12 @@ static id nilBinaryTreeNode; return _count; } + /* replace this with something better eventually */ - _tmpPrintFromNode: aNode indent: (int)count { printf("%-*s", count, ""); - if ([aNode respondsTo:@selector(printForDebugger)]) - [aNode printForDebugger]; - else - printf("?\n"); + printf("%s\n", [[aNode description] cStringNoCopy]); printf("%-*s.", count, ""); if ([aNode leftNode] != [self nilNode]) [self _tmpPrintFromNode:[aNode leftNode] indent:count+2]; @@ -621,3 +495,112 @@ static id nilBinaryTreeNode; @end + +/* These methods removed because they belong to an + OrderedCollection implementation, not an IndexedCollection + implementation. */ + +#if 0 +// NOTE: This gives you the power to put elements in unsorted order; +- insertObject: newObject before: oldObject +{ + id tmp; + + /* Make sure no one else already owns the newObject. */ + assert ([newObject linkedList] == NO_OBJECT); + + /* Claim ownership of the newObject. */ + [newObject retain]; + [newObject setBinaryTree: self]; + + [newObject setRightNode:[self nilNode]]; + [newObject setLeftNode:[self nilNode]]; + if ((tmp = [oldObject leftNode]) != [self nilNode]) + { + [(tmp = [self rightmostNodeFromNode:tmp]) setRightNode:newObject]; + [newObject setParentNode:tmp]; + } + else if (newObject != [self nilNode]) + { + [oldObject setLeftNode:newObject]; + [newObject setParentNode:oldObject]; + } + else + { + _contents_root = newObject; + [newObject setParentNode:[self nilNode]]; + } + _count++; + RETAIN_ELT(newObject); + return self; +} + +// NOTE: This gives you the power to put elements in unsorted order; +- insertObject: newObject after: oldObject +{ + id tmp; + + /* Make sure no one else already owns the newObject. */ + assert ([newObject linkedList] == NO_OBJECT); + + /* Claim ownership of the newObject. */ + [newObject retain]; + [newObject setBinaryTree: self]; + + [newObject setRightNode:[self nilNode]]; + [newObject setLeftNode:[self nilNode]]; + if ((tmp = [oldObject rightNode]) != [self nilNode]) + { + [(tmp = [self leftmostNodeFromNode:tmp]) setLeftNode:newObject]; + [newObject setParentNode:tmp]; + } + else if (newObject != [self nilNode]) + { + [oldObject setRightNode:newObject]; + [newObject setParentNode:oldObject]; + } + else + { + _contents_root = newObject; + [newObject setParentNode:[self nilNode]]; + } + _count++; + RETAIN_ELT(newObject); + return self; +} + +// NOTE: This gives you the power to put elements in unsorted order; +- insertObject: newObject atIndex: (unsigned)index +{ + CHECK_INDEX_RANGE_ERROR(index, _count+1); + + if (index == _count) + [self appendObject:newObject]; + else + [self insertObject:newObject before:[self ObjectAtIndex:index]]; + return self; +} + +// NOTE: This gives you the power to put elements in unsorted order; +- appendObject: newObject +{ + if (_count == 0) + { + /* Make sure no one else already owns the newObject. */ + assert ([newObject linkedList] == NO_OBJECT); + + /* Claim ownership of the newObject. */ + [newObject retain]; + [newObject setBinaryTree: self]; + + _contents_root = newObject; + _count = 1; + [newObject setLeftNode:[self nilNode]]; + [newObject setRightNode:[self nilNode]]; + [newObject setParentNode:[self nilNode]]; + } + else + [self insertObject:newObject after:[self lastObject]]; + return self; +} +#endif diff --git a/Source/BinaryTreeNode.m b/Source/BinaryTreeNode.m index 983d7aa08..085505350 100644 --- a/Source/BinaryTreeNode.m +++ b/Source/BinaryTreeNode.m @@ -45,6 +45,8 @@ [aCoder encodeObjectReference:_right withName:@"Right BinaryTree Node"]; [aCoder encodeObjectReference:_left withName:@"Left BinaryTree Node"]; [aCoder encodeObjectReference:_parent withName:@"Parent BinaryTree Node"]; + [aCoder encodeObjectReference:_binary_tree + withName:@"BinaryTree"]; } - initWithCoder: aCoder @@ -53,6 +55,7 @@ [aCoder decodeObjectAt:&_right withName:NULL]; [aCoder decodeObjectAt:&_left withName:NULL]; [aCoder decodeObjectAt:&_parent withName:NULL]; + [aCoder decodeObjectAt:&_binary_tree withName:NULL]; return self; } @@ -71,22 +74,29 @@ return _parent; } -- setLeftNode: aNode +- (void) setLeftNode: aNode { _left = aNode; - return self; } -- setRightNode: aNode +- (void) setRightNode: aNode { _right = aNode; - return self; } -- setParentNode: aNode +- (void) setParentNode: aNode { _parent = aNode; - return self; +} + +- binaryTree +{ + return _binary_tree; +} + +- (void) setBinaryTree: anObject +{ + _binary_tree = anObject; } @end diff --git a/Source/CircularArray.m b/Source/CircularArray.m index 64ce122bc..c1ed26408 100644 --- a/Source/CircularArray.m +++ b/Source/CircularArray.m @@ -1,8 +1,8 @@ /* Implementation for Objective-C CircularArray collection object - Copyright (C) 1993,1994, 1995 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1995, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum - Date: May 1993 + Created: May 1993 This file is part of the GNU Objective C Class Library. @@ -27,17 +27,10 @@ @implementation CircularArray -+ (void) initialize -{ - if (self == [CircularArray class]) - [self setVersion:0]; /* beta release */ -} - /* This is the designated initializer of this class */ -- initWithType: (const char *)contentEncoding - capacity: (unsigned)aCapacity +- initWithCapacity: (unsigned)aCapacity { - [super initWithType:contentEncoding capacity:aCapacity]; + [super initWithCapacity:aCapacity]; _start_index = 0; return self; } @@ -51,13 +44,6 @@ return self; } -- _readInit: (TypedStream*)aStream -{ - [super _readInit: aStream]; - _start_index = 0; - return self; -} - /* Empty copy must empty an allocCopy'ed version of self */ - emptyCopy @@ -67,24 +53,23 @@ return copy; } -- _empty +- (void) _collectionEmpty { - [super _empty]; + [super _collectionEmpty]; _start_index = 0; - return self; } /* This is the only method that changes the value of the instance - variable _capacity, except for "-initDescription:capacity:" */ + variable _capacity, except for "-initWithCapacity:" */ -- setCapacity: (unsigned)newCapacity +- (void) setCapacity: (unsigned)newCapacity { - elt *new_contents; + id *new_contents; int i; if (newCapacity > _count) { /* This could be more efficient */ - OBJC_MALLOC(new_contents, elt, newCapacity); + OBJC_MALLOC(new_contents, id, newCapacity); for (i = 0; i < _count; i++) new_contents[i] = _contents_array[CIRCULAR_TO_BASIC(i)]; OBJC_FREE(_contents_array); @@ -92,95 +77,83 @@ _start_index = 0; _capacity = newCapacity; } - return self; } -- (elt) removeElementAtIndex: (unsigned)index +- (void) removeObjectAtIndex: (unsigned)index { unsigned basicIndex; - elt ret; CHECK_INDEX_RANGE_ERROR(index, _count); basicIndex = CIRCULAR_TO_BASIC(index); - ret = _contents_array[basicIndex]; + [_contents_array[basicIndex] release]; circularFillHoleAt(self, basicIndex); decrementCount(self); - return AUTORELEASE_ELT(ret); } -- (elt) removeFirstElement +- (void) removeFirstObject { - elt ret; - - ret = _contents_array[_start_index]; + if (!_count) + return; + [_contents_array[_start_index] release]; _start_index = (_start_index + 1) % _capacity; decrementCount(self); - return AUTORELEASE_ELT(ret); } -- (elt) removeLastElement +- (void) removeLastObject { - elt ret; - if (!_count) - NO_ELEMENT_FOUND_ERROR(); - ret = _contents_array[CIRCULAR_TO_BASIC(_count-1)]; + return; + [_contents_array[CIRCULAR_TO_BASIC(_count-1)] release]; decrementCount(self); - return AUTORELEASE_ELT(ret); } -- (elt) elementAtIndex: (unsigned)index +- objectAtIndex: (unsigned)index { CHECK_INDEX_RANGE_ERROR(index, _count); return _contents_array[CIRCULAR_TO_BASIC(index)]; } -- appendElement: (elt)newElement +- (void) appendObject: newObject { incrementCount(self); - RETAIN_ELT(newElement); - _contents_array[CIRCULAR_TO_BASIC(_count-1)] = newElement; - return self; + [newObject retain]; + _contents_array[CIRCULAR_TO_BASIC(_count-1)] = newObject; } -- prependElement: (elt)newElement +- (void) prependElement: newObject { incrementCount(self); - RETAIN_ELT(newElement); + [newObject retain]; _start_index = (_capacity + _start_index - 1) % _capacity; - _contents_array[_start_index] = newElement; - return self; + _contents_array[_start_index] = newObject; } -- insertElement: (elt)newElement atIndex: (unsigned)index +- (void) insertElement: newObject atIndex: (unsigned)index { unsigned basicIndex; CHECK_INDEX_RANGE_ERROR(index, _count+1); incrementCount(self); - RETAIN_ELT(newElement); + [newObject retain]; basicIndex = CIRCULAR_TO_BASIC(index); circularMakeHoleAt(self, basicIndex); - _contents_array[basicIndex] = newElement; - return self; + _contents_array[basicIndex] = newObject; } -- (elt) replaceElementAtIndex: (unsigned)index with: (elt)newElement +- (void) replaceObjectAtIndex: (unsigned)index withObject: newObject { - elt ret; unsigned basicIndex; CHECK_INDEX_RANGE_ERROR(index, _count); - RETAIN_ELT(newElement); + [newObject retain]; basicIndex = CIRCULAR_TO_BASIC(index); - ret = _contents_array[basicIndex]; - _contents_array[basicIndex] = newElement; - return AUTORELEASE_ELT(ret); + [_contents_array[basicIndex] release]; + _contents_array[basicIndex] = newObject; } -- swapAtIndeces: (unsigned)index1 : (unsigned)index2 +- (void) swapAtIndeces: (unsigned)index1 : (unsigned)index2 { - elt tmp; + id tmp; CHECK_INDEX_RANGE_ERROR(index1, _count); CHECK_INDEX_RANGE_ERROR(index2, _count); @@ -189,10 +162,10 @@ tmp = _contents_array[index1]; _contents_array[index1] = _contents_array[index2]; _contents_array[index2] = tmp; - return self; } -/* just temporary for debugging +#if 0 +/* just temporary for debugging */ - circularArrayPrintForDebugger { int i; @@ -217,7 +190,7 @@ return self; } -*/ +#endif @end diff --git a/Source/Collection.m b/Source/Collection.m index cd10d1ca3..6d2e03c6c 100644 --- a/Source/Collection.m +++ b/Source/Collection.m @@ -27,347 +27,187 @@ #include /* for -contentsEqual: */ #include /* for -safeWithElementsCall: */ #include +#include -@implementation Collection +@implementation Enumerator -+ (void) initialize +- initWithCollection: coll { - if (self == [Collection class]) - { - [self setVersion:0]; // beta release; - } -} - -// INITIALIZING AND RELEASING; - -// This is the designated initializer of this class; -- initWithType:(const char *)contentEncoding -{ - [super init]; - if (!elt_get_comparison_function(contentEncoding)) - [self error:"There is no elt comparison function for type encoding %s", - contentEncoding]; + collection = [coll retain]; + enum_state = [coll newEnumState]; return self; } +- nextObject +{ + return [collection nextObjectWithEnumState: enum_state]; +} + +- (void) dealloc +{ + [collection freeEnumState: enum_state]; + [collection release]; +} + +@end + +@implementation ConstantCollection + + +// INITIALIZING AND RELEASING; + - init { - // default contents are objects; - return [self initWithType:@encode(id)]; + return [self initWithObjects: NULL count: 0]; +} + +// This is the designated initializer of this class; +- initWithObjects: (id*)objc count: (unsigned)c +{ + [self subclassResponsibility: _cmd]; + return self; +} + +- initWithObjects: firstObject, ... +{ + va_list ap; + va_start(ap, firstObject); + self = [self initWithObjects:firstObject rest:ap]; + va_end(ap); + return self; +} + +#define INITIAL_OBJECTS_SIZE 10 +- initWithObjects: firstObject rest: (va_list)ap +{ + id *objects; + int i = 0; + int s = INITIAL_OBJECTS_SIZE; + + OBJC_MALLOC(objects, id, s); + if (firstObject != nil) + { + objects[i++] = firstObject; + while ((objects[i++] = va_arg(ap, id))) + { + if (i >= s) + { + s *= 2; + OBJC_REALLOC(objects, id, s); + } + } + } + self = [self initWithObjects:objects count:i-1]; + OBJC_FREE(objects); + return self; } /* Subclasses can override this for efficiency. For example, Array can init itself with enough capacity to hold aCollection. */ - initWithContentsOf: (id )aCollection { - [self initWithType:[aCollection contentType]]; - [self addContentsOf:aCollection]; - return self; -} - -- (void) _safeWithElementsCallNoRetain: (void(*)(elt))aFunc -{ - int c = [self count]; - elt *elts = (elt*) (*objc_malloc) (c * sizeof(elt)); + int count = [aCollection count]; + id contents_array[count]; + id o; int i = 0; - void *es = [self newEnumState]; - elt e; - while ([self getNextElement:&e withEnumState:&es]) - { - elts[i++] = e; - } - [self freeEnumState:&es]; - assert (c == i); - for (i = 0; i < c; i++) - aFunc(elts[i]); - (*objc_free) (elts); -} -- (void) _releaseContents -{ - int c = [self count]; - elt *elts = (elt*) (*objc_malloc) (c * sizeof(elt)); - int i = 0; - void *es = [self newEnumState]; - elt e; - while ([self getNextElement:&e withEnumState:&es]) + FOR_COLLECTION(aCollection, o) { - elts[i++] = e; + contents_array[i++] = o; } - [self freeEnumState:&es]; - assert (c == i); - for (i = 0; i < c; i++) - [elts[i].id_u release]; - (*objc_free) (elts); + END_FOR_COLLECTION(aCollection); + return [self initWithObjects: contents_array count: count]; } - (void) dealloc { - if (CONTAINS_OBJECTS) - [self _releaseContents]; + /* xxx Get rid of this since Set, Bag, Dictionary, and String + subclasses don't want to use it? */ + [self _collectionReleaseContents]; [self _collectionDealloc]; [super dealloc]; } -/* May be inefficient. Could be overridden; */ -- empty -{ - if ([self isEmpty]) - return self; - if (CONTAINS_OBJECTS) - [self _releaseContents]; - [self _empty]; - return self; -} - -// ADDING; - -- addElement: (elt)newElement -{ - return [self subclassResponsibility:_cmd]; -} - -- addElementIfAbsent: (elt)newElement -{ - if (![self includesElement:newElement]) - return [self addElement:newElement]; - return nil; -} - -- addContentsOf: (id )aCollection -{ - id (*addElementImp)(id,SEL,elt) = (id(*)(id,SEL,elt)) - objc_msg_lookup(self, @selector(addElement:)); - - void doIt(elt e) - { - (*addElementImp)(self, @selector(addElement:), e); - } - - [aCollection withElementsCall:doIt]; - return self; -} - -- addContentsOfIfAbsent: (id )aCollection -{ - id (*addElementImp)(id,SEL,elt) = (id(*)(id,SEL,elt)) - objc_msg_lookup(self, @selector(addElement:)); - BOOL (*includesElementImp)(id,SEL,elt) = (BOOL(*)(id,SEL,elt)) - objc_msg_lookup(self, @selector(includesElement:)); - - void doIt(elt e) - { - if (!((*includesElementImp)(self, @selector(includesElement), e))) - (*addElementImp)(self, @selector(addElement:), e); - } - - [aCollection withElementsCall:doIt]; - return self; -} - -- addElementsCount: (unsigned)count, ... -{ - va_list ap; - - // could use objc_msg_lookup here also; - va_start(ap, count); - while (count--) - [self addElement:va_arg(ap, elt)]; - va_end(ap); - return self; -} - - -// REMOVING AND REPLACING; - -- (elt) removeElement: (elt)oldElement -{ - elt err(arglist_t argFrame) - { - return ELEMENT_NOT_FOUND_ERROR(oldElement); - } - return [self removeElement:oldElement ifAbsentCall:err]; -} - -- (elt) removeElement: (elt)oldElement ifAbsentCall: (elt(*)(arglist_t))excFunc -{ - return [self subclassResponsibility:_cmd]; -} - - -- removeAllOccurrencesOfElement: (elt)oldElement -{ - BOOL (*includesElementImp)(id,SEL,elt) = (BOOL(*)(id,SEL,elt)) - objc_msg_lookup(self, @selector(includesElement:)); - elt (*removeElementImp)(id,SEL,elt) = (elt(*)(id,SEL,elt)) - objc_msg_lookup(self, @selector(removeElement:)); - - while ((*includesElementImp)(self, @selector(includesElement:), oldElement)) - { - (*removeElementImp)(self, @selector(removeElement:), oldElement); - } - return self; -} - -- removeContentsIn: (id )aCollection -{ - BOOL (*includesElementImp)(id,SEL,elt) = (BOOL(*)(id,SEL,elt)) - objc_msg_lookup(self, @selector(includesElement:)); - elt (*removeElementImp)(id,SEL,elt) = (elt(*)(id,SEL,elt)) - objc_msg_lookup(self, @selector(removeElement:)); - - void doIt(elt e) - { - if ((*includesElementImp)(self, @selector(includesElement:), e)) - (*removeElementImp)(self, @selector(removeElement:), e); - } - - [aCollection withElementsCall:doIt]; - return self; -} - -- removeContentsNotIn: (id )aCollection -{ - BOOL (*includesElementImp)(id,SEL,elt) = (BOOL(*)(id,SEL,elt)) - objc_msg_lookup(aCollection, @selector(includesElement:)); - elt (*removeElementImp)(id,SEL,elt) = (elt(*)(id,SEL,elt)) - objc_msg_lookup(self, @selector(removeElement:)); - - void doIt(elt e) - { - if (!(*includesElementImp)(aCollection, @selector(includesElement:), e)) - (*removeElementImp)(self, @selector(removeElement:), e); - } - - [self safeWithElementsCall:doIt]; - return self; -} - -// remember this has to be overridden for IndexedCollection's; -- (elt) replaceElement: (elt )oldElement with: (elt )newElement - ifAbsentCall: (elt(*)(arglist_t))excFunc -{ - elt err(arglist_t argFrame) - { - RETURN_BY_CALLING_EXCEPTION_FUNCTION(excFunc); - } - elt ret; - ret = [self removeElement:oldElement ifAbsentCall:err]; - [self addElement:newElement]; - return ret; -} - -- (elt) replaceElement: (elt )oldElement with: (elt)newElement -{ - elt err(arglist_t argFrame) - { - return ELEMENT_NOT_FOUND_ERROR(oldElement); - } - return [self replaceElement:oldElement with:newElement - ifAbsentCall:err]; -} - -- replaceAllOccurrencesOfElement: (elt)oldElement with: (elt)newElement -{ - BOOL (*includesElementImp)(id,SEL,elt) = (BOOL(*)(id,SEL,elt)) - objc_msg_lookup(self, @selector(includesElement:)); - elt (*replaceElementImp)(id,SEL,elt,elt) = (elt(*)(id,SEL,elt,elt)) - objc_msg_lookup(self, @selector(replaceElement:with:)); - - if (ELEMENTS_EQUAL(oldElement, newElement)) - return self; - while ((*includesElementImp)(self,@selector(includesElement:),oldElement)) - (*replaceElementImp)(self,@selector(replaceElement:with:), - oldElement, newElement); - return self; -} - -/* This is pretty inefficient? Try to come up with something better. */ -- uniqueContents -{ - // Use objc_msg_lookup here also; - void doIt(elt e) - { - while ([self occurrencesOfElement:e] > 1) - [self removeElement:e]; - } - [self safeWithElementsCall:doIt]; - return self; -} - -// TESTING; + +// QUERYING COUNTS; - (BOOL) isEmpty { return ([self count] == 0); } -// Potentially inefficient, may be overridden in subclasses; -- (BOOL) includesElement: (elt)anElement +// Inefficient, so should be overridden in subclasses; +- (unsigned) count { - int (*cf)(elt,elt) = [self comparisonFunction]; - BOOL test(elt e) - { - if (!((*cf)(anElement, e))) - return YES; - else - return NO; - } + unsigned n = 0; + id o; - return [self trueForAnyElementsByCalling:test]; + FOR_COLLECTION(self, o) + { + n++; + } + END_FOR_COLLECTION(self); + return n; } +// Potentially inefficient, may be overridden in subclasses; +- (BOOL) containsObject: anObject +{ + id o; + FOR_COLLECTION (self, o) + { + if ([anObject isEqual: o]) + return YES; + } + END_FOR_COLLECTION(self); + return NO; +} + +- (unsigned) occurrencesOfObject: anObject +{ + unsigned count = 0; + id o; + + FOR_COLLECTION(self, o) + { + if ([anObject isEqual: o]) + count++; + } + END_FOR_COLLECTION(self); + return count; +} + + +// COMPARISON WITH OTHER COLLECTIONS; + - (BOOL) isSubsetOf: (id )aCollection { - BOOL test(elt e) - { - return [aCollection includesElement:e]; + id o; + FOR_COLLECTION (self, o) + { + if (![aCollection containsObject: o]) + return NO; } - return [self trueForAllElementsByCalling:test]; + END_FOR_COLLECTION (self); + return YES; } -- (BOOL) contentsEqual: (id )aCollection +- (BOOL) isDisjointFrom: (id )aCollection { - id bag; - BOOL flag; + // Use objc_msg_lookup here also; + BOOL flag = YES; + id o; - // Could use objc_msg_lookup here also; - void doIt(elt e) + FOR_COLLECTION_WHILE_TRUE(self, o, flag) { - if ([bag includesElement:e]) - [bag removeElement:e]; - else + if (![aCollection containsObject: o]) flag = NO; } - - if ([self count] != [aCollection count] - || ([self comparisonFunction] != [aCollection comparisonFunction])) - return NO; - bag = [[Bag alloc] initWithContentsOf:aCollection]; - flag = YES; - [self withElementsCall:doIt whileTrue:&flag]; - if ((!flag) || [bag count]) - flag = NO; - else - flag = YES; - [bag release]; - return flag; + END_FOR_COLLECTION_WHILE_TRUE(self); + return !flag; } -/* This is what I'd like the -compare: implementation in Object.m to - look like. */ -- (int) _objectCompare: anObject -{ - if (self == anObject) - return 0; - if (self > anObject) - return 1; - else - return -1; -} - -// Fix this ; -// How do we want to compare unordered contents?? ; +// xxx How do we want to compare unordered contents?? ; - (int) compareContentsOf: (id )aCollection { if ([self contentsEqual:aCollection]) @@ -377,17 +217,6 @@ return -1; } -// Deal with this in IndexedCollection also ; -// How do we want to compare collections? ; -- (int) compare: anObject -{ - if ([self isEqual:anObject]) - return 0; - if (self > anObject) - return 1; - return -1; -} - - (BOOL) isEqual: anObject { if (self == anObject) @@ -399,198 +228,274 @@ return NO; } -- (BOOL) isDisjointFrom: (id )aCollection +// Deal with this in IndexedCollection also ; +// How do we want to compare collections? ; +- (int) compare: anObject { - // Use objc_msg_lookup here also; - BOOL flag = YES; - void doIt(elt e) - { - if (![aCollection includesElement:e]) - flag = NO; - } - [self withElementsCall:doIt whileTrue:&flag]; - return !flag; + if ([self isEqual:anObject]) + return 0; + if (self > anObject) + return 1; + return -1; } -- (BOOL) trueForAllElementsByCalling: (BOOL(*)(elt))aFunc +- (BOOL) contentsEqual: (id )aCollection { - BOOL flag = YES; - void doIt(elt e) + id bag, o; + BOOL flag; + + if ([self count] != [aCollection count]) + return NO; + bag = [[Bag alloc] initWithContentsOf:aCollection]; + flag = YES; + FOR_COLLECTION_WHILE_TRUE (self, o, flag) { - if (!((*aFunc)(e))) + if ([bag containsObject: o]) + [bag removeObject: o]; + else flag = NO; } - [self withElementsCall:doIt whileTrue:&flag]; + END_FOR_COLLECTION_WHILE_TRUE(self); + if ((!flag) || [bag count]) + flag = NO; + else + flag = YES; + [bag release]; return flag; } -- (BOOL) trueForAllObjectsByCalling: (BOOL(*)(id))aFunc + +// PROPERTIES OF CONTENTS; + +- (BOOL) trueForAllObjectsByInvoking: (id )anInvocation { BOOL flag = YES; - void doIt(elt e) + id o; + + FOR_COLLECTION_WHILE_TRUE(self, o, flag) { - if (!((*aFunc)(e.id_u))) + [anInvocation invokeWithObject: o]; + if (![anInvocation returnValueIsTrue]) flag = NO; } - CHECK_CONTAINS_OBJECTS_ERROR(); - [self withElementsCall:doIt whileTrue:&flag]; + END_FOR_COLLECTION_WHILE_TRUE(self); return flag; } -- (BOOL) trueForAnyElementsByCalling: (BOOL(*)(elt))aFunc; +- (BOOL) trueForAnyObjectsByInvoking: (id )anInvocation; { BOOL flag = YES; - void doIt(elt e) + id o; + + FOR_COLLECTION_WHILE_TRUE(self, o, flag) { - if ((*aFunc)(e)) + [anInvocation invokeWithObject: o]; + if ([anInvocation returnValueIsTrue]) flag = NO; } - [self withElementsCall:doIt whileTrue:&flag]; + END_FOR_COLLECTION_WHILE_TRUE(self); return !flag; } -- (BOOL) trueForAnyObjectsByCalling: (BOOL(*)(id))aFunc; +- detectObjectByInvoking: (id )anInvocation; { BOOL flag = YES; - void doIt(elt e) + id detectedObject; + id o; + + FOR_COLLECTION_WHILE_TRUE(self, o, flag) { - if ((*aFunc)(e.id_u)) - flag = NO; + if ([anInvocation invokeWithObject: o]) + { + flag = NO; + detectedObject = o; + } } - CHECK_CONTAINS_OBJECTS_ERROR(); - [self withElementsCall:doIt whileTrue:&flag]; - return !flag; + END_FOR_COLLECTION_WHILE_TRUE(self); + if (flag) + return NO_OBJECT; + else + return detectedObject; } -// Inefficient, so should be overridden in subclasses; -- (unsigned) count +- maxObject { - unsigned n = 0; - void doIt(elt e) + id o, max; + BOOL firstTime = YES; + + if (![self count]) + return NO_OBJECT; + FOR_COLLECTION(self, o) { - n++; + if (firstTime) + { + firstTime = NO; + max = o; + } + else + { + if ([o compare: max] > 0) + max = o; + } } - [self withElementsCall:doIt]; - return n; + END_FOR_COLLECTION(self); + return max; } -- (unsigned) occurrencesOfElement: (elt)anElement +- minObject { - unsigned count = 0; - int (*cf)(elt,elt) = [self comparisonFunction]; - void doIt(elt e) + id o, min; + BOOL firstTime = YES; + + if (![self count]) + return NO_OBJECT; + FOR_COLLECTION(self, o) { - if (!((*cf)(anElement, e))) - count++; + if (firstTime) + { + firstTime = NO; + min = o; + } + else + { + if ([o compare: min] < 0) + min = o; + } } - [self withElementsCall:doIt]; - return count; + END_FOR_COLLECTION(self); + return min; } -- (unsigned) occurrencesOfObject: anObject -{ - CHECK_CONTAINS_OBJECTS_ERROR(); - return [self occurrencesOfElement:anObject]; -} - -- (BOOL) contentsAreObjects -{ - return CONTAINS_OBJECTS; -} - -/* The two default implementations below are used by the node collection - objects: the collections whose contents are required to be objects - conforming to some <...Comprising> protocol. */ - -/* some subclasses will have to override this for correctness */ -- (const char *) contentType -{ - /* objects are the default */ - return @encode(id); -} - -/* some subclasses will have to override this for correctness */ -- (int(*)(elt,elt)) comparisonFunction -{ - /* objects are the default */ - return elt_compare_objects; -} +/* Consider adding: + - maxObjectByInvoking: (id )anInvocation; + - minObjectByInvoking: (id )anInvocation; + */ + // ENUMERATING; -- (BOOL) getNextElement:(elt *)anElementPtr withEnumState: (void**)enumState +- (id ) objectEnumerator { - [self subclassResponsibility:_cmd]; - return NO; + return [[[Enumerator alloc] initWithCollection: self] + autorelease]; } +- (void) withObjectsInvoke: (id )anInvocation +{ + id o; + + FOR_COLLECTION(self, o) + { + [anInvocation invokeWithObject: o]; + } + END_FOR_COLLECTION(self); +} + +- (void) withObjectsInvoke: (id )anInvocation whileTrue:(BOOL *)flag; +{ + id o; + + FOR_COLLECTION_WHILE_TRUE(self, o, *flag) + { + [anInvocation invokeWithObject: o]; + } + END_FOR_COLLECTION_WHILE_TRUE(self); +} + +- (void) makeObjectsPerform: (SEL)aSel +{ + id o; + + FOR_COLLECTION(self, o) + { + [o perform: aSel]; + } + END_FOR_COLLECTION(self); +} + +- (void) makeObjectsPerform: (SEL)aSel withObject: argObject +{ + id o; + + FOR_COLLECTION(self, o) + { + [o perform: aSel withObject: argObject]; + } + END_FOR_COLLECTION(self); +} + + + +// FILTERED ENUMERATING; + +- (void) withObjectsTrueByInvoking: (id )testInvocation + invoke: (id )anInvocation +{ + id o; + + FOR_COLLECTION(self, o) + { + [testInvocation invokeWithObject: o]; + if ([testInvocation returnValueIsTrue]) + [anInvocation invokeWithObject: o]; + } + END_FOR_COLLECTION(self); +} + +- (void) withObjectsFalseByInvoking: (id )testInvocation + invoke: (id )anInvocation +{ + id o; + + FOR_COLLECTION(self, o) + { + [testInvocation invokeWithObject: o]; + if (![testInvocation returnValueIsTrue]) + [anInvocation invokeWithObject: o]; + } + END_FOR_COLLECTION(self); +} + +- (void) withObjectsTransformedByInvoking: (id )transInvocation + invoke: (id )anInvocation +{ + id o; + + FOR_COLLECTION(self, o) + { + [transInvocation invokeWithObject: o]; + [anInvocation invokeWithObject: [transInvocation objectReturnValue]]; + } + END_FOR_COLLECTION(self); +} + + + +// LOW-LEVEL ENUMERATING; + - (void*) newEnumState { return (void*)0; } -- freeEnumState: (void**)enumState +- nextObjectWithEnumState: (void**)enumState; { - *enumState = (void*)0; - return self; -} - -// Getting objects one at a time. Pass *enumState == 0 to start.; -- (BOOL) getNextObject:(id *)anObjectPtr withEnumState: (void**)enumState; -{ - elt o; - - CHECK_CONTAINS_OBJECTS_ERROR(); - if ([self getNextElement:&o withEnumState:enumState]) - { - *anObjectPtr = o.id_u; - return YES; - } + [self subclassResponsibility: _cmd]; return NO; } -- withElementsCall: (void(*)(elt))aFunc whileTrue: (BOOL*)flag +- (void) freeEnumState: (void**)enumState { - void *enumState = [self newEnumState]; - elt e; - - while (*flag && [self getNextElement:&e withEnumState:&enumState]) - (*aFunc)(e); - [self freeEnumState:&enumState]; - return self; + *enumState = (void*)0; } -- withElementsCall: (void(*)(elt))aFunc -{ - BOOL flag = YES; - return [self withElementsCall:aFunc whileTrue:&flag]; -} - -- safeWithElementsCall: (void(*)(elt))aFunc -{ - id tmp = [[Array alloc] initWithContentsOf:self]; - [tmp withElementsCall:aFunc]; - [tmp release]; - return self; -} - -- safeWithElementsCall: (void(*)(elt))aFunc whileTrue: (BOOL*)flag -{ - id tmp = [[Array alloc] initWithContentsOf:self]; - [tmp withElementsCall:aFunc whileTrue:flag]; - [tmp release]; - return self; -} + // COPYING; - allocCopy { -#if NeXT_runtime - return object_copy(self, 0); -#else - return object_copy(self); -#endif + return NSCopyObject (self, 0, [self zone]); } // the copy to be filled by -shallowCopyAs: etc... ; @@ -607,8 +512,7 @@ if (aCollectionClass == [self species]) return [self emptyCopy]; else - return [[(id)aCollectionClass alloc] - initWithType:[self contentType]]; + return [[(id)aCollectionClass alloc] init]; } - shallowCopy @@ -619,6 +523,7 @@ - shallowCopyAs: (Class)aCollectionClass { id newColl = [self emptyCopyAs:aCollectionClass]; + //#warning fix this addContentsOf for ConstantCollection [newColl addContentsOf:self]; return newColl; } @@ -631,14 +536,14 @@ - copyAs: (id )aCollectionClass { id newColl = [self emptyCopyAs:aCollectionClass]; - void addCopy(elt e) + id o; + + FOR_COLLECTION(self, o) { - [newColl addElement:[e.id_u copy]]; + //#warning fix this addObject for ConstantCollection + [newColl addObject:[o copy]]; } - if (CONTAINS_OBJECTS) - [self withElementsCall:addCopy]; - else - [newColl addContentsOf:self]; + END_FOR_COLLECTION(self); return newColl; } @@ -647,587 +552,20 @@ return [self copyAs:[self species]]; } -// This method shouldn't be necessary any more; -// (Yuck---replaceElement: is inefficient for many subclasses; -// Also, override this in KeyedCollection and IndexedCollection;) -- deepen -{ - // could use objc_msg_lookup here too; - void doIt(elt o) - { - [self replaceElement:o with:[[o.id_u shallowCopy] deepen]]; - } - - [self error:"Collections don't use -deepen. Use -copy instead."]; - if (!CONTAINS_OBJECTS) - return self; - [self safeWithElementsCall:doIt]; - return self; -} - - species { return [self class]; } - -// FILTERED ENUMERATING; - -- withElementsTrueByCalling: (BOOL(*)(elt))testFunc - call: (void(*)(elt))destFunc -{ - void doIt(elt e) - { - if ((*testFunc)(e)) - (*destFunc)(e); - } - [self withElementsCall:doIt]; - return self; -} - -- withObjectsTrueByCalling: (BOOL(*)(id))testFunc - call: (void(*)(id))destFunc -{ - void doIt(elt e) - { - if ((*testFunc)(e.id_u)) - (*destFunc)(e.id_u); - } - CHECK_CONTAINS_OBJECTS_ERROR(); - [self withElementsCall:doIt]; - return self; -} - -- withElementsFalseByCalling: (BOOL(*)(elt))testFunc - call: (void(*)(elt))destFunc -{ - void doIt(elt e) - { - if (!(*testFunc)(e)) - (*destFunc)(e); - } - [self withElementsCall:doIt]; - return self; -} - -- withObjectsFalseByCalling: (BOOL(*)(id))testFunc - call: (void(*)(id))destFunc -{ - void doIt(elt e) - { - if (!(*testFunc)(e.id_u)) - (*destFunc)(e.id_u); - } - CHECK_CONTAINS_OBJECTS_ERROR(); - [self withElementsCall:doIt]; - return self; -} - -- withElementsTransformedByCalling: (elt(*)(elt))transFunc - call: (void(*)(elt))destFunc -{ - void doIt(elt e) - { - (*destFunc)((*transFunc)(e)); - } - [self withElementsCall:doIt]; - return self; -} - -- withObjectsTransformedByCalling: (id(*)(id))transFunc - call: (void(*)(id))destFunc -{ - void doIt(elt e) - { - (*destFunc)((*transFunc)(e.id_u)); - } - CHECK_CONTAINS_OBJECTS_ERROR(); - [self withElementsCall:doIt]; - return self; -} - - -// NON-COPYING ENUMERATORS; - -- (elt) detectElementByCalling: (BOOL(*)(elt))aFunc - ifNoneCall: (elt(*)(arglist_t))excFunc -{ - BOOL flag = YES; - elt detectedElement; - void doIt(elt e) - { - if ((*aFunc)(e)) { - flag = NO; - detectedElement = e; - } - } - [self withElementsCall:doIt whileTrue:&flag]; - if (flag) - RETURN_BY_CALLING_EXCEPTION_FUNCTION(excFunc); - else - return detectedElement; -} - -- (elt) detectElementByCalling: (BOOL(*)(elt))aFunc -{ - elt err(arglist_t argFrame) - { - return NO_ELEMENT_FOUND_ERROR(); - } - return [self detectElementByCalling:aFunc ifNoneCall:err]; -} - -- (elt) maxElementByCalling: (int(*)(elt,elt))aFunc -{ - elt max; - BOOL firstTime = YES; - void doIt(elt e) - { - if (firstTime) - { - firstTime = NO; - max = e; - } - else - { - if ((*aFunc)(e,max) > 0) - max = e; - } - } - if (![self count]) - NO_ELEMENT_FOUND_ERROR(); - [self withElementsCall:doIt]; - return max; -} - -- (elt) maxElement -{ - return [self maxElementByCalling:COMPARISON_FUNCTION]; -} - -- (elt) minElementByCalling: (int(*)(elt,elt))aFunc -{ - elt min; - BOOL firstTime = YES; - void doIt(elt e) - { - if (firstTime) - { - firstTime = NO; - min = e; - } - else - { - if ((*aFunc)(e,min) < 0) - min = e; - } - } - if (![self count]) - NO_ELEMENT_FOUND_ERROR(); - [self withElementsCall:doIt]; - return min; -} - -- (elt) minElement -{ - return [self minElementByCalling:COMPARISON_FUNCTION]; -} - -- (elt) injectElement: (elt)initialData byCalling: (elt(*)(elt,elt))aFunc -{ - void doIt(elt e) - { - initialData = (*aFunc)(initialData, e); - } - [self withElementsCall:doIt]; - return initialData; -} - -- injectObject: (id)initialObject byCalling: (id(*)(id,id))aFunc -{ - void doIt(elt e) - { - initialObject = (*aFunc)(initialObject, e.id_u); - } - CHECK_CONTAINS_OBJECTS_ERROR(); - [self withElementsCall:doIt]; - return initialObject; -} - - -- maxObjectByCalling: (int(*)(id,id))aFunc -{ - id max; - BOOL firstTime = YES; - void doIt(id e) - { - if (firstTime) - { - firstTime = NO; - max = e; - } - else - { - if ((*aFunc)(e,max) > 0) - max = e; - } - } - CHECK_CONTAINS_OBJECTS_ERROR(); - if (![self count]) - NO_ELEMENT_FOUND_ERROR(); - [self withObjectsCall:doIt]; - return max; -} - -- maxObject -{ - CHECK_CONTAINS_OBJECTS_ERROR(); - return [self maxElement].id_u; -} - -- minObjectByCalling: (int(*)(id,id))aFunc -{ - id min; - BOOL firstTime = YES; - void doIt(id e) - { - if (firstTime) - { - firstTime = NO; - min = e; - } - else - { - if ((*aFunc)(e,min) < 0) - min = e; - } - } - CHECK_CONTAINS_OBJECTS_ERROR(); - if (![self count]) - NO_ELEMENT_FOUND_ERROR(); - [self withObjectsCall:doIt]; - return min; -} - -- minObject -{ - CHECK_CONTAINS_OBJECTS_ERROR(); - return [self minElement].id_u; -} - - - -// OBJECT-COMPATIBLE MESSAGE NAMES - -// ADDING; - -- addObject: newObject -{ - CHECK_CONTAINS_OBJECTS_ERROR(); - return [self addElement:newObject]; -} - -- addObjectIfAbsent: newObject -{ - CHECK_CONTAINS_OBJECTS_ERROR(); - return [self addElementIfAbsent:newObject]; -} - -- addObjectsCount: (unsigned)count, ... -{ - va_list ap; - - CHECK_CONTAINS_OBJECTS_ERROR(); - // could use objc_msg_lookup here also; - va_start(ap, count); - while (count--) - [self addElement:va_arg(ap, id)]; - va_end(ap); - return self; -} - -// REMOVING AND REPLACING; - -- removeObject: oldObject -{ - CHECK_CONTAINS_OBJECTS_ERROR(); - return [self removeElement:oldObject].id_u; -} - -- removeObject: oldObject ifAbsentCall: (id(*)(arglist_t))excFunc -{ - elt elt_err(arglist_t argFrame) - { - RETURN_BY_CALLING_EXCEPTION_FUNCTION(excFunc); - } - CHECK_CONTAINS_OBJECTS_ERROR(); - return [self removeElement:oldObject ifAbsentCall:elt_err].id_u; -} - -- removeAllOccurrencesOfObject: oldObject -{ - CHECK_CONTAINS_OBJECTS_ERROR(); - return [self removeAllOccurrencesOfElement:oldObject]; -} - -- replaceObject: oldObject with: newObject - ifAbsentCall: (id(*)(arglist_t))excFunc -{ - elt elt_err(arglist_t argFrame) - { - RETURN_BY_CALLING_EXCEPTION_FUNCTION(excFunc); - } - CHECK_CONTAINS_OBJECTS_ERROR(); - return [self replaceElement:oldObject with: newObject - ifAbsentCall:elt_err].id_u; -} - -- replaceObject: oldObject with: newObject -{ - CHECK_CONTAINS_OBJECTS_ERROR(); - return [self replaceElement:oldObject with:newObject].id_u; -} - -- replaceAllOccurrencesOfObject: oldObject with: newObject -{ - CHECK_CONTAINS_OBJECTS_ERROR(); - return [self replaceAllOccurrencesOfElement:oldObject with:newObject]; -} - -// TESTING; - -- (BOOL) includesObject: anObject -{ - CHECK_CONTAINS_OBJECTS_ERROR(); - return [self includesElement:anObject]; -} - - -// ENUMERATING - -- withObjectsCall: (void(*)(id))aFunc -{ - void doIt(elt e) - { - (*aFunc)(e.id_u); - } - CHECK_CONTAINS_OBJECTS_ERROR(); - return [self withElementsCall:doIt]; -} - -/* xxx a temporary implementation until the collection overhaul gets done. */ -- (void) withObjectsInvoke: anInvocation -{ - void doIt(elt e) - { - [anInvocation invokeWithObject: e.id_u]; - } - CHECK_CONTAINS_OBJECTS_ERROR(); - [self withElementsCall:doIt]; -} - -- safeWithObjectsCall: (void(*)(id))aFunc -{ - void doIt(elt e) - { - (*aFunc)(e.id_u); - } - CHECK_CONTAINS_OBJECTS_ERROR(); - return [self safeWithElementsCall:doIt]; -} - -- withObjectsCall: (void(*)(id))aFunc whileTrue:(BOOL *)flag -{ - void doIt(elt e) - { - (*aFunc)(e.id_u); - } - CHECK_CONTAINS_OBJECTS_ERROR(); - return [self withElementsCall:doIt whileTrue:flag]; -} - -- safeWithObjectsCall: (void(*)(id))aFunc whileTrue:(BOOL *)flag -{ - void doIt(elt e) - { - (*aFunc)(e.id_u); - } - CHECK_CONTAINS_OBJECTS_ERROR(); - return [self safeWithElementsCall:doIt whileTrue:flag]; -} - -- makeObjectsPerform: (SEL)aSel -{ - void doIt(elt e) - { - [e.id_u perform:aSel]; - } - - CHECK_CONTAINS_OBJECTS_ERROR(); - [self withElementsCall:doIt]; - return self; -} - -- safeMakeObjectsPerform: (SEL)aSel -{ - void doIt(elt e) - { - [e.id_u perform:aSel]; - } - - CHECK_CONTAINS_OBJECTS_ERROR(); - [self safeWithElementsCall:doIt]; - return self; -} - -- safeMakeObjectsPerform: (SEL)aSel with: argObject -{ - void doIt(elt e) - { - [e.id_u perform:aSel with:argObject]; - } - - CHECK_CONTAINS_OBJECTS_ERROR(); - [self safeWithElementsCall:doIt]; - return self; -} - -- makeObjectsPerform: (SEL)aSel with: argObject -{ - void doIt(elt e) - { - /* xxx change these to objc runtime functions, - in case object doesn't responds to "perform" methods. */ - [e.id_u perform:aSel with:argObject]; - } - - CHECK_CONTAINS_OBJECTS_ERROR(); - [self withElementsCall:doIt]; - return self; -} - -/* xxx What about adding "-askObjectsPerform: (SEL)aSel with: argObject" - that doesn't perform is object doesn't respond to aSel */ - -- withObjectsPerform: (SEL)aSel in: selObject -{ - id (*aSelImp)(id,SEL,id) = (id(*)(id,SEL,id)) - objc_msg_lookup(selObject, aSel); - void doIt(elt e) - { - aSelImp(selObject, aSel, e.id_u); - } - - CHECK_CONTAINS_OBJECTS_ERROR(); - [self withElementsCall:doIt]; - return self; -} - -- safeWithObjectsPerform: (SEL)aSel in: selObject -{ - id (*aSelImp)(id,SEL,id) = (id(*)(id,SEL,id)) - objc_msg_lookup(selObject, aSel); - void doIt(elt e) - { - aSelImp(selObject, aSel, e.id_u); - } - - CHECK_CONTAINS_OBJECTS_ERROR(); - [self safeWithElementsCall:doIt]; - return self; -} - -- withObjectsPerform: (SEL)aSel in: selObject with: argObject -{ - id (*aSelImp)(id,SEL,id,id) = (id(*)(id,SEL,id,id)) - objc_msg_lookup(selObject, aSel); - void doIt(elt e) - { - aSelImp(selObject, aSel, e.id_u, argObject); - } - - CHECK_CONTAINS_OBJECTS_ERROR(); - [self withElementsCall:doIt]; - return self; -} - -- safeWithObjectsPerform: (SEL)aSel in: selObject with: argObject -{ - id (*aSelImp)(id,SEL,id,id) = (id(*)(id,SEL,id,id)) - objc_msg_lookup(selObject, aSel); - void doIt(elt e) - { - aSelImp(selObject, aSel, e.id_u, argObject); - } - - CHECK_CONTAINS_OBJECTS_ERROR(); - [self safeWithElementsCall:doIt]; - return self; -} - - -// NON-COPYING ENUMERATORS; - -- detectObjectByCalling: (BOOL(*)(id))aFunc -{ - id err(arglist_t argFrame) - { - return NO_ELEMENT_FOUND_ERROR(); - } - return [self detectObjectByCalling:aFunc ifNoneCall:err]; -} - -- detectObjectByCalling: (BOOL(*)(id))aFunc - ifNoneCall: (id(*)(arglist_t))excFunc -{ - elt err(arglist_t argFrame) - { - RETURN_BY_CALLING_EXCEPTION_FUNCTION(excFunc); - } - BOOL test(elt e) - { - return (*aFunc)(e.id_u); - } - CHECK_CONTAINS_OBJECTS_ERROR(); - return [self detectElementByCalling:test ifNoneCall:err].id_u; -} - -// This printing stuff will change when we get Stream objects; - -- printElement: (elt)anElement -{ - elt_fprintf_elt(stdout, [self contentType], anElement); - return self; -} - -- printForDebugger -{ - void doIt(elt e) - { - [self printElement:e]; - printf(" "); - } - [self withElementsCall:doIt]; - printf(" :%s\n", [self name]); - return self; -} - -- _libobjectsMethodNotYetImplemented: (SEL)aSel -{ - [self error:"method %s in libobjects not yet implemented.\n\ -Contact mccallum@gnu.ai.mit.edu (R. Andrew McCallum)\n\ -for info about latest version.", - sel_get_name(aSel)]; - return self; -} + +// EXTRAS; - (const char *) libobjectsLicense { const char *licenseString = - "Copyright (C) 1993,1994,1994 Free Software Foundation, Inc.\n" + "Copyright (C) 1993,1994,1995,1996 Free Software Foundation, Inc.\n" "\n" - "Written by: R. Andrew McCallum \n" - "Date: May 1993\n" + "Chief Maintainer: Andrew McCallum \n" "\n" "This object is part of the GNU Objective C Class Library.\n" "\n" @@ -1247,21 +585,17 @@ for info about latest version.", return licenseString; } -- write: (TypedStream *)aStream -{ - [super write: aStream]; - [self _writeInit:aStream]; - [self _writeContents:aStream]; - return self; -} - -- read: (TypedStream *)aStream -{ - [super read: aStream]; - [self _readInit:aStream]; - [self _readContents:aStream]; - return self; -} +- printForDebugger +{ + id o; + FOR_COLLECTION(self, o) + { + printf("%s ", [[o description] cStringNoCopy]); + } + END_FOR_COLLECTION(self); + printf(": %s\n", [self name]); + return self; +} - (void) encodeWithCoder: aCoder { @@ -1278,8 +612,8 @@ for info about latest version.", @end - -@implementation Collection (ArchivingHelpers) + +@implementation ConstantCollection (ArchivingHelpers) - (void) _encodeCollectionWithCoder: aCoder { @@ -1294,94 +628,66 @@ for info about latest version.", return [super initWithCoder:aCoder]; } -- _writeInit: (TypedStream*)aStream -{ - // there are no instance vars; - return self; -} - -- _readInit: (TypedStream*)aStream -{ - // there are no instance vars; - return self; -} - -- (void) _encodeContentsWithCoder: (Coder*)aCoder +- (void) _encodeContentsWithCoder: (id )aCoder { unsigned int count = [self count]; - const char *encoding = [self contentType]; - void archiveElement(elt e) + id o; + + [aCoder encodeValueOfCType: @encode(unsigned) + at: &count + withName: @"Collection content count"]; + FOR_COLLECTION(self, o) { - [aCoder encodeValueOfObjCType:encoding - at:elt_get_ptr_to_member(encoding, &e) + [aCoder encodeObject: o withName:@"Collection element"]; } - - [aCoder encodeValueOfCType:@encode(unsigned) - at:&count - withName:@"Collection element count"]; - [self withElementsCall:archiveElement]; + END_FOR_COLLECTION(self); } -- (void) _decodeContentsWithCoder: (Coder*)aCoder +- (void) _decodeContentsWithCoder: (id )aCoder { + id *content_array; unsigned int count, i; - elt newElement; - const char *encoding = [self contentType]; + id o; [aCoder decodeValueOfCType:@encode(unsigned) at:&count withName:NULL]; + OBJC_MALLOC(content_array, id, count); for (i = 0; i < count; i++) - { - [aCoder decodeValueOfObjCType:encoding - at:elt_get_ptr_to_member(encoding, &newElement) - withName:NULL]; - [self addElement:newElement]; - } -} - -- _writeContents: (TypedStream*)aStream -{ - unsigned int count = [self count]; - const char *encoding = [self contentType]; - void archiveElement(elt e) - { - objc_write_type(aStream, encoding, - elt_get_ptr_to_member(encoding, &e)); - } - - objc_write_type(aStream, @encode(unsigned int), &count); - [self withElementsCall:archiveElement]; - return self; -} - -- _readContents: (TypedStream*)aStream -{ - unsigned int count, i; - elt newElement; - const char *encoding = [self contentType]; - - objc_read_type(aStream, @encode(unsigned int), &count); - for (i = 0; i < count; i++) - { - objc_read_type(aStream, encoding, - elt_get_ptr_to_member(encoding, &newElement)); - [self addElement:newElement]; - } - return self; + [aCoder decodeObjectAt: &(content_array[i]) + withName:NULL]; + [self initWithObjects: content_array count: count]; } @end -@implementation Collection (DeallocationHelpers) + +@implementation ConstantCollection (DeallocationHelpers) /* This must work without sending any messages to content objects. Content objects already may be dealloc'd when this is executed. */ -- _empty +- (void) _collectionEmpty { [self subclassResponsibility:_cmd]; - return self; +} + +- (void) _collectionReleaseContents +{ + int c = [self count]; + id *array = (id*) (*objc_malloc) (c * sizeof(id)); + int i = 0; + void *es = [self newEnumState]; + id o; + while ((o = [self nextObjectWithEnumState:&es])) + { + array[i++] = o; + } + [self freeEnumState: &es]; + assert (c == i); + for (i = 0; i < c; i++) + [array[i] release]; + (*objc_free) (array); } - (void) _collectionDealloc @@ -1390,3 +696,143 @@ for info about latest version.", } @end + + +@implementation Collection + +// ADDING; + +- (void) addObject: anObject +{ + [self subclassResponsibility:_cmd]; +} + +- (void) addObjectIfAbsent: newObject; +{ + if (![self containsObject: newObject]) + [self addObject: newObject]; +} + +- (void) addContentsOf: (id )aCollection +{ + id o; + + FOR_COLLECTION(aCollection, o) + { + [self addObject: o]; + } + END_FOR_COLLECTION(aCollection); +} + +- (void) addContentsIfAbsentOf: (id )aCollection +{ + id o; + + FOR_COLLECTION(aCollection, o) + { + if (![self containsObject:o]) + [self addObject: o]; + } + END_FOR_COLLECTION(aCollection); +} + +- (void) addWithObjects: (id*)objc count: (unsigned)c +{ + [self notImplemented: _cmd]; +} + +- (void) addObjects: firstObject, ... +{ + [self notImplemented: _cmd]; +} + +- (void) addObjects: firstObject rest: (va_list)ap +{ + [self notImplemented: _cmd]; +} + + +// REMOVING AND REPLACING; + +- (void) removeObject: oldObject +{ + [self subclassResponsibility: _cmd]; +} + +- (void) removeAllOccurrencesOfObject: oldObject +{ + while ([self containsObject: oldObject]) + [self removeObject: oldObject]; +} + +- (void) removeContentsIn: (id )aCollection +{ + id o; + + FOR_COLLECTION(aCollection, o) + { + [self removeObject: o]; + } + END_FOR_COLLECTION(aCollection); +} + +- (void) removeContentsNotIn: (id )aCollection +{ + id o; + + FOR_COLLECTION(self, o) + { + if (![aCollection containsObject: o]) + [self removeObject: o]; + } + END_FOR_COLLECTION(self); +} + +- (void) uniqueContents +{ + id cp = [self shallowCopy]; + int count; + id o; + + FOR_COLLECTION(self, o) + { + count = [self occurrencesOfObject: o]; + if (!count) + continue; + while (count--) + [self removeObject: o]; + } + END_FOR_COLLECTION(self); +} + +/* May be inefficient. Could be overridden; */ +- (void) empty +{ + if ([self isEmpty]) + return; + [self _collectionReleaseContents]; + [self _collectionEmpty]; +} + + +// REPLACING; + +- (void) replaceObject: oldObject withObject: newObject +{ + if ([newObject isEqual: newObject]) + return; + [oldObject retain]; + [self removeObject: oldObject]; + [self addObject: newObject]; + [oldObject release]; +} + +- (void) replaceAllOccurrencesOfObject: oldObject withObject: newObject +{ + if ([oldObject isEqual: newObject]) + return; + while ([self containsObject: oldObject]) + [self replaceObject: oldObject withObject: newObject]; +} + +@end diff --git a/Source/LinkedList.m b/Source/LinkedList.m index 9b8de79b4..e2e215878 100644 --- a/Source/LinkedList.m +++ b/Source/LinkedList.m @@ -1,5 +1,5 @@ /* Implementation for Objective-C LinkedList collection object - Copyright (C) 1993,1994, 1995 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1995, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum Date: May 1993 @@ -27,21 +27,22 @@ @implementation LinkedList -+ (void) initialize -{ - if (self == [LinkedList class]) - [self setVersion:0]; /* beta release */ -} - /* This is the designated initializer of this class */ - init { - [super initWithType:@encode(id)]; _count = 0; _first_link = nil; + _last_link = nil; return self; } +- initWithObjects: (id*)objs count: (unsigned)c +{ + [self init]; + while (c--) + [self prependObject: objs[c]]; +} + /* Archiving must mimic the above designated initializer */ - _initCollectionWithCoder: aCoder @@ -49,10 +50,11 @@ [super _initCollectionWithCoder:aCoder]; _count = 0; _first_link = nil; + _last_link = nil; return self; } -- (void) _encodeContentsWithCoder: aCoder +- (void) _encodeContentsWithCoder: (id )aCoder { [aCoder startEncodingInterconnectedObjects]; [super _encodeContentsWithCoder:aCoder]; @@ -63,257 +65,302 @@ We shouldn't do an -addElement. finishEncodingInterconnectedObjects should take care of all that. */ -- (void) _decodeContentsWithCoder: (Coder*)aCoder +- (void) _decodeContentsWithCoder: (id )aCoder { [aCoder startDecodingInterconnectedObjects]; [super _decodeContentsWithCoder:aCoder]; [aCoder finishDecodingInterconnectedObjects]; } -- _readInit: (TypedStream*)aStream -{ - [super _readInit:aStream]; - _count = 0; - _first_link = nil; - return self; -} - /* Empty copy must empty an allocCopy'ed version of self */ - emptyCopy { LinkedList *copy = [super emptyCopy]; - copy->_count = 0; copy->_first_link = nil; + copy->_last_link = nil; + copy->_count = 0; return copy; } /* This must work without sending any messages to content objects */ -- _empty +- (void) _empty { _count = 0; _first_link = nil; - return self; -} - -/* Override the designated initializer for our superclass IndexedCollection - to make sure we have object values. */ -- initWithType: (const char *)contentEncoding -{ - if (!ENCODING_IS_OBJECT(contentEncoding)) - [self error:"LinkedList contents must be objects conforming to " - " protocol"]; - [self init]; - return self; + _last_link = nil; } /* These next four methods are the only ones that change the values of the instance variables _count, _first_link, except for - "-initDescription:". */ + "-init". */ -- (elt) removeElement: (elt)oldElement +- (void) removeObject: oldObject { - if (_first_link == oldElement.id_u) + assert ([oldObject linkedList] == self); + if (_first_link == oldObject) { if (_count > 1) - _first_link = [oldElement.id_u nextLink]; + _first_link = [oldObject nextLink]; else _first_link = nil; } - [[oldElement.id_u nextLink] setPrevLink:[oldElement.id_u prevLink]]; - [[oldElement.id_u prevLink] setNextLink:[oldElement.id_u nextLink]]; + else + [[oldObject prevLink] setNextLink:[oldObject nextLink]]; + if (_last_link == oldObject) + { + if (_count > 1) + _last_link = [oldObject prevLink]; + else + _first_link = nil; + } + else + [[oldObject nextLink] setPrevLink:[oldObject prevLink]]; _count--; - return AUTORELEASE_ELT(oldElement); + [oldObject setNextLink: NO_OBJECT]; + [oldObject setPrevLink: NO_OBJECT]; + [oldObject release]; } -- insertElement: (elt)newElement after: (elt)oldElement +- (void) insertObject: newObject after: oldObject { + /* Make sure we actually own the oldObject. */ + assert ([oldObject linkedList] == self); + + /* Make sure no one else already owns the newObject. */ + assert ([newObject linkedList] == NO_OBJECT); + + /* Claim ownership of the newObject. */ + [newObject retain]; + [newObject setLinkedList: self]; + + /* Insert it. */ if (_count == 0) { - /* link to self */ - _first_link = newElement.id_u; - [newElement.id_u setNextLink:newElement.id_u]; - [newElement.id_u setPrevLink:newElement.id_u]; + _first_link = newObject; + _last_link = newObject; + [newObject setNextLink: NO_OBJECT]; + [newObject setPrevLink: NO_OBJECT]; } else { - [newElement.id_u setNextLink:[oldElement.id_u nextLink]]; - [newElement.id_u setPrevLink:oldElement.id_u]; - [[oldElement.id_u nextLink] setPrevLink:newElement.id_u]; - [oldElement.id_u setNextLink:newElement.id_u]; + if (oldObject == _last_link) + _last_link = newObject; + [newObject setNextLink: [oldObject nextLink]]; + [newObject setPrevLink: oldObject]; + [[oldObject nextLink] setPrevLink: newObject]; + [oldObject setNextLink: newObject]; } _count++; - return self; } -- insertElement: (elt)newElement before: (elt)oldElement +- (void) insertObject: newObject before: oldObject { - if (oldElement.id_u == _first_link) - _first_link = newElement.id_u; + /* Make sure we actually own the oldObject. */ + assert ([oldObject linkedList] == self); + + /* Make sure no one else already owns the newObject. */ + assert ([newObject linkedList] == NO_OBJECT); + + /* Claim ownership of the newObject. */ + [newObject retain]; + [newObject setLinkedList: self]; + + /* Insert it. */ if (_count == 0) { - /* Link to self */ - [newElement.id_u setNextLink:newElement.id_u]; - [newElement.id_u setPrevLink:newElement.id_u]; + _first_link = newObject; + _last_link = newObject; + [newObject setNextLink: NO_OBJECT]; + [newObject setPrevLink: NO_OBJECT]; } else { - [newElement.id_u setPrevLink:[oldElement.id_u prevLink]]; - [newElement.id_u setNextLink:oldElement.id_u]; - [[oldElement.id_u prevLink] setNextLink:newElement.id_u]; - [oldElement.id_u setPrevLink:newElement.id_u]; + if (oldObject == _first_link) + _first_link = newObject; + [newObject setPrevLink: [oldObject prevLink]]; + [newObject setNextLink: oldObject]; + [[oldObject prevLink] setNextLink: newObject]; + [oldObject setPrevLink: newObject]; } _count++; - RETAIN_ELT(newElement); - return self; } -- (elt) replaceElement: (elt)oldElement with: (elt)newElement +- (void) replaceObject: oldObject with: newObject { - RETAIN_ELT(newElement); - if (oldElement.id_u == _first_link) - _first_link = newElement.id_u; - [newElement.id_u setNextLink:[oldElement.id_u nextLink]]; - [newElement.id_u setPrevLink:[oldElement.id_u prevLink]]; - [[oldElement.id_u prevLink] setNextLink:newElement.id_u]; - [[oldElement.id_u nextLink] setPrevLink:newElement.id_u]; - return AUTORELEASE_ELT(oldElement); + /* Make sure we actually own the oldObject. */ + assert ([oldObject linkedList] == self); + + /* Make sure no one else already owns the newObject. */ + assert ([newObject linkedList] == NO_OBJECT); + + /* Claim ownership of the newObject. */ + [newObject retain]; + [newObject setLinkedList: self]; + + /* Do the replacement. */ + if (oldObject == _first_link) + _first_link = newObject; + [newObject setNextLink:[oldObject nextLink]]; + [newObject setPrevLink:[oldObject prevLink]]; + [[oldObject prevLink] setNextLink:newObject]; + [[oldObject nextLink] setPrevLink:newObject]; + + /* Release ownership of the oldObject. */ + [oldObject setNextLink: NO_OBJECT]; + [oldObject setPrevLink: NO_OBJECT]; + [oldObject setLinkedList: NO_OBJECT]; + [oldObject release]; } /* End of methods that change the instance variables. */ + - -- appendElement: (elt)newElement +- (void) appendObject: newObject { - if (_count) - [self insertElement:newElement after:[self lastElement]]; + /* Make sure no one else already owns the newObject. */ + assert ([newObject linkedList] == NO_OBJECT); + + /* Claim ownership of the newObject. */ + [newObject retain]; + [newObject setLinkedList: self]; + + /* Insert it. */ + if (_count == 0) + { + _first_link = newObject; + _last_link = newObject; + [newObject setNextLink: NO_OBJECT]; + [newObject setPrevLink: NO_OBJECT]; + } else - [self insertElement:newElement after:nil]; - return self; + [self insertObject: newObject after: _last_link]; } -- prependElement: (elt)newElement +- prependElement: newObject { - [self insertElement:newElement before:_first_link]; - return self; + /* Make sure no one else already owns the newObject. */ + assert ([newObject linkedList] == NO_OBJECT); + + /* Claim ownership of the newObject. */ + [newObject retain]; + [newObject setLinkedList: self]; + + /* Insert it. */ + if (_count == 0) + { + _first_link = newObject; + _last_link = newObject; + [newObject setNextLink: NO_OBJECT]; + [newObject setPrevLink: NO_OBJECT]; + } + else + [self insertObject: newObject before: _first_link]; } -- insertElement: (elt)newElement atIndex: (unsigned)index +- insertElement: newObject atIndex: (unsigned)index { CHECK_INDEX_RANGE_ERROR(index, (_count+1)); - if (index == _count) - [self insertElement:newElement after:[self lastElement]]; + + /* Make sure no one else already owns the newObject. */ + assert ([newObject linkedList] == NO_OBJECT); + + /* Claim ownership of the newObject. */ + [newObject retain]; + [newObject setLinkedList: self]; + + /* Insert it. */ + if (_count == 0) + { + _first_link = newObject; + _last_link = newObject; + [newObject setNextLink: NO_OBJECT]; + [newObject setPrevLink: NO_OBJECT]; + } + else if (index == _count) + [self insertObject: newObject after: _last_link]; else - [self insertElement:newElement before:[self elementAtIndex:index]]; + [self insertObject:newObject before: [self objectAtIndex: index]]; return self; } -- (elt) removeElementAtIndex: (unsigned)index +- (void) removeObjectAtIndex: (unsigned)index { CHECK_INDEX_RANGE_ERROR(index, _count); - return [self removeElement:[self elementAtIndex:index]]; + [self removeObject: [self objectAtIndex: index]]; } -- (elt) elementAtIndex: (unsigned)index +- objectAtIndex: (unsigned)index { - id aLink; + id link; CHECK_INDEX_RANGE_ERROR(index, _count); + if (index < _count / 2) - for (aLink = _first_link; + for (link = _first_link; index; - aLink = [aLink nextLink], index--) + link = [link nextLink], index--) ; else - for (aLink = [_first_link prevLink], index = _count - index - 1; + for (link = _last_link, index = _count - index - 1; index; - aLink = [aLink prevLink], index--) + link = [link prevLink], index--) ; - return aLink; + return link; } -- (elt) firstElement +- firstObject { return _first_link; } -- (elt) lastElement +- lastObject { - if (_count) - return [_first_link prevLink]; - else - return NO_ELEMENT_FOUND_ERROR(); + return _last_link; } -- (elt) successorOfElement: (elt)oldElement +- successorOfObject: oldObject { - id nextElement = [oldElement.id_u nextLink]; - if (_first_link == nextElement) - return nil; - else - return (elt)nextElement; + /* Make sure we actually own the oldObject. */ + assert ([oldObject linkedList] == self); + + return [oldObject nextLink]; } -- (elt) predecessorOfElement: (elt)oldElement +- predecessorOfObject: oldObject { - if (_first_link == oldElement.id_u) - return nil; - else - return (elt)[oldElement.id_u prevLink]; + /* Make sure we actually own the oldObject. */ + assert ([oldObject linkedList] == self); + + return [oldObject prevLink]; } -- (BOOL) getNextElement:(elt *)anElementPtr withEnumState: (void**)enumState +- nextObjectWithEnumState: (void**)enumState { + /* *enumState points to the next object to be returned. */ + id ret; + if (*enumState == _first_link) - return NO; + return NO_OBJECT; else if (!(*enumState)) *enumState = _first_link; - *anElementPtr = *enumState; + ret = (id) *enumState; *enumState = [(id)(*enumState) nextLink]; - return YES; + return ret; } -- (BOOL) getPrevElement:(elt *)anElementPtr withEnumState: (void**)enumState +- prevObjectWithEnumState: (void**)enumState { - if (*enumState == _first_link) - return NO; - if (!(*enumState)) - *enumState = _first_link; + id ret; + + if (*enumState == _last_link) + return NO_OBJECT; + else if (!(*enumState)) + *enumState = _last_link; + ret = (id) *enumState; *enumState = [(id)(*enumState) prevLink]; - *anElementPtr = *enumState; - return YES; + return ret; } -- withElementsCall: (void(*)(elt))aFunc whileTrue:(BOOL *)flag -{ - id link; - unsigned i; - - for (link = _first_link, i = 0; - *flag && i < _count; - link = [link nextLink], i++) - { - (*aFunc)(link); - } - return self; -} - -- withElementsInReverseCall: (void(*)(elt))aFunc whileTrue:(BOOL *)flag -{ - id link; - unsigned i; - - if (!_first_link) - return self; - for (link = [_first_link prevLink], i = 0; - *flag && i < _count; - link = [link prevLink], i++) - { - (*aFunc)(link); - } - return self; -} - - - (unsigned) count { return _count; diff --git a/Source/LinkedListNode.m b/Source/LinkedListNode.m index cd4648dfb..61cbde533 100644 --- a/Source/LinkedListNode.m +++ b/Source/LinkedListNode.m @@ -1,8 +1,8 @@ /* Implementation for Objective-C LinkedListNode object - Copyright (C) 1993,1994, 1995 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1995, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum - Date: May 1993 + Created: May 1993 This file is part of the GNU Objective C Class Library. @@ -44,6 +44,7 @@ [super encodeWithCoder:aCoder]; [aCoder encodeObjectReference:_next withName:@"Next LinkedList Node"]; [aCoder encodeObjectReference:_prev withName:@"Prev LinkedList Node"]; + [aCoder encodeObjectReference:_linked_list withName:@"LinkedList"]; } - initWithCoder: aCoder @@ -51,6 +52,7 @@ [super initWithCoder:aCoder]; [aCoder decodeObjectAt:&_next withName:NULL]; [aCoder decodeObjectAt:&_prev withName:NULL]; + [aCoder decodeObjectAt:&_linked_list withName:NULL]; return self; } @@ -64,17 +66,24 @@ return _prev; } -- setNextLink: (id )aLink +- (void) setNextLink: (id )aLink { _next = aLink; - return self; } -- setPrevLink: (id )aLink +- (void) setPrevLink: (id )aLink { _prev = aLink; - return self; +} + +- linkedList +{ + return _linked_list; +} + +- (void) setLinkedList: anObject; +{ + _linked_list = anObject; } @end - diff --git a/Source/MappedCollector.m b/Source/MappedCollector.m index e02694be0..8e1566b24 100644 --- a/Source/MappedCollector.m +++ b/Source/MappedCollector.m @@ -1,8 +1,8 @@ /* Implementation for Objective-C MappedCollector collection object - Copyright (C) 1993,1994, 1995 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1995, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum - Date: May 1993 + Created: May 1993 This file is part of the GNU Objective C Class Library. @@ -28,13 +28,9 @@ @implementation MappedCollector /* This is the designated initializer for this class */ -- initCollection: (id )aDomain +- initWithCollection: (id )aDomain map: (id )aMap { - if (strcmp([aMap contentType], [aDomain keyType])) - [self error:"map's contents are not the same as domain's keys"]; - [super initWithType:[aDomain contentType] - keyType:[aMap keyType]]; _map = aMap; _domain = aDomain; return self; @@ -53,20 +49,10 @@ return self; } -- _writeInit: (TypedStream*)aStream +/* Override our superclass' designated initializer */ +- initWithObjects: (id*)objects forKeys: (id*)keys count: (unsigned)c { - [super _writeInit: aStream]; - objc_write_object(aStream, _map); - objc_write_object(aStream, _domain); - return self; -} - -- _readInit: (TypedStream*)aStream -{ - [super _readInit: aStream]; - objc_read_object(aStream, &_map); - objc_read_object(aStream, &_domain); - return self; + [self notImplemented: _cmd]; } /* Empty copy must empty an allocCopy'ed version of self */ @@ -79,83 +65,56 @@ } /* This must work without sending any messages to content objects */ -- empty +- (void) empty { [_domain empty]; +} + +- objectAtKey: aKey +{ + return [_domain objectAtKey: [_map objectAtKey: aKey]]; +} + +- keyOfObject: aContentObject +{ + [self notImplemented: _cmd]; return self; } -- (const char *) contentType +- (void) replaceObjectAtKey: aKey with: newObject { - return [_domain contentType]; + return [_domain replaceObjectAtKey: [_map objectAtKey: aKey] + with: newObject]; } -- (const char *) keyType +- (void) putObject: newObject atKey: aKey { - return [_map keyType]; + return [_domain putObject: newObject + atKey: [_map objectAtKey:aKey]]; } -- (int(*)(elt,elt)) comparisonFunction +- (void) removeObjectAtKey: aKey { - return [_domain comparisonFunction]; + return [_domain removeObjectAtKey: [_map objectAtKey: aKey]]; } -- (elt) elementAtKey: (elt)aKey +- (BOOL) containsKey: aKey { - return [_domain elementAtKey:[_map elementAtKey:aKey]]; + return [_domain containsKey: [_map objectAtKey:aKey]]; } -- (elt) replaceElementAtKey: (elt)aKey with: (elt)newElement +- nextObjectAndKey: (id*)keyPtr withEnumState: (void**)enumState { - return [_domain replaceElementAtKey:[_map elementAtKey:aKey] - with:newElement]; -} + id mapContent; + id domainKey; -- putElement: (elt)newElement atKey: (elt)aKey -{ - return [_domain putElement:newElement - atKey:[_map elementAtKey:aKey]]; -} - -- (elt) removeElementAtKey: (elt)aKey -{ - return [_domain removeElementAtKey:[_map elementAtKey:aKey]]; -} - -- (BOOL) includesKey: (elt)aKey -{ - return [_domain includesKey:[_map elementAtKey:aKey]]; -} - -- withKeyElementsAndContentElementsCall: (void(*)(const elt,elt))aFunc - whileTrue: (BOOL *)flag -{ - void doIt(elt e) - { - elt domainKey = [_map elementAtKey:e]; - if ([_domain includesKey:domainKey]) - (*aFunc)(e, [_domain elementAtKey:domainKey]); - } - [_map withKeyElementsCall:doIt]; - return self; -} - -- (BOOL) getNextKey: (elt*)aKeyPtr content: (elt*)anElementPtr - withEnumState: (void**)enumState; -{ - BOOL ret; - elt mapContent; - elt domainKey; - - while ((ret = [_map getNextKey:aKeyPtr content:&mapContent - withEnumState:enumState]) + while ((mapContent = [_map nextObjectAndKey:keyPtr withEnumState:enumState]) && - (![_domain includesKey:(domainKey = [_map elementAtKey:*aKeyPtr])])) + (![_domain containsKey: (domainKey = [_map objectAtKey:*keyPtr])])) ; - if (!ret) - return NO; - *anElementPtr = [_domain elementAtKey:domainKey]; - return YES; + if (mapContent == NO_OBJECT) + return NO_OBJECT; + return [_domain objectAtKey: domainKey]; } - species diff --git a/Source/Queue.m b/Source/Queue.m index af606a06c..367d225ff 100644 --- a/Source/Queue.m +++ b/Source/Queue.m @@ -1,8 +1,8 @@ /* Implementation for Objective-C Queue object - Copyright (C) 1993,1994 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum - Date: May 1993 + Created: May 1993 This file is part of the GNU Objective C Class Library. @@ -26,40 +26,22 @@ @implementation Queue -+ (void) initialize +- (void) enqueueObject: newObject { - if (self == [Queue class]) - [self setVersion:0]; /* beta release */ -} - -- enqueueElement: (elt)newElement -{ - [self prependElement:newElement]; - return self; -} - -/* Overriding */ -- addElement: (elt)anElement -{ - [self enqueueElement:anElement]; - return self; -} - -- (elt) dequeueElement -{ - return [self removeLastElement]; -} - -// OBJECT-COMPATIBLE MESSAGE NAMES; - -- enqueueObject: newObject -{ - return [self enqueueElement:newObject]; + [self prependObject: newObject]; } - dequeueObject { - return [self dequeueElement].id_u; + id ret = [[self lastObject] retain]; + [self removeLastObject]; + return [ret autorelease]; +} + +/* Overriding */ +- (void) addObject: newObject +{ + [self enqueueObject: newObject]; } @end diff --git a/Source/RBTree.m b/Source/RBTree.m index da0f1d54d..e470d4469 100644 --- a/Source/RBTree.m +++ b/Source/RBTree.m @@ -1,5 +1,5 @@ /* Implementation for Objective-C Red-Black Tree collection object - Copyright (C) 1993,1994 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum Date: May 1993 @@ -37,7 +37,6 @@ static id nilRBNode; { if (self == [RBTree class]) { - [self setVersion:0]; /* beta release */ nilRBNode = [[RBTreeNode alloc] init]; [nilRBNode setBlack]; } @@ -48,68 +47,67 @@ static id nilRBNode; return nilRBNode; } -- sortAddElement: (elt)newElement byCalling: (int(*)(elt,elt))aFunc +- (void) sortAddObject: newObject { id y; - [super sortAddElement:newElement byCalling:aFunc]; - [newElement.id_u setRed]; - while (newElement.id_u != _contents_root - && [[newElement.id_u parentNode] isRed]) + [super sortAddObject: newObject]; + [newObject setRed]; + while (newObject != _contents_root + && [[newObject parentNode] isRed]) { - if ([newElement.id_u parentNode] == - [[[newElement.id_u parentNode] parentNode] leftNode]) + if ([newObject parentNode] == + [[[newObject parentNode] parentNode] leftNode]) { - y = [[[newElement.id_u parentNode] parentNode] leftNode]; + y = [[[newObject parentNode] parentNode] leftNode]; if ([y isRed]) { - [[newElement.id_u parentNode] setBlack]; + [[newObject parentNode] setBlack]; [y setBlack]; - [[[newElement.id_u parentNode] parentNode] setRed]; - newElement.id_u = [[newElement.id_u parentNode] parentNode]; + [[[newObject parentNode] parentNode] setRed]; + newObject = [[newObject parentNode] parentNode]; } else { - if (newElement.id_u == [[newElement.id_u parentNode] rightNode]) + if (newObject == [[newObject parentNode] rightNode]) { - newElement.id_u = [newElement.id_u parentNode]; - [self leftRotateAroundNode:newElement.id_u]; + newObject = [newObject parentNode]; + [self leftRotateAroundNode:newObject]; } - [[newElement.id_u parentNode] setBlack]; - [[[newElement.id_u parentNode] parentNode] setRed]; + [[newObject parentNode] setBlack]; + [[[newObject parentNode] parentNode] setRed]; [self rightRotateAroundNode: - [[newElement.id_u parentNode] parentNode]]; + [[newObject parentNode] parentNode]]; } } else { - y = [[[newElement.id_u parentNode] parentNode] rightNode]; + y = [[[newObject parentNode] parentNode] rightNode]; if ([y isRed]) { - [[newElement.id_u parentNode] setBlack]; + [[newObject parentNode] setBlack]; [y setBlack]; - [[[newElement.id_u parentNode] parentNode] setRed]; - newElement.id_u = [[newElement.id_u parentNode] parentNode]; + [[[newObject parentNode] parentNode] setRed]; + newObject = [[newObject parentNode] parentNode]; } else { - if (newElement.id_u == [[newElement.id_u parentNode] leftNode]) + if (newObject == [[newObject parentNode] leftNode]) { - newElement.id_u = [newElement.id_u parentNode]; - [self rightRotateAroundNode:newElement.id_u]; + newObject = [newObject parentNode]; + [self rightRotateAroundNode:newObject]; } - [[newElement.id_u parentNode] setBlack]; - [[[newElement.id_u parentNode] parentNode] setRed]; + [[newObject parentNode] setBlack]; + [[[newObject parentNode] parentNode] setRed]; [self leftRotateAroundNode: - [[newElement.id_u parentNode] parentNode]]; + [[newObject parentNode] parentNode]]; } } } [_contents_root setBlack]; - return self; } -- _RBTreeDeleteFixup: x +- (void) _RBTreeDeleteFixup: x { id w; @@ -185,18 +183,17 @@ static id nilRBNode; } } [x setBlack]; - return self; } -- (elt) removeElement: (elt)oldElement +- (void) removeObject: oldObject { id x, y; - if ([oldElement.id_u leftNode] == [self nilNode] - || [oldElement.id_u rightNode] == [self nilNode]) - y = oldElement.id_u; + if ([oldObject leftNode] == [self nilNode] + || [oldObject rightNode] == [self nilNode]) + y = oldObject; else - y = [self successorOfElement:oldElement].id_u; + y = [self successorOfObject: oldObject]; if ([y leftNode] != [self nilNode]) x = [y leftNode]; @@ -215,63 +212,38 @@ static id nilRBNode; [[y parentNode] setRightNode:x]; } - if (y != oldElement.id_u) + if (y != oldObject) { - /* put y in the place of oldElement.id_u */ - [y setParentNode:[oldElement.id_u parentNode]]; - [y setLeftNode:[oldElement.id_u leftNode]]; - [y setRightNode:[oldElement.id_u rightNode]]; - if (oldElement.id_u == [[oldElement.id_u parentNode] leftNode]) - [[oldElement.id_u parentNode] setLeftNode:y]; + /* put y in the place of oldObject */ + [y setParentNode:[oldObject parentNode]]; + [y setLeftNode:[oldObject leftNode]]; + [y setRightNode:[oldObject rightNode]]; + if (oldObject == [[oldObject parentNode] leftNode]) + [[oldObject parentNode] setLeftNode:y]; else - [[oldElement.id_u parentNode] setRightNode:oldElement.id_u]; - [[oldElement.id_u leftNode] setParentNode:y]; - [[oldElement.id_u rightNode] setParentNode:y]; + [[oldObject parentNode] setRightNode:oldObject]; + [[oldObject leftNode] setParentNode:y]; + [[oldObject rightNode] setParentNode:y]; } if (NODE_IS_BLACK(y)) [self _RBTreeDeleteFixup:x]; - [oldElement.id_u setRightNode:[self nilNode]]; - [oldElement.id_u setLeftNode:[self nilNode]]; - [oldElement.id_u setParentNode:[self nilNode]]; _count--; - return AUTORELEASE_ELT(oldElement); + + /* Release ownership of the object. */ +#if 0 + [oldObject setRightNode: [self nilNode]]; + [oldObject setLeftNode: [self nilNode]]; + [oldObject setParentNode: [self nilNode]]; +#else + [oldObject setLeftNode: NO_OBJECT]; + [oldObject setRightNode: NO_OBJECT]; + [oldObject setParentNode: NO_OBJECT]; +#endif + [oldObject setBinaryTree: NO_OBJECT]; + [oldObject release]; } -/* Override methods that could violate assumptions of RBTree structure. - Perhaps I shouldn't DISALLOW this, let users have the power to do - whatever they want. I mention this in the QUESTIONS section of the - TODO file. */ - -/*** -Or perhaps instead of calling INSERTION_ERROR we could fix up the RB -property of the tree. - -- insertElement: (elt)newElement before: (elt)oldElement -{ - INSERTION_ERROR(); - return self; -} - -- insertElement: (elt)newElement after: (elt)oldElement -{ - INSERTION_ERROR(); - return self; -} - -- insertElement: (elt)newElement atIndex: (unsigned)index -{ - INSERTION_ERROR(); - return self; -} - -- appendElement: (elt)newElement -{ - INSERTION_ERROR(); - return self; -} -****/ - @end diff --git a/Source/Set.m b/Source/Set.m index 2b491d1a8..a56075407 100644 --- a/Source/Set.m +++ b/Source/Set.m @@ -24,17 +24,12 @@ #include #include #include +#include #define DEFAULT_SET_CAPACITY 32 @implementation Set -+ (void) initialize -{ - if (self == [Set class]) - [self setVersion:0]; /* beta release */ -} - // MANAGING CAPACITY; /* Eventually we will want to have better capacity management, @@ -48,75 +43,28 @@ // INITIALIZING AND FREEING; /* This is the designated initializer of this class */ -- initWithType:(const char *)encoding - capacity: (unsigned)aCapacity +- initWithCapacity: (unsigned)cap { - [super initWithType:encoding]; - _contents_hash = - coll_hash_new(POWER_OF_TWO(aCapacity), - elt_get_hash_function(encoding), - elt_get_comparison_function(encoding)); + _contents_hash = NSCreateHashTable(NSObjectsHashCallBacks, cap); + return self; +} + +/* Override Collection's designated initializer */ +- initWithObjects: (id*)objs count: (unsigned)count +{ + [self initWithCapacity: count]; + while (count--) + [self addObject: objs[count]]; return self; } /* Archiving must mimic the above designated initializer */ -- (void) _encodeCollectionWithCoder: (Coder*) aCoder -{ - const char *enc = [self contentType]; - - [super _encodeCollectionWithCoder:aCoder]; - [aCoder encodeValueOfCType:@encode(char*) - at:&enc - withName:@"Set contents encoding"]; - [aCoder encodeValueOfCType:@encode(unsigned) - at:&(_contents_hash->size) - withName:@"Set contents capacity"]; - return; -} - - _initCollectionWithCoder: aCoder { - char *encoding; - unsigned size; - [super _initCollectionWithCoder:aCoder]; - [aCoder decodeValueOfCType:@encode(char*) - at:&encoding - withName:NULL]; - [aCoder decodeValueOfCType:@encode(unsigned) - at:&size - withName:NULL]; - _contents_hash = - coll_hash_new(size, - elt_get_hash_function(encoding), - elt_get_comparison_function(encoding)); - return self; -} - -- _writeInit: (TypedStream*)aStream -{ - const char *encoding = [self contentType]; - - [super _writeInit:aStream]; - /* This implicitly archives the key's comparison and hash functions */ - objc_write_type(aStream, @encode(char*), &encoding); - objc_write_type(aStream, @encode(unsigned int), &(_contents_hash->size)); - return self; -} - -- _readInit: (TypedStream*)aStream -{ - char *encoding; - unsigned int size; - - [super _readInit:aStream]; - objc_read_type(aStream, @encode(char*), &encoding); - objc_read_type(aStream, @encode(unsigned int), &size); - _contents_hash = - coll_hash_new(size, - elt_get_hash_function(encoding), - elt_get_comparison_function(encoding)); + _contents_hash = NSCreateHashTable(NSObjectsHashCallBacks, + DEFAULT_SET_CAPACITY); return self; } @@ -125,52 +73,38 @@ { Set *copy = [super emptyCopy]; copy->_contents_hash = - coll_hash_new(_contents_hash->size, - _contents_hash->hash_func, - _contents_hash->compare_func); + NSCreateHashTable (NSObjectsHashCallBacks, 0); return copy; } -/* Override designated initializer of superclass */ -- initWithType:(const char *)contentEncoding +- (void) dealloc { - return [self initWithType:contentEncoding - capacity:[[self class] defaultCapacity]]; -} - -- initWithCapacity: (unsigned)aCapacity -{ - return [self initWithType:@encode(id) capacity:aCapacity]; -} - -- (void) _collectionDealloc -{ - coll_hash_delete(_contents_hash); + NSFreeHashTable (_contents_hash); [super _collectionDealloc]; } // SET OPERATIONS; -- intersectWithCollection: (id )aCollection +- (void) intersectWithCollection: (id )aCollection { - [self removeContentsNotIn:aCollection]; - return self; + [self removeContentsNotIn: aCollection]; } -- unionWithCollection: (id )aCollection +- (void) unionWithCollection: (id )aCollection { - [self addContentsOfIfAbsent:aCollection]; - return self; + [self addContentsIfAbsentOf: aCollection]; } -- differenceWithCollection: (id )aCollection +- (void) differenceWithCollection: (id )aCollection { - [self removeContentsIn:aCollection]; - return self; + [self removeContentsIn: aCollection]; } - shallowCopyIntersectWithCollection: (id )aCollection { + [self notImplemented: _cmd]; + return nil; +#if 0 id newColl = [self emptyCopyAs:[self species]]; void doIt(elt e) { @@ -179,18 +113,25 @@ } [self withElementsCall:doIt]; return newColl; +#endif } - shallowCopyUnionWithCollection: (id )aCollection { + [self notImplemented: _cmd]; + return nil; +#if 0 id newColl = [self shallowCopy]; - [newColl addContentsOf:aCollection]; return newColl; +#endif } - shallowCopyDifferenceWithCollection: (id )aCollection { + [self notImplemented: _cmd]; + return nil; +#if 0 id newColl = [self emptyCopyAs:[self species]]; void doIt(elt e) { @@ -199,128 +140,78 @@ } [self withElementsCall:doIt]; return newColl; +#endif } - + // ADDING; -- addElement: (elt)anElement +- (void) addObject: newObject { - if (coll_hash_value_for_key(_contents_hash, anElement).void_ptr_u == 0) - coll_hash_add(&_contents_hash, anElement, 1); - RETAIN_ELT(anElement); - return self; + NSHashInsert (_contents_hash, newObject); } - + // REMOVING AND REPLACING; -- (elt) removeElement: (elt)oldElement ifAbsentCall: (elt(*)(arglist_t))excFunc +- (void) removeObject: oldObject { - if (coll_hash_value_for_key(_contents_hash, oldElement).void_ptr_u == 0) - coll_hash_remove(_contents_hash, oldElement); - else - RETURN_BY_CALLING_EXCEPTION_FUNCTION(excFunc); - return AUTORELEASE_ELT(oldElement); + NSHashRemove (_contents_hash, oldObject); } /* This must work without sending any messages to content objects */ -- _empty +- (void) _collectionEmpty { - coll_hash_empty(_contents_hash); - return self; + NSResetHashTable (_contents_hash); } -- uniqueContents +- (void) uniqueContents { - return self; + return; } - + // TESTING; -- (int(*)(elt,elt)) comparisonFunction +- (BOOL) containsObject: anObject { - return _contents_hash->compare_func; -} - -- (const char *) contentType -{ - return elt_get_encoding(_contents_hash->compare_func); -} - -- (BOOL) includesElement: (elt)anElement -{ - if (coll_hash_value_for_key(_contents_hash, anElement).void_ptr_u != 0) - return YES; - else - return NO; + return (NSHashGet (_contents_hash, anObject) ? 1 : 0); } - (unsigned) count { - return _contents_hash->used; + return NSCountHashTable (_contents_hash); } -- (unsigned) occurrencesOfElement: (elt)anElement +- (unsigned) occurrencesOfObject: anObject { - if ([self includesElement:anElement]) + if ([self containsObject: anObject]) return 1; else return 0; } - + // ENUMERATING; -- (BOOL) getNextElement:(elt *)anElementPtr withEnumState: (void**)enumState +- nextObjectWithEnumState: (void**)enumState { - coll_node_ptr node = coll_hash_next(_contents_hash, enumState); - if (node) - { - *anElementPtr = node->key; - return YES; - } - return NO; + return NSNextHashEnumeratorItem (((NSHashEnumerator*)enumState)); } - (void*) newEnumState { - return (void*)0; + void *es; + + OBJC_MALLOC (es, NSMapEnumerator, 1); + *((NSHashEnumerator*)es) = NSEnumerateHashTable (_contents_hash); + return es; } -- freeEnumState: (void**)enumState +- (void) freeEnumState: (void**)enumState { - /* Yipes, this interface is ugly. collhash:coll_hash_next malloc'ed this */ - if (*enumState) - OBJC_FREE(*enumState); - return self; + OBJC_FREE (*enumState); } -- withElementsCall: (void(*)(elt))aFunc whileTrue:(BOOL *)flag -{ - void *state = 0; - coll_node_ptr node; - - while (*flag && (node = coll_hash_next(_contents_hash, &state))) - { - (*aFunc)(node->key); - } - return self; -} - -- withElementsCall: (void(*)(elt))aFunc -{ - void *state = 0; - coll_node_ptr node = 0; - - while ((node = coll_hash_next(_contents_hash, &state))) - { - (*aFunc)(node->key); - } - return self; -} - - @end diff --git a/Source/SplayTree.m b/Source/SplayTree.m index 11c0f0b9c..a7474748e 100644 --- a/Source/SplayTree.m +++ b/Source/SplayTree.m @@ -1,5 +1,5 @@ /* Implementation for Objective-C SplayTree collection object - Copyright (C) 1993,1994 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum Date: May 1993 @@ -26,14 +26,8 @@ @implementation SplayTree -+ (void) initialize -{ - if (self == [SplayTree class]) - [self setVersion:0]; /* beta release */ -} - /* Make this a function ? */ -- _doSplayOperationOnNode: aNode +- (void) _doSplayOperationOnNode: aNode { id parent = [aNode parentNode]; id parentRightChild = @@ -41,7 +35,7 @@ if (aNode == _contents_root || aNode == [self nilNode]) { - return self; + return; } else if (aNode == parentRightChild) { @@ -80,53 +74,28 @@ [self leftRotateAroundNode:[aNode parentNode]]; } } - return self; } -- splayNode: aNode +- (void) splayNode: aNode { while (aNode != _contents_root) [self _doSplayOperationOnNode:aNode]; - return self; } /* We could make this a little more efficient by doing the splay as we search down the tree for the correct insertion point. */ -- sortAddElement: (elt)newElement byCalling: (int(*)(elt,elt))aFunc +- (void) sortAddObject: newObject { - [super sortAddElement:newElement byCalling:aFunc]; - [self splayNode:newElement.id_u]; - return self; + [super sortAddObject: newObject]; + [self splayNode: newObject]; } -- insertElement: (elt)newElement before: (elt)oldElement +- (void) removeObject: anObject { - [super insertElement:newElement before:oldElement]; - // ?? [self splayNode:newElement.id_u]; - return self; -} - -- insertElement: (elt)newElement after: (elt)oldElement -{ - [super insertElement:newElement after:oldElement]; - // ?? [self splayNode:newElement.id_u]; - return self; -} - -- insertElement: (elt)newElement atIndex: (unsigned)index -{ - [super insertElement:newElement atIndex:index]; - // ?? [self splayNode:newElement.id_u]; - return self; -} - -- (elt) removeElement: (elt)anElement -{ - id parent = [anElement.id_u parentNode]; - [super removeElement:anElement]; + id parent = [anObject parentNode]; + [super removeObject: anObject]; if (parent && parent != [self nilNode]) [self splayNode:parent]; - return AUTORELEASE_ELT(anElement); } @end diff --git a/Source/Stack.m b/Source/Stack.m index c90d6c72f..714926c95 100644 --- a/Source/Stack.m +++ b/Source/Stack.m @@ -1,8 +1,8 @@ /* Implementation for Objective-C Stack object - Copyright (C) 1993,1994 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum - Date: May 1993 + Created: May 1993 This file is part of the GNU Objective C Class Library. @@ -26,65 +26,40 @@ @implementation Stack -+ (void) initialize +- (void) pushObject: newObject { - if (self == [Stack class]) - [self setVersion:0]; /* beta release */ -} - -- pushElement: (elt)anElement -{ - [self appendElement:anElement]; - return self; + [self appendObject: newObject]; } /* Overriding */ -- addElement: (elt)anElement +- (void) addObject: newObject { - [self pushElement:anElement]; - return self; -} - -- (elt) popElement -{ - return [self removeLastElement]; -} - -- (elt) topElement -{ - return [self lastElement]; -} - -/* Yipes. What copying semantics do we want here? */ -- duplicateTop -{ - [self pushElement:[self topElement]]; - return self; -} - -- exchangeTop -{ - if (_count <= 1) - return nil; - [self swapAtIndeces:_count-1 :_count-2]; - return self; -} - -// OBJECT-COMPATIBLE MESSAGE NAMES; - -- pushObject: anObject -{ - return [self pushElement:anObject]; + [self pushObject: newObject]; } - popObject { - return [self popElement].id_u; + id ret; + ret = [[self lastObject] retain]; + [self removeLastObject]; + return [ret autorelease]; } - topObject { - return [self topElement].id_u; + return [self lastObject]; +} + +/* xxx Yipes. What copying semantics do we want here? */ +- (void) duplicateTop +{ + [self pushObject: [self topObject]]; +} + +- (void) exchangeTop +{ + if (_count > 1) + [self swapAtIndeces:_count-1 :_count-2]; } @end diff --git a/Source/objects/Array.h b/Source/objects/Array.h index 6f49482d0..63e1ff8c8 100644 --- a/Source/objects/Array.h +++ b/Source/objects/Array.h @@ -1,5 +1,5 @@ /* Interface for Objective-C Array collection object - Copyright (C) 1993,1994 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum Date: May 1993 @@ -26,13 +26,19 @@ #include #include +#include -@interface Array : IndexedCollection +@interface ConstantArray : IndexedCollection { @public - int (*_comparison_function)(elt,elt); - elt *_contents_array; + id *_contents_array; unsigned int _count; +} +@end + +@interface Array : ConstantArray +{ + @public unsigned int _capacity; unsigned int _grow_factor; } @@ -40,16 +46,18 @@ + (unsigned) defaultCapacity; + (unsigned) defaultGrowFactor; -- initWithType: (const char *)contentEncoding - capacity: (unsigned)aCapacity; - initWithCapacity: (unsigned) aCapacity; -- setCapacity: (unsigned)newCapacity; +- (void) setCapacity: (unsigned)newCapacity; - (unsigned) growFactor; -- setGrowFactor: (unsigned)aNum; +- (void) setGrowFactor: (unsigned)aNum; @end +/* Put this on category instead of class to avoid bogus complaint from gcc */ +@interface Array (Ordering) +@end + #define FOR_ARRAY(ARRAY, ELEMENT_VAR) \ { \ unsigned _FOR_ARRAY_i; \ diff --git a/Source/objects/Bag.h b/Source/objects/Bag.h index 4e6a6e679..03142030f 100644 --- a/Source/objects/Bag.h +++ b/Source/objects/Bag.h @@ -1,8 +1,8 @@ /* Interface for Objective-C Bag collection object - Copyright (C) 1993,1994 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum - Date: May 1993 + Created: May 1993 This file is part of the GNU Objective C Class Library. @@ -25,39 +25,27 @@ #define __Bag_h_INCLUDE_GNU #include -#include +#include +#include -@interface Bag : Set +@interface Bag : Collection { + NSMapTable *_contents_map; unsigned int _count; // the number of elements; } +// INITIALIZING AND FREEING; +- initWithCapacity: (unsigned)aCapacity; + // ADDING; -- addObject: newObject withOccurrences: (unsigned)count; +- (void) addObject: newObject withOccurrences: (unsigned)count; // REMOVING AND REPLACING; -- removeObject: oldObject occurrences: (unsigned)count; -- removeObject: oldObject occurrences: (unsigned)count - ifAbsentCall: (id(*)(arglist_t))excFunc; +- (void) removeObject: oldObject occurrences: (unsigned)count; // TESTING; - (unsigned) uniqueCount; - -// NON-OBJECT ELEMENT METHOD NAMES; - -// INITIALIZING AND FREEING; -- initWithType: (const char *)contentEncoding - capacity: (unsigned)aCapacity; - -// ADDING; -- addElement: (elt)newElement withOccurrences: (unsigned)count; - -// REMOVING AND REPLACING; -- (elt) removeElement:(elt)oldElement occurrences: (unsigned)count; -- (elt) removeElement:(elt)oldElement occurrences: (unsigned)count - ifAbsentCall: (elt(*)(arglist_t))excFunc; - @end #endif /* __Bag_h_INCLUDE_GNU */ diff --git a/Source/objects/BinaryTree.h b/Source/objects/BinaryTree.h index ef35e54cd..168be9fed 100644 --- a/Source/objects/BinaryTree.h +++ b/Source/objects/BinaryTree.h @@ -1,8 +1,8 @@ /* Interface for Objective-C BinaryTree collection object - Copyright (C) 1993,1994 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum - Date: May 1993 + Created: May 1993 This file is part of the GNU Objective C Class Library. @@ -35,13 +35,15 @@ /* The protocol defines the interface to an object that may be an element in a BinaryTree. */ -@protocol BinaryTreeComprising +@protocol BinaryTreeComprising - leftNode; - rightNode; - parentNode; -- setLeftNode: (id )aNode; -- setRightNode: (id )aNode; -- setParentNode: (id )aNode; +- (void) setLeftNode: (id )aNode; +- (void) setRightNode: (id )aNode; +- (void) setParentNode: (id )aNode; +- binaryTree; +- (void) setBinaryTree: anObject; @end #define NODE_IS_RIGHTCHILD(NODE) (NODE == [[NODE parentNode] rightNode]) diff --git a/Source/objects/BinaryTreeNode.h b/Source/objects/BinaryTreeNode.h index 0dfab985c..a04708fee 100644 --- a/Source/objects/BinaryTreeNode.h +++ b/Source/objects/BinaryTreeNode.h @@ -1,8 +1,8 @@ /* Interface for Objective-C BinaryTreeNode object - Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1995, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum - Date: May 1993 + Created: May 1993 This file is part of the GNU Objective C Class Library. @@ -33,6 +33,7 @@ id _left; id _right; id _parent; + id _binary_tree; } @end diff --git a/Source/objects/Collecting.h b/Source/objects/Collecting.h index 1e726627f..0c723ddc7 100644 --- a/Source/objects/Collecting.h +++ b/Source/objects/Collecting.h @@ -1,8 +1,8 @@ /* Protocol for Objective-C objects that hold collections of elements. - Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1995, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum - Date: May 1993 + Created: May 1993 This file is part of the GNU Objective C Class Library. @@ -35,54 +35,61 @@ #define __Collecting_h_INCLUDE_GNU #include -#include -#include +#include +#include +#include @protocol ConstantCollecting // INITIALIZING; - init; +- initWithObjects: (id*)objc count: (unsigned)c; +- initWithObjects: firstObject, ...; +- initWithObjects: firstObject rest: (va_list)ap; - initWithContentsOf: (id )aCollection; -// TESTING; +// QUERYING COUNTS; - (BOOL) isEmpty; -- (BOOL) includesObject: anObject; +- (unsigned) count; +- (BOOL) containsObject: anObject; +- (unsigned) occurrencesOfObject: anObject; + +// COMPARISON WITH OTHER COLLECTIONS; - (BOOL) isSubsetOf: (id )aCollection; - (BOOL) isDisjointFrom: (id )aCollection; -- (int) compare: anObject; - (BOOL) isEqual: anObject; +- (int) compare: anObject; - (BOOL) contentsEqual: (id )aCollection; -- (unsigned) count; -- (unsigned) occurrencesOfObject: anObject; -- (BOOL) trueForAllObjectsByCalling: (BOOL(*)(id))aFunc; -- (BOOL) trueForAnyObjectsByCalling: (BOOL(*)(id))aFunc; -- detectObjectByCalling: (BOOL(*)(id))aFunc; -- detectObjectByCalling: (BOOL(*)(id))aFunc - ifNoneCall: (id(*)(arglist_t))excFunc; + +// PROPERTIES OF CONTENTS; +- (BOOL) trueForAllObjectsByInvoking: (id )anInvocation; +- (BOOL) trueForAnyObjectsByInvoking: (id )anInvocation; +- detectObjectByInvoking: (id )anInvocation; - maxObject; -- maxObjectByCalling: (int(*)(id,id))aFunc; - minObject; -- minObjectByCalling: (int(*)(id,id))aFunc; // ENUMERATING -- (void*) newEnumState; -- (BOOL) getNextObject:(id *)anObjectPtr withEnumState: (void**)enumState; -- freeEnumState: (void**)enumState; -- withObjectsCall: (void(*)(id))aFunc; -- withObjectsCall: (void(*)(id))aFunc whileTrue:(BOOL *)flag; -- injectObject: initialArgObject byCalling:(id(*)(id,id))aFunc; -- makeObjectsPerform: (SEL)aSel; -- makeObjectsPerform: (SEL)aSel with: argObject; +- (id ) objectEnumerator; +- (void) withObjectsInvoke: (id )anInvocation; +- (void) withObjectsInvoke: (id )anInvocation whileTrue:(BOOL *)flag; +- (void) makeObjectsPerform: (SEL)aSel; +- (void) makeObjectsPerform: (SEL)aSel withObject: argObject; // FILTERED ENUMERATING; -- withObjectsTrueByCalling: (BOOL(*)(id))testFunc - call: (void(*)(id))destFunc; -- withObjectsFalseByCalling: (BOOL(*)(id))testFunc - call: (void(*)(id))destFunc; -- withObjectsTransformedByCalling: (id(*)(id))transFunc - call: (void(*)(id))destFunc; +- (void) withObjectsTrueByInvoking: (id )testInvocation + invoke: (id )anInvocation; +- (void) withObjectsFalseByInvoking: (id )testInvocation + invoke: (id )anInvocation; +- (void) withObjectsTransformedByInvoking: (id )transInvocation + invoke: (id )anInvocation; -// COPYING +// LOW-LEVEL ENUMERATING; +- (void*) newEnumState; +- nextObjectWithEnumState: (void**)enumState; +- (void) freeEnumState: (void**)enumState; + +// COPYING; +- allocCopy; - emptyCopy; - emptyCopyAs: (Class)aCollectionClass; - shallowCopy; @@ -91,98 +98,34 @@ - copyAs: (Class)aCollectionClass; - species; -// NON-OBJECT ELEMENT METHOD NAMES; - -// INITIALIZING; -- initWithType:(const char *)contentEncoding; - -// TESTING; -- (BOOL) includesElement: (elt)anElement; -- (unsigned) occurrencesOfElement: (elt)anElement; -- (elt) detectElementByCalling: (BOOL(*)(elt))aFunc; -- (elt) detectElementByCalling: (BOOL(*)(elt))aFunc - ifNoneCall: (elt(*)(arglist_t))excFunc; -- (elt) maxElement; -- (elt) maxElementByCalling: (int(*)(elt,elt))aFunc; -- (elt) minElement; -- (elt) minElementByCalling: (int(*)(elt,elt))aFunc; -- (BOOL) trueForAllElementsByCalling: (BOOL(*)(elt))aFunc; -- (BOOL) trueForAnyElementsByCalling: (BOOL(*)(elt))aFunc; -- (const char *) contentType; -- (BOOL) contentsAreObjects; -- (int(*)(elt,elt)) comparisonFunction; - -// ENUMERATING; -- (BOOL) getNextElement:(elt *)anElementPtr withEnumState: (void**)enumState; -- withElementsCall: (void(*)(elt))aFunc; -- withElementsCall: (void(*)(elt))aFunc whileTrue: (BOOL*)flag; -- (elt) injectElement: (elt)initialElement byCalling: (elt(*)(elt,elt))aFunc; - -// FILTERED ENUMERATING; -- withElementsTrueByCalling: (BOOL(*)(elt))testFunc - call: (void(*)(elt))destFunc; -- withElementsFalseByCalling: (BOOL(*)(elt))testFunc - call: (void(*)(elt))destFunc; -- withElementsTransformedByCalling: (elt(*)(elt))transFunc - call: (void(*)(elt))destFunc; - @end @protocol Collecting // ADDING; -- addObject: newObject; -- addObjectIfAbsent: newObject; -- addContentsOf: (id )aCollection; -- addContentsOfIfAbsent: (id )aCollection; -- addObjectsCount: (unsigned)count, ...; +- (void) addObject: newObject; +- (void) addObjectIfAbsent: newObject; +- (void) addContentsOf: (id )aCollection; +- (void) addContentsIfAbsentOf: (id )aCollection; +- (void) addWithObjects: (id*)objc count: (unsigned)c; +- (void) addObjects: firstObject, ...; +- (void) addObjects: firstObject rest: (va_list)ap; // REMOVING; -- removeObject: oldObject; -- removeObject: oldObject ifAbsentCall: (id(*)(arglist_t))excFunc; -- removeAllOccurrencesOfObject: oldObject; -- removeContentsIn: (id )aCollection; -- removeContentsNotIn: (id )aCollection; -- uniqueContents; -- empty; +- (void) removeObject: oldObject; +- (void) removeAllOccurrencesOfObject: oldObject; +- (void) removeContentsIn: (id )aCollection; +- (void) removeContentsNotIn: (id )aCollection; +- (void) uniqueContents; +- (void) empty; // REPLACING; -- replaceObject: oldObject with: newObject; -- replaceObject: oldObject with: newObject - ifAbsentCall:(id(*)(arglist_t))excFunc; -- replaceAllOccurrencesOfObject: oldObject with: newObject; - -// ENUMERATING WHILE CHANGING CONTENTS; -- safeMakeObjectsPerform: (SEL)aSel; -- safeMakeObjectsPerform: (SEL)aSel with: argObject; -- safeWithObjectsCall: (void(*)(id))aFunc; -- safeWithObjectsCall: (void(*)(id))aFunc whileTrue:(BOOL *)flag; - - -// NON-OBJECT ELEMENT METHOD NAMES; - -// ADDING; -- addElement: (elt)newElement; -- addElementIfAbsent: (elt)newElement; -- addElementsCount: (unsigned)count, ...; - -// REMOVING; -- (elt) removeElement: (elt)oldElement; -- (elt) removeElement: (elt)oldElement - ifAbsentCall: (elt(*)(arglist_t))excFunc; -- removeAllOccurrencesOfElement: (elt)oldElement; - -// REPLACING; -- (elt) replaceElement: (elt)oldElement with: (elt)newElement; -- (elt) replaceElement: (elt)oldElement with: (elt)newElement - ifAbsentCall: (elt(*)(arglist_t))excFunc; -- replaceAllOccurrencesOfElement: (elt)oldElement with: (elt)newElement; - -// ENUMERATING WHILE CHANGING CONTENTS; -- safeWithElementsCall: (void(*)(elt))aFunc; -- safeWithElementsCall: (void(*)(elt))aFunc whileTrue: (BOOL*)flag; +- (void) replaceObject: oldObject withObject: newObject; +- (void) replaceAllOccurrencesOfObject: oldObject withObject: newObject; @end +#define NO_OBJECT nil + #endif /* __Collecting_h_INCLUDE_GNU */ diff --git a/Source/objects/Collection.h b/Source/objects/Collection.h index 8c6986991..774041c39 100644 --- a/Source/objects/Collection.h +++ b/Source/objects/Collection.h @@ -32,29 +32,42 @@ #include #include #include -#include #include -@interface Collection : NSObject -{ -} - -- (void) withObjectsInvoke: anInvocation; -- printElement: (elt)anElement; -- printForDebugger; - +@interface ConstantCollection : NSObject +- printForDebugger; /* This method will disappear later. */ @end -// #warning fix this macro -#define FOR_COLL(ACOLL, ELT) \ +@interface Collection : ConstantCollection +@end + +@interface Enumerator : NSObject +{ + id collection; + void *enum_state; +} +@end + +#define FOR_COLLECTION(ACOLL, ELT) \ { \ - void *_es = [ACOLL initEnumState]; \ - while ([ACOLL getNextElement:&(ELT) withEnumState:&_es]) \ + void *_es = [ACOLL newEnumState]; \ + while ((ELT = [ACOLL nextObjectWithEnumState: &_es])) \ { -#define FOR_COLL_END \ +#define END_FOR_COLLECTION(ACOLL) \ } \ - [ACOLL freeEnumState:_es]; \ + [ACOLL freeEnumState: &_es]; \ +} + +#define FOR_COLLECTION_WHILE_TRUE(ACOLL, ELT, FLAG) \ +{ \ + void *_es = [ACOLL newEnumState]; \ + while (FLAG && (ELT = [ACOLL nextObjectWithEnumState: &_es])) \ + { + +#define END_FOR_COLLECTION_WHILE_TRUE(ACOLL) \ + } \ + [ACOLL freeEnumState: &_es]; \ } /* The only subclassResponsibilities in Collection are: diff --git a/Source/objects/CollectionPrivate.h b/Source/objects/CollectionPrivate.h index 1b7183022..27e9bc611 100644 --- a/Source/objects/CollectionPrivate.h +++ b/Source/objects/CollectionPrivate.h @@ -1,8 +1,8 @@ /* Collection definitions for the use of subclass implementations only - Copyright (C) 1993,1994, 1995 Free Software Foundation, Inc. + Copyright (C) 1993,1994, 1995, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum - Date: May 1993 + Created: May 1993 This file is part of the GNU Objective C Class Library. @@ -25,28 +25,23 @@ #define __CollectionPrivate_h_INCLUDE_GNU #include -#include -@interface Collection (ArchivingHelpers) -/* These methods should never be called except in order inside - -write: and -read: */ -- _writeInit: (TypedStream*)aStream; -- _readInit: (TypedStream*)aStream; -- _writeContents: (TypedStream*)aStream; -- _readContents: (TypedStream*)aStream; - -/* The Coding versions of the above */ +@interface ConstantCollection (ArchivingHelpers) +/* These methods should never be called except in order, and inside + -encodeWithCoder: and -decodeWithCoder: */ - (void) _encodeCollectionWithCoder: (id )aCoder; - _initCollectionWithCoder: (id )aCoder; - (void) _encodeContentsWithCoder: (id )aCoder; - (void) _decodeContentsWithCoder: (id )aCoder; @end -@interface Collection (DeallocationHelpers) +@interface ConstantCollection (DeallocationHelpers) /* Empty the internals of a collection after the contents have already been released. */ -- _empty; +- (void) _collectionEmpty; + +- (void) _collectionReleaseContents; /* Deallocate the internals of a collection after the contents have already been released. */ @@ -54,63 +49,4 @@ @end - -/* To be used inside methods for getting the element comparison function. - This macro could be redefined when the comparison function is an - instance variable or is fixed. - I'm wondering if I should put _comparison_function back as an instance - variable in Collection. */ -#define COMPARISON_FUNCTION [self comparisonFunction] - -/* Use this for comparing elements in your implementation. */ -#define COMPARE_ELEMENTS(ELT1, ELT2) \ - ((*COMPARISON_FUNCTION)(ELT1, ELT2)) - -#define ELEMENTS_EQUAL(ELT1, ELT2) \ - (COMPARE_ELEMENTS(ELT1, ELT2) == 0) - -#define ENCODING_IS_OBJECT(ENCODING) \ - ((*(ENCODING) == _C_ID) || (*(ENCODING) == _C_CLASS)) - -/* To be used inside a method for determining if the contents are objects */ -#define CONTAINS_OBJECTS \ - (ENCODING_IS_OBJECT([self contentType])) - -/* Used inside a method for sending "-retain" if necessary */ -#define RETAIN_ELT(ELT) \ - if (CONTAINS_OBJECTS) [ELT.id_u retain] - -/* Used inside a method for sending "-release" if necessary */ -#define RELEASE_ELT(ELT) \ - if (CONTAINS_OBJECTS) [ELT.id_u release] - -/* Used inside a method for sending "-autorelease" if necessary */ -#define AUTORELEASE_ELT(ELT) \ - ((CONTAINS_OBJECTS) ? ((elt)[ELT.id_u autorelease]) : ELT) - - -/* Error Handling */ - -#define RETURN_BY_CALLING_EXCEPTION_FUNCTION(FUNC) \ -return (*FUNC)(__builtin_apply_args()) - - -/* To be used inside a method for making sure the contents are objects. - typeof(DEFAULT_ERROR_RETURN) must be the same type as the method - returns. */ -#define CHECK_CONTAINS_OBJECTS_ERROR() \ -({if (!(CONTAINS_OBJECTS)) \ -{ \ - [self error:"in %s, requires object contents", sel_get_name(_cmd)]; \ -}}) - -/* To be used inside a method whenever a particular element isn't found */ -#define ELEMENT_NOT_FOUND_ERROR(AN_ELEMENT) \ -([self error:"in %s, element not found.", sel_get_name(_cmd)]) - -/* To be used inside a method whenever there is no element matching the - needed criteria */ -#define NO_ELEMENT_FOUND_ERROR() \ -([self error:"in %s, no element found.", sel_get_name(_cmd)]) - #endif /* __CollectionPrivate_h_INCLUDE_GNU */ diff --git a/Source/objects/Dictionary.h b/Source/objects/Dictionary.h index 5bea6fb7f..9f6a67cfa 100644 --- a/Source/objects/Dictionary.h +++ b/Source/objects/Dictionary.h @@ -1,5 +1,5 @@ /* Interface for Objective-C Dictionary collection object - Copyright (C) 1993,1994, 1995 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1995, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum Date: May 1993 @@ -26,18 +26,14 @@ #include #include +#include @interface Dictionary : KeyedCollection { - coll_cache_ptr _contents_hash; // a hashtable to hold the contents; - int (*_comparison_function)(elt,elt); + NSMapTable *_contents_hash; } -- initWithType: (const char *)contentEncoding - keyType: (const char *)keyEncoding - capacity: (unsigned)aCapacity; -- initWithType: (const char *)contentEncoding - capacity: (unsigned)aCapacity; +- initWithCapacity: (unsigned)aCapacity; @end diff --git a/Source/objects/Heap.h b/Source/objects/Heap.h index d3592c4e3..44d427ec4 100644 --- a/Source/objects/Heap.h +++ b/Source/objects/Heap.h @@ -1,8 +1,8 @@ /* Interface for Objective-C Heap collection object - Copyright (C) 1993,1994 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum - Date: May 1993 + Created: May 1993 This file is part of the GNU Objective C Class Library. @@ -28,14 +28,9 @@ #include @interface Heap : Array -{ -} -- addElement: (elt)anElement; -- (elt) removeFirstElement; - -- heapifyFromIndex: (unsigned)index; -- heapify; +- (void) heapifyFromIndex: (unsigned)index; +- (void) heapify; @end diff --git a/Source/objects/IndexedCollecting.h b/Source/objects/IndexedCollecting.h index cb7bfc742..89ef892d4 100644 --- a/Source/objects/IndexedCollecting.h +++ b/Source/objects/IndexedCollecting.h @@ -1,5 +1,5 @@ /* Protocol for Objective-C objects that hold elements accessible by index - Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1995, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum Date: May 1993 @@ -36,7 +36,7 @@ #define __IndexedCollecting_h_OBJECTS_INCLUDE #include -#include +#include typedef struct _IndexRange { unsigned location; @@ -47,8 +47,8 @@ typedef struct _IndexRange { ({IndexRange __a=(RANGE1), __b=(RANGE2); \ __a.start<=__b.start && __a.end>=__b.end;}) - -@protocol ConstantIndexedCollecting + +@protocol ConstantIndexedCollecting // GETTING MEMBERS BY INDEX; - objectAtIndex: (unsigned)index; @@ -61,115 +61,53 @@ typedef struct _IndexRange { // GETTING INDICES BY MEMBER; - (unsigned) indexOfObject: anObject; -- (unsigned) indexOfObject: anObject - ifAbsentCall: (unsigned(*)(arglist_t))excFunc; - (unsigned) indexOfObject: anObject inRange: (IndexRange)aRange; -- (unsigned) indexOfObject: anObject inRange: (IndexRange)aRange - ifAbsentCall: (unsigned(*)(arglist_t))excFunc; // TESTING; -- (BOOL) includesIndex: (unsigned)index; - (BOOL) contentsEqualInOrder: (id )aColl; +- (int) compareInOrderContentsOf: (id )aCollection; - (unsigned) indexOfFirstDifference: (id )aColl; - (unsigned) indexOfFirstIn: (id )aColl; - (unsigned) indexOfFirstNotIn: (id )aColl; // ENUMERATING; -- (BOOL) getPrevObject: (id*)anIdPtr withEnumState: (void**)enumState; -- withObjectsInRange: (IndexRange)aRange call:(void(*)(id))aFunc; -- withObjectsInReverseCall: (void(*)(id))aFunc; -- withObjectsInReverseCall: (void(*)(id))aFunc whileTrue:(BOOL *)flag; +- (id ) reverseObjectEnumerator; +- (void) withObjectsInRange: (IndexRange)aRange + invoke: (id )anInvocation; +- (void) withObjectsInReverseInvoke: (id )anInvocation; +- (void) withObjectsInReverseInvoke: (id )anInvocation + whileTrue:(BOOL *)flag; +- (void) makeObjectsPerformInReverse: (SEL)aSel; +- (void) makeObjectsPerformInReverse: (SEL)aSel withObject: argObject; -// NON-OBJECT MESSAGE NAMES; - -// GETTING ELEMENTS BY INDEX; -- (elt) elementAtIndex: (unsigned)index; -- (elt) firstElement; -- (elt) lastElement; - -// GETTING MEMBERS BY NEIGHBOR; -- (elt) successorOfElement: (elt)anElement; -- (elt) predecessorOfElement: (elt)anElement; - -// GETTING INDICES BY MEMBER; -- (unsigned) indexOfElement: (elt)anElement; -- (unsigned) indexOfElement: (elt)anElement - ifAbsentCall: (unsigned(*)(arglist_t))excFunc; -- (unsigned) indexOfElement: (elt)anElement inRange: (IndexRange)aRange; -- (unsigned) indexOfElement: (elt)anElement inRange: (IndexRange)aRange - ifAbsentCall: (unsigned(*)(arglist_t))excFunc; - -// ENUMERATING; -- (BOOL) getPrevElement:(elt*)anElementPtr withEnumState: (void**)enumState; -- withElementsInRange: (IndexRange)aRange call:(void(*)(elt))aFunc; -- withElementsInReverseCall: (void(*)(elt))aFunc; -- withElementsInReverseCall: (void(*)(elt))aFunc whileTrue:(BOOL *)flag; +// LOW-LEVEL ENUMERATING; +- prevObjectWithEnumState: (void**)enumState; @end -@protocol IndexedCollecting + +@protocol IndexedCollecting -// ADDING; -- insertObject: newObject atIndex: (unsigned)index; -- insertObject: newObject before: oldObject; -- insertObject: newObject after: oldObject; -- insertContentsOf: (id )aCollection - atIndex: (unsigned)index; -- appendObject: newObject; -- prependObject: newObject; -- appendContentsOf: (id )aCollection; -- prependContentsOf: (id )aCollection; +// REPLACING; +- (void) replaceObjectAtIndex: (unsigned)index with: newObject; -// REPLACING AND SWAPPING -- replaceObjectAtIndex: (unsigned)index with: newObject; -- replaceRange: (IndexRange)aRange with: (id )aCollection; -- replaceRange: (IndexRange)aRange using: (id )aCollection; -- swapAtIndeces: (unsigned)index1 : (unsigned)index2; - -// REMOVING -- removeObjectAtIndex: (unsigned)index; -- removeFirstObject; -- removeLastObject; -- removeRange: (IndexRange)aRange; - -// ENUMERATING WHILE CHANGING CONTENTS; -- safeWithObjectsInReverseCall: (void(*)(id))aFunc; -- safeWithObjectsInReverseCall: (void(*)(id))aFunc whileTrue:(BOOL *)flag; +// REMOVING; +- (void) removeObjectAtIndex: (unsigned)index; +- (void) removeFirstObject; +- (void) removeLastObject; +- (void) removeRange: (IndexRange)aRange; // SORTING; -- sortContents; -- sortObjectsByCalling: (int(*)(id,id))aFunc; -- sortAddObject: newObject; -- sortAddObject: newObject byCalling: (int(*)(id,id))aFunc; - - -// NON-OBJECT MESSAGE NAMES; - -// ADDING; -- appendElement: (elt)newElement; -- prependElement: (elt)newElement; -- insertElement: (elt)newElement atIndex: (unsigned)index; -- insertElement: (elt)newElement before: (elt)oldElement; -- insertElement: (elt)newElement after: (elt)oldElement; - -// REMOVING AND REPLACING; -- (elt) removeElementAtIndex: (unsigned)index; -- (elt) removeFirstElement; -- (elt) removeLastElement; -- (elt) replaceElementAtIndex: (unsigned)index with: (elt)newElement; - -// ENUMERATING WHILE CHANGING CONTENTS; -- safeWithElementsInRange: (IndexRange)aRange call:(void(*)(elt))aFunc; -- safeWithElementsInReverseCall: (void(*)(elt))aFunc; -- safeWithElementsInReverseCall: (void(*)(elt))aFunc whileTrue:(BOOL *)flag; - -// SORTING; -- sortElementsByCalling: (int(*)(elt,elt))aFunc; -- sortAddElement: (elt)newElement; -- sortAddElement: (elt)newElement byCalling: (int(*)(elt,elt))aFunc; +- (void) sortContents; +- (void) sortAddObject: newObject; @end +#include +#define NO_INDEX UINT_MAX + +/* xxx Fix this comment: */ + /* Most methods in the KeyedCollecting protocol that mention a key are duplicated in the IndexedCollecting protocol, with their names modified to reflect that the "key" now must be an unsigned integer, diff --git a/Source/objects/IndexedCollection.h b/Source/objects/IndexedCollection.h index 6d7337c0c..c5df4e78b 100644 --- a/Source/objects/IndexedCollection.h +++ b/Source/objects/IndexedCollection.h @@ -1,5 +1,5 @@ /* Interface for Objective-C Sequential Collection object. - Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1995, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum Date: May 1993 @@ -28,12 +28,54 @@ #include #include -@interface IndexedCollection : KeyedCollection +@interface ConstantIndexedCollection : ConstantCollection +@end + +@interface IndexedCollection : ConstantIndexedCollection +@end + +@interface ReverseEnumerator : Enumerator @end /* Put this on category instead of class to avoid bogus complaint from gcc */ -@interface IndexedCollection (IndexedCollectionProtocol) +@interface ConstantIndexedCollection (Protocol) @end +@interface IndexedCollection (Protocol) +@end + +#define FOR_INDEXED_COLLECTION(ACOLL, ELT) \ +{ \ + void *_es = [ACOLL newEnumState]; \ + while ((ELT = [ACOLL nextObjectWithEnumState: &_es])) \ + { + +#define END_FOR_INDEXED_COLLECTION(ACOLL) \ + } \ + [ACOLL freeEnumState: &_es]; \ +} + +#define FOR_INDEXED_COLLECTION_REVERSE(ACOLL, ELT) \ +{ \ + void *_es = [ACOLL newEnumState]; \ + while ((ELT = [ACOLL prevObjectWithEnumState: &_es])) \ + { + +#define END_FOR_INDEXED_COLLECTION_REVERSE(ACOLL) \ + } \ + [ACOLL freeEnumState: &_es]; \ +} + +#define FOR_INDEXED_COLLECTION_WHILE_TRUE(ACOLL, ELT, FLAG) \ +{ \ + void *_es = [ACOLL newEnumState]; \ + while (FLAG && (ELT = [ACOLL nextObjectWithEnumState: &_es])) \ + { + +#define END_FOR_INDEXED_COLLECTION_WHILE_TRUE(ACOLL) \ + } \ + [ACOLL freeEnumState: &_es]; \ +} + /* The only subclassResponsibilities in IndexedCollection are: diff --git a/Source/objects/KeyedCollecting.h b/Source/objects/KeyedCollecting.h index 6f8a51cf6..f50dbfe2c 100644 --- a/Source/objects/KeyedCollecting.h +++ b/Source/objects/KeyedCollecting.h @@ -1,5 +1,5 @@ /* Protocol for Objective-C objects holding (keyElement,contentElement) pairs. - Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1995, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum Date: May 1993 @@ -38,84 +38,44 @@ @protocol ConstantKeyedCollecting +// INITIALIZING; +- initWithObjects: (id*)objects forKeys: (id*)keys count: (unsigned)c; + // GETTING ELEMENTS AND KEYS; -- objectAtKey: (elt)aKey; -- keyObjectOfObject: aContentObject; +- objectAtKey: aKey; +- keyOfObject: aContentObject; // TESTING; -- (BOOL) includesKey: (elt)aKey; +- (BOOL) containsKey: aKey; // ENUMERATIONS; -- withKeyObjectsCall: (void(*)(id))aFunc; -- withKeyObjectsAndContentObjectsCall: (void(*)(id,id))aFunc; -- withKeyObjectsAndContentObjectsCall: (void(*)(id,id))aFunc +- (id ) keyEnumerator; +- withKeyObjectsInvoke: (id )anInvocation; +- withKeyObjectsInvoke: (id )anInvocation whileTrue: (BOOL *)flag; -// NON-OBJECT ELEMENT METHOD NAMES; +// LOW-LEVEL ENUMERATING; +- nextObjectAndKey: (id*)keyPtr withEnumState: (void**)enumState; -// INITIALIZING; -- initWithType: (const char *)contentsEncoding - keyType: (const char *)keyEncoding; -- initKeyType: (const char *)keyEncoding; - -// GETTING ELEMENTS AND KEYS; -- (elt) elementAtKey: (elt)aKey; -- (elt) elementAtKey: (elt)aKey ifAbsentCall: (elt(*)(arglist_t))excFunc; -- (elt) keyElementOfElement: (elt)aContentObject; -- (elt) keyElementOfElement: (elt)aContentObject - ifAbsentCall: (elt(*)(arglist_t))excFunc; - -// TESTING; -- (const char *) keyType; - -// ENUMERATING; -- (BOOL) getNextKey: (elt*)aKeyPtr content: (elt*)anElementPtr - withEnumState: (void**)enumState; -- withKeyElementsCall: (void(*)(elt))aFunc; -- withKeyElementsAndContentElementsCall: (void(*)(elt,elt))aFunc; -- withKeyElementsAndContentElementsCall: (void(*)(elt,elt))aFunc - whileTrue: (BOOL *)flag; +// COPYING; +- shallowCopyValuesAs: (Class)aCollectingClass; +- shallowCopyKeysAs: (Class)aCollectingClass; +- copyValuesAs: (Class)aCollectingClass; +- copyKeysAs: (Class)aCollectingClass; @end @protocol KeyedCollecting // ADDING; -- putObject: newContentObject atKey: (elt)aKey; +- (void) putObject: newContentObject atKey: aKey; // REPLACING AND SWAPPING; -- replaceObjectAtKey: (elt)aKey with: newContentObject; -- swapAtKeys: (elt)key1 : (elt)key2; +- (void) replaceObjectAtKey: aKey with: newContentObject; +- (void) swapObjectsAtKeys: key1 : key2; // REMOVING; -- removeObjectAtKey: (elt)aKey; - -// ENUMERATING WHILE CHANGING CONTENTS; -- safeWithKeyObjectsCall: (void(*)(id))aFunc; -- safeWithKeyObjectsAndContentObjectsCall: (void(*)(id,id))aFunc; -- safeWithKeyObjectsAndContentObjectsCall: (void(*)(id,id))aFunc - whileTrue: (BOOL *)flag; - - -// NON-OBJECT ELEMENT METHOD NAMES; - -// ADDING; -- putElement: (elt)newContentElement atKey: (elt)aKey; - -// REPLACING; -- (elt) replaceElementAtKey: (elt)aKey with: (elt)newContentElement; -- (elt) replaceElementAtKey: (elt)aKey with: (elt)newContentElement - ifAbsentCall: (elt(*)(arglist_t))excFunc; - -// REMOVING; -- (elt) removeElementAtKey: (elt)aKey; -- (elt) removeElementAtKey: (elt)aKey ifAbsentCall: (elt(*)(arglist_t))excFunc; - -// ENUMERATING WHILE CHANGING CONTENTS; -- safeWithKeyElementsCall: (void(*)(elt))aFunc; -- safeWithKeyElementsAndContentElementsCall: (void(*)(elt,elt))aFunc; -- safeWithKeyElementsAndContentElementsCall: (void(*)(elt,elt))aFunc - whileTrue: (BOOL *)flag; +- (void) removeObjectAtKey: aKey; @end diff --git a/Source/objects/KeyedCollection.h b/Source/objects/KeyedCollection.h index 5f1af6558..a9d6d32ba 100644 --- a/Source/objects/KeyedCollection.h +++ b/Source/objects/KeyedCollection.h @@ -1,5 +1,5 @@ /* Interface for Objective-C KeyedCollection collection object - Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1995, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum Date: May 1993 @@ -28,12 +28,44 @@ #include #include -@interface KeyedCollection : Collection +@interface ConstantKeyedCollection : Collection +@end + +@interface KeyedCollection : ConstantKeyedCollection @end /* Put this on category instead of class to avoid bogus complaint from gcc */ -@interface KeyedCollection (KeyedCollectingProtocol) +@interface ConstantKeyedCollection (Protocol) @end +@interface KeyedCollection (Protocol) +@end + +@interface KeyEnumerator : Enumerator +@end + +#define FOR_KEYED_COLLECTION(ACOLL, ELT, KEY) \ +{ \ + void *_es = [ACOLL newEnumState]; \ + while ((ELT = [ACOLL nextObjectAndKey: &(KEY) withEnumState: &_es])) \ + { + +#define END_FOR_KEYED_COLLECTION(ACOLL) \ + } \ + [ACOLL freeEnumState: &_es]; \ +} + +#define FOR_KEYED_COLLECTION_WHILE_TRUE(ACOLL, ELT, KEY, FLAG) \ +{ \ + void *_es = [ACOLL newEnumState]; \ + while (FLAG && (ELT = [ACOLL nextObjectAndKey: &(KEY) \ + withEnumState: &_es])) \ + { + +#define END_FOR_KEYED_COLLECTION_WHILE_TRUE(ACOLL) \ + } \ + [ACOLL freeEnumState: &_es]; \ +} + /* The only subclassResponsibilities in IndexedCollection are: diff --git a/Source/objects/LinkedList.h b/Source/objects/LinkedList.h index b6a2f7e20..198a21b36 100644 --- a/Source/objects/LinkedList.h +++ b/Source/objects/LinkedList.h @@ -1,8 +1,8 @@ /* Interface for Objective-C LinkedList collection object - Copyright (C) 1993,1994 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum - Date: May 1993 + Created: May 1993 This file is part of the GNU Objective C Class Library. @@ -25,7 +25,7 @@ #define __LinkedList_h_INCLUDE_GNU #include -#include +#include /* The protocol defines the interface to an object that may be an element in a LinkedList. @@ -33,13 +33,16 @@ @protocol LinkedListComprising - nextLink; - prevLink; -- setNextLink: (id )aLink; -- setPrevLink: (id )aLink; +- (void) setNextLink: (id )aLink; +- (void) setPrevLink: (id )aLink; +- linkedList; +- (void) setLinkedList: aLinkedList; @end -@interface LinkedList : IndexedCollection +@interface LinkedList : OrderedCollection { id _first_link; + id _last_link; unsigned int _count; } diff --git a/Source/objects/LinkedListNode.h b/Source/objects/LinkedListNode.h index 09c323b7e..c2b9af08a 100644 --- a/Source/objects/LinkedListNode.h +++ b/Source/objects/LinkedListNode.h @@ -1,8 +1,8 @@ /* Interface for Objective-C LinkedListNode object - Copyright (C) 1993,1994, 1995 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1995, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum - Date: May 1993 + Created: May 1993 This file is part of the GNU Objective C Class Library. @@ -32,8 +32,8 @@ { id _next; id _prev; + id _linked_list; } @end #endif /* __LinkedListNode_h_INCLUDE_GNU */ - diff --git a/Source/objects/MappedCollector.h b/Source/objects/MappedCollector.h index 6856d6eb3..1c7420aac 100644 --- a/Source/objects/MappedCollector.h +++ b/Source/objects/MappedCollector.h @@ -1,5 +1,5 @@ /* Interface for Objective-C MappedCollector collection object - Copyright (C) 1993,1994 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum Date: May 1993 @@ -33,7 +33,7 @@ id _domain; } -- initCollection: (id )aDomain +- initWithCollection: (id )aDomain map: (id )aMap; @end diff --git a/Source/objects/Queue.h b/Source/objects/Queue.h index f845ef99b..2ef5a868b 100644 --- a/Source/objects/Queue.h +++ b/Source/objects/Queue.h @@ -1,8 +1,8 @@ /* Interface for Objective-C Queue object - Copyright (C) 1993,1994 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum - Date: May 1993 + Created: May 1993 This file is part of the GNU Objective C Class Library. @@ -29,13 +29,9 @@ @interface Queue : CircularArray -- enqueueObject: newObject; +- (void) enqueueObject: newObject; - dequeueObject; -// NON-OBJECT MESSAGE NAMES; -- enqueueElement: (elt)newElement; -- (elt) dequeueElement; - @end #endif /* __Queue_h_INCLUDE_GNU */ diff --git a/Source/objects/Set.h b/Source/objects/Set.h index 5e7278cb0..b433d5936 100644 --- a/Source/objects/Set.h +++ b/Source/objects/Set.h @@ -1,5 +1,5 @@ /* Interface for Objective-C Set collection object - Copyright (C) 1993,1994 Free Software Foundation, Inc. + Copyright (C) 1993,1994, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum Date: May 1993 @@ -26,24 +26,23 @@ #include #include +#include @interface Set : Collection { - coll_cache_ptr _contents_hash; // a hashtable to hold the contents; + NSHashTable *_contents_hash; // a hashtable to hold the contents; } // MANAGING CAPACITY; + (unsigned) defaultCapacity; // INITIALIZING AND FREEING; -- initWithType: (const char *)contentEncoding - capacity: (unsigned)aCapacity; - initWithCapacity: (unsigned)aCapacity; // SET OPERATIONS; -- intersectWithCollection: (id )aCollection; -- unionWithCollection: (id )aCollection; -- differenceWithCollection: (id )aCollection; +- (void) intersectWithCollection: (id )aCollection; +- (void) unionWithCollection: (id )aCollection; +- (void) differenceWithCollection: (id )aCollection; - shallowCopyIntersectWithCollection: (id )aCollection; - shallowCopyUnionWithCollection: (id )aCollection; diff --git a/Source/objects/SplayTree.h b/Source/objects/SplayTree.h index 73e3edfcf..779afb1af 100644 --- a/Source/objects/SplayTree.h +++ b/Source/objects/SplayTree.h @@ -1,5 +1,5 @@ /* Interface for Objective-C SplayTree collection object - Copyright (C) 1993,1994 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum Date: May 1993 @@ -41,7 +41,7 @@ { } -- splayNode: aNode; +- (void) splayNode: aNode; @end diff --git a/Source/objects/Stack.h b/Source/objects/Stack.h index bfd7d5d02..2932f9a48 100644 --- a/Source/objects/Stack.h +++ b/Source/objects/Stack.h @@ -1,8 +1,8 @@ /* Interface for Objective-C Stack object - Copyright (C) 1993,1994 Free Software Foundation, Inc. + Copyright (C) 1993, 1994, 1996 Free Software Foundation, Inc. Written by: R. Andrew McCallum - Date: May 1993 + Created: May 1993 This file is part of the GNU Objective C Class Library. @@ -29,17 +29,11 @@ @interface Stack : Array -- pushObject: anObject; +- (void) pushObject: anObject; - popObject; - topObject; -- duplicateTop; -- exchangeTop; - -// NON-OBJECT MESSAGE NAMES; -- pushElement: (elt)anElement; -- (elt) popElement; -- (elt) topElement; - +- (void) duplicateTop; +- (void) exchangeTop; @end