Skip to content

Commit 74063a9

Browse files
committed
iadd drop and next
1 parent e026a4d commit 74063a9

5 files changed

Lines changed: 128 additions & 89 deletions

File tree

Headers/GNUstepBase/GSMinHeap.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ GS_EXPORT_CLASS
7171
*/
7272
- (NSUInteger) count;
7373

74+
/** Removes the first object from the heap.
75+
*/
76+
- (void) drop;
77+
7478
/** Removes all objects from the heap.
7579
*/
7680
- (void) empty;
@@ -83,7 +87,12 @@ GS_EXPORT_CLASS
8387
- (instancetype) initWithCapacity: (size_t)cap
8488
andComparator: (GSMinHeapComparator)cmp;
8589

86-
/** Returns the first object on the heap or nil if it is empty.
90+
/** Discards the first object from the heap and returns the next object.
91+
* (equivalent to -drop followed by -peek).
92+
*/
93+
- (id) next;
94+
95+
/** Returns the first object on the heap or nil if it is empty
8796
*/
8897
- (id) peek;
8998

Source/Additions/GSMinHeap.m

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,14 @@ - (void) dealloc
276276
DEALLOC
277277
}
278278

279+
- (void) drop
280+
{
281+
if (internal->size > 0)
282+
{
283+
RELEASE(heap_pop(internal));
284+
}
285+
}
286+
279287
- (void) empty
280288
{
281289
size_t i;
@@ -325,6 +333,19 @@ - (instancetype) initWithCapacity: (size_t)cap
325333
return self;
326334
}
327335

336+
- (id) next
337+
{
338+
if (internal->size > 0)
339+
{
340+
RELEASE(heap_pop(internal));
341+
}
342+
if (0 == internal->size)
343+
{
344+
return nil;
345+
}
346+
return internal->data[0];
347+
}
348+
328349
- (id) peek
329350
{
330351
if (0 == internal->size)

Tests/base/GSMinHeap/test00.m

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,48 @@
1818

1919
heap = [[GSMinHeap alloc] init];
2020

21-
PASS(heap != nil, "can create heap with default init");
21+
PASS(heap != nil, "can create heap with default init")
2222

23-
PASS([heap peek] == nil, "peek on empty heap returns nil");
24-
PASS([heap pop] == nil, "pop on empty heap returns nil");
23+
PASS([heap peek] == nil, "peek on empty heap returns nil")
24+
PASS([heap pop] == nil, "pop on empty heap returns nil")
2525

26-
PASS([heap push: nil] == NO, "cannot push nil");
27-
PASS([heap count] == 0, "heap count zero after push:nil");
28-
PASS([heap peek] == nil, "heap still empty after push:nil");
26+
PASS([heap push: nil] == NO, "cannot push nil")
27+
PASS([heap count] == 0, "heap count zero after push:nil")
28+
PASS([heap peek] == nil, "heap still empty after push:nil")
2929

30-
PASS([heap push: @"c"] == YES, "push first object");
31-
PASS([heap count] == 1, "heap count one after push first object");
32-
PASS([[heap peek] isEqual: @"c"], "peek returns first object");
30+
PASS([heap push: @"c"] == YES, "push first object")
31+
PASS([heap count] == 1, "heap count one after push first object")
32+
PASS([[heap peek] isEqual: @"c"], "peek returns first object")
3333

34-
PASS([heap push: @"a"] == YES, "push smaller object");
35-
PASS([heap count] == 2, "heap count two after push second object");
36-
PASS([[heap peek] isEqual: @"a"], "peek returns minimum object");
34+
PASS([heap push: @"a"] == YES, "push smaller object")
35+
PASS([heap count] == 2, "heap count two after push second object")
36+
PASS([[heap peek] isEqual: @"a"], "peek returns minimum object")
3737

38-
PASS([heap push: @"b"] == YES, "push third object");
39-
PASS([heap count] == 3, "heap count three after push third object");
38+
PASS([heap push: @"b"] == YES, "push third object")
39+
PASS([heap count] == 3, "heap count three after push third object")
4040

4141
obj = [heap pop];
42-
PASS([obj isEqual: @"a"], "first pop returns smallest object");
43-
PASS([heap count] == 2, "heap count two after pop one object");
42+
PASS([obj isEqual: @"a"], "first pop returns smallest object")
43+
PASS([heap count] == 2, "heap count two after pop one object")
4444

4545
obj = [heap pop];
46-
PASS([obj isEqual: @"b"], "second pop returns second smallest object");
47-
PASS([heap count] == 1, "heap count two after pop two objects");
46+
PASS([obj isEqual: @"b"], "second pop returns second smallest object")
47+
PASS([heap count] == 1, "heap count two after pop two objects")
4848

4949
obj = [heap pop];
50-
PASS([obj isEqual: @"c"], "third pop returns largest object");
51-
PASS([heap count] == 0, "heap count two after pop three objects");
50+
PASS([obj isEqual: @"c"], "third pop returns largest object")
51+
PASS([heap count] == 0, "heap count two after pop three objects")
5252

53-
PASS([heap pop] == nil, "heap empty after removing all objects");
53+
PASS([heap pop] == nil, "heap empty after removing all objects")
54+
55+
[heap push: @"a"];
56+
[heap drop];
57+
PASS([heap count] == 0, "heap count 0 after push drop sequence")
58+
59+
[heap push: @"a"];
60+
[heap push: @"b"];
61+
PASS_EQUAL([heap next], @"b", "heap -next of a,b returns b")
62+
PASS([heap count] == 1, "heap count 1 after push push next")
5463

5564
DESTROY(heap);
5665

@@ -65,19 +74,19 @@
6574
for (i = 100; i >= 0; i--)
6675
{
6776
PASS([heap push: [NSNumber numberWithInt: i]] == YES,
68-
"push succeeds");
77+
"push succeeds")
6978
}
7079

7180
PASS_EQUAL([[heap peek] stringValue], @"0",
72-
"minimum survives resizing");
81+
"minimum survives resizing")
7382

7483
for (i = 0; i <= 100; i++)
7584
{
7685
NSNumber *n = [heap pop];
77-
PASS([n intValue] == i, "pop returns values in ascending order");
86+
PASS([n intValue] == i, "pop returns values in ascending order")
7887
}
7988

80-
PASS([heap pop] == nil, "heap empty after growth test");
89+
PASS([heap pop] == nil, "heap empty after growth test")
8190

8291
DESTROY(heap);
8392

@@ -93,16 +102,16 @@
93102
[heap push: @"b"];
94103

95104
PASS([[heap peek] isEqual: @"c"],
96-
"custom comparator changes ordering");
105+
"custom comparator changes ordering")
97106

98107
obj = [heap pop];
99-
PASS([obj isEqual: @"c"], "first pop uses custom comparator");
108+
PASS([obj isEqual: @"c"], "first pop uses custom comparator")
100109

101110
obj = [heap pop];
102-
PASS([obj isEqual: @"b"], "second pop uses custom comparator");
111+
PASS([obj isEqual: @"b"], "second pop uses custom comparator")
103112

104113
obj = [heap pop];
105-
PASS([obj isEqual: @"a"], "third pop uses custom comparator");
114+
PASS([obj isEqual: @"a"], "third pop uses custom comparator")
106115

107116
DESTROY(heap);
108117

@@ -118,14 +127,14 @@
118127

119128
[heap empty];
120129

121-
PASS([heap peek] == nil, "peek after empty returns nil");
122-
PASS([heap pop] == nil, "pop after empty returns nil");
130+
PASS([heap peek] == nil, "peek after empty returns nil")
131+
PASS([heap pop] == nil, "pop after empty returns nil")
123132

124133
PASS([heap push: @"x"] == YES,
125-
"heap remains usable after empty");
134+
"heap remains usable after empty")
126135

127136
PASS([[heap pop] isEqual: @"x"],
128-
"heap functions correctly after empty");
137+
"heap functions correctly after empty")
129138

130139
DESTROY(heap);
131140

Tests/base/GSMinHeap/test01.m

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@
2121
[heap push: b];
2222

2323
PASS([heap containsObject: a1] == YES,
24-
"containsObject finds identical object");
24+
"containsObject finds identical object")
2525

2626
PASS([heap containsObject: a2] == YES,
27-
"containsObject uses isEqual:");
27+
"containsObject uses isEqual:")
2828

2929
PASS([heap containsObjectIdenticalTo: a1] == YES,
30-
"containsObjectIdenticalTo finds same pointer");
30+
"containsObjectIdenticalTo finds same pointer")
3131

3232
PASS([heap containsObjectIdenticalTo: a2] == NO,
33-
"containsObjectIdenticalTo distinguishes equal objects");
33+
"containsObjectIdenticalTo distinguishes equal objects")
3434

3535
PASS([heap containsObject: @"zzz"] == NO,
36-
"containsObject returns NO when absent");
36+
"containsObject returns NO when absent")
3737

3838
DESTROY(heap);
3939

@@ -43,24 +43,24 @@
4343

4444
heap = [[GSMinHeap alloc] init];
4545

46-
NSMutableString *a1 = [NSMutableString stringWithString:@"a"];
47-
NSMutableString *a2 = [NSMutableString stringWithString:@"a"];
48-
NSMutableString *b = [NSMutableString stringWithString:@"b"];
46+
NSMutableString *a1 = [NSMutableString stringWithString: @"a"];
47+
NSMutableString *a2 = [NSMutableString stringWithString: @"a"];
48+
NSMutableString *b = [NSMutableString stringWithString: @"b"];
4949

50-
[heap push:a1];
51-
[heap push:a2];
52-
[heap push:b];
50+
[heap push: a1];
51+
[heap push: a2];
52+
[heap push: b];
5353

54-
[heap removeObject:@"a"];
54+
[heap removeObject: @"a"];
5555

5656
PASS([heap count] == 1,
57-
"removeObject removes all equal objects");
57+
"removeObject removes all equal objects")
5858

59-
PASS([[heap pop] isEqual:@"b"],
60-
"remaining object correct");
59+
PASS([[heap pop] isEqual: @"b"],
60+
"remaining object correct")
6161

6262
PASS([heap pop] == nil,
63-
"heap empty after removal");
63+
"heap empty after removal")
6464

6565
DESTROY(heap);
6666

@@ -71,22 +71,22 @@
7171

7272
heap = [[GSMinHeap alloc] init];
7373

74-
NSMutableString *a1 = [NSMutableString stringWithString:@"a"];
75-
NSMutableString *a2 = [NSMutableString stringWithString:@"a"];
74+
NSMutableString *a1 = [NSMutableString stringWithString: @"a"];
75+
NSMutableString *a2 = [NSMutableString stringWithString: @"a"];
7676

77-
[heap push:a1];
78-
[heap push:a2];
77+
[heap push: a1];
78+
[heap push: a2];
7979

80-
[heap removeObjectIdenticalTo:a1];
80+
[heap removeObjectIdenticalTo: a1];
8181

8282
PASS([heap count] == 1,
83-
"removeObjectIdenticalTo removes only identical object");
83+
"removeObjectIdenticalTo removes only identical object")
8484

85-
PASS([heap containsObjectIdenticalTo:a1] == NO,
86-
"removed identical object absent");
85+
PASS([heap containsObjectIdenticalTo: a1] == NO,
86+
"removed identical object absent")
8787

88-
PASS([heap containsObjectIdenticalTo:a2] == YES,
89-
"equal but different object remains");
88+
PASS([heap containsObjectIdenticalTo: a2] == YES,
89+
"equal but different object remains")
9090

9191
DESTROY(heap);
9292

0 commit comments

Comments
 (0)