Skip to content

Commit 7428f24

Browse files
committed
added -pushIfNotPresent:
1 parent 74063a9 commit 7428f24

3 files changed

Lines changed: 45 additions & 4 deletions

File tree

Headers/GNUstepBase/GSMinHeap.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,20 @@ GS_EXPORT_CLASS
102102
- (id) pop;
103103

104104
/** Adds obj to the heap and returns YES on success. May return NO on failure
105-
* (of obj was nil or if there is insufficient memory for the heap to grow).
105+
* (if obj was nil or if there is insufficient memory for the heap to grow).
106106
* The heap takes ownership of (retains) obj on success, but not on failure.
107107
* It is possible to push the same object ot the heap more than once.
108108
*/
109109
- (BOOL) push: (id)obj;
110110

111+
/** Adds obj to the heap if (and only if) it is not already present.
112+
* Returns YES on success (the object was present or was added).
113+
* May return NO on failure (if obj was nil or if there is insufficient
114+
* memory for the heap to grow).
115+
* The heap takes ownership of (retains) obj on success if it was added.
116+
*/
117+
- (BOOL) pushIfNotPresent: (id)obj;
118+
111119
/** Removes all objects matching obj using the -isEqual: method.
112120
*/
113121
- (void) removeObject: (id)obj;

Source/Additions/GSMinHeap.m

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,11 +362,36 @@ - (id) pop
362362

363363
- (BOOL) push: (id)obj
364364
{
365-
if (obj != nil && heap_push(internal, RETAIN(obj)))
365+
if (obj != nil)
366366
{
367-
return YES;
367+
if (heap_push(internal, obj))
368+
{
369+
RETAIN(obj);
370+
return YES;
371+
}
372+
}
373+
return NO;
374+
}
375+
376+
- (BOOL) pushIfNotPresent: (id)obj
377+
{
378+
if (obj != nil)
379+
{
380+
size_t index = internal->size;
381+
382+
while (index-- > 0)
383+
{
384+
if (obj == internal->data[index])
385+
{
386+
return YES;
387+
}
388+
}
389+
if (heap_push(internal, obj))
390+
{
391+
RETAIN(obj);
392+
return YES;
393+
}
368394
}
369-
RELEASE(obj);
370395
return NO;
371396
}
372397

Tests/base/GSMinHeap/test00.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@
6161
PASS_EQUAL([heap next], @"b", "heap -next of a,b returns b")
6262
PASS([heap count] == 1, "heap count 1 after push push next")
6363

64+
PASS([heap pushIfNotPresent: @"a"] == YES, "-pushIfNotPresent: reports a added to heap")
65+
PASS([heap count] == 2, "heap count 2 after -pushIfNotPresent: added a")
66+
PASS([heap pushIfNotPresent: @"a"] == YES, "-pushIfNotPresent: reports a already in heap")
67+
PASS([heap count] == 2, "heap count 2 after -pushIfNotPresent: added a again")
68+
69+
PASS([heap pushIfNotPresent: nil] == NO, "-pushIfNotPresent: refuses to add nil")
70+
PASS([heap count] == 2, "heap count 2 after -pushIfNotPresent: refused to add nil")
71+
6472
DESTROY(heap);
6573

6674
END_SET("GSMinHeap")

0 commit comments

Comments
 (0)