Skip to content

Commit b13a3c6

Browse files
committed
feat: add retrun value for Remove() method
1 parent 2871600 commit b13a3c6

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

kpq/keyed_priority_queue.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,14 +229,14 @@ func (pq *KeyedPriorityQueue[K, V]) ValueOf(k K) (V, bool) {
229229
}
230230

231231
// Remove removes the priority value associated with the given key k from the priority queue.
232-
// It's a no-op if there's no such key k in the priority queue.
233-
func (pq *KeyedPriorityQueue[K, V]) Remove(k K) {
232+
// It's a no-op if there's no such key k in the priority queue and it will return false.
233+
func (pq *KeyedPriorityQueue[K, V]) Remove(k K) bool {
234234
pq.mu.Lock()
235235
defer pq.mu.Unlock()
236236

237237
i, ok := pq.im[k]
238238
if !ok {
239-
return
239+
return false
240240
}
241241
n := len(pq.pm) - 1
242242
if i != n {
@@ -247,6 +247,7 @@ func (pq *KeyedPriorityQueue[K, V]) Remove(k K) {
247247
pq.pm = pq.pm[:n]
248248
delete(pq.im, k)
249249
delete(pq.vals, k)
250+
return true
250251
}
251252

252253
// Len returns the size of the priority queue.

kpq/keyed_priority_queue_test.go

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -286,39 +286,60 @@ func TestKeyedPriorityQueue_Remove(t *testing.T) {
286286
t.Run("Keys", func(t *testing.T) {
287287
testCases := []struct {
288288
key string
289+
wantStatus bool
289290
wantPeekKey string
290291
wantPeekValue int
291292
wantLen int
292293
}{
293294
{
294295
key: "first",
296+
wantStatus: true,
295297
wantPeekKey: "second",
296298
wantPeekValue: 8,
297299
wantLen: 4,
298300
},
299301
{
300302
key: "third",
303+
wantStatus: true,
301304
wantPeekKey: "second",
302305
wantPeekValue: 8,
303306
wantLen: 3,
304307
},
305308
{
306309
key: "second",
310+
wantStatus: true,
307311
wantPeekKey: "fourth",
308312
wantPeekValue: 10,
309313
wantLen: 2,
310314
},
311315
{
312316
key: "last",
317+
wantStatus: true,
313318
wantPeekKey: "fourth",
314319
wantPeekValue: 10,
315320
wantLen: 1,
316321
},
322+
{
323+
key: "nonexistent",
324+
wantStatus: false,
325+
wantLen: 1,
326+
},
317327
}
318328

319329
for _, tc := range testCases {
320330
t.Run(tc.key, func(t *testing.T) {
321-
pq.Remove(tc.key)
331+
gotStatus := pq.Remove(tc.key)
332+
if gotStatus != tc.wantStatus {
333+
t.Errorf("pq.Remove(): got status %t; want %t", gotStatus, tc.wantStatus)
334+
}
335+
336+
if got := pq.Len(); got != tc.wantLen {
337+
t.Errorf("pq.Len(): got %d; want %d", got, tc.wantLen)
338+
}
339+
340+
if !gotStatus {
341+
return
342+
}
322343

323344
gotPeekKey, gotPeekValue, ok := pq.Peek()
324345
if !ok {
@@ -332,22 +353,9 @@ func TestKeyedPriorityQueue_Remove(t *testing.T) {
332353
if gotPeekValue != tc.wantPeekValue {
333354
t.Errorf("pq.PeekValue(): got value %d; want %d", gotPeekValue, tc.wantPeekValue)
334355
}
335-
336-
if got := pq.Len(); got != tc.wantLen {
337-
t.Errorf("pq.Len(): got %d; want %d", got, tc.wantLen)
338-
}
339356
})
340357
}
341358
})
342-
343-
t.Run("NonExistingKey", func(t *testing.T) {
344-
want := pq.Len()
345-
pq.Remove("non-existing-key")
346-
347-
if got := pq.Len(); got != want {
348-
t.Errorf("pq.Len(): got %d; want %d", got, want)
349-
}
350-
})
351359
}
352360

353361
func TestKeyedPriorityQueue_Peek_EmptyQueue(t *testing.T) {
@@ -479,7 +487,6 @@ func TestKeyedPriorityQueue_Set(t *testing.T) {
479487
}
480488
})
481489
}
482-
483490
}
484491

485492
func benchmarkKeyedPriorityQueue_PushPop(b *testing.B, n int) {
@@ -500,18 +507,23 @@ func benchmarkKeyedPriorityQueue_PushPop(b *testing.B, n int) {
500507
func BenchmarkKeyedPriorityQueue_PushPop_10(b *testing.B) {
501508
benchmarkKeyedPriorityQueue_PushPop(b, 10)
502509
}
510+
503511
func BenchmarkKeyedPriorityQueue_PushPop_100(b *testing.B) {
504512
benchmarkKeyedPriorityQueue_PushPop(b, 100)
505513
}
514+
506515
func BenchmarkKeyedPriorityQueue_PushPop_1000(b *testing.B) {
507516
benchmarkKeyedPriorityQueue_PushPop(b, 1000)
508517
}
518+
509519
func BenchmarkKeyedPriorityQueue_PushPop_10000(b *testing.B) {
510520
benchmarkKeyedPriorityQueue_PushPop(b, 10000)
511521
}
522+
512523
func BenchmarkKeyedPriorityQueue_PushPop_100000(b *testing.B) {
513524
benchmarkKeyedPriorityQueue_PushPop(b, 100000)
514525
}
526+
515527
func BenchmarkKeyedPriorityQueue_PushPop_1000000(b *testing.B) {
516528
benchmarkKeyedPriorityQueue_PushPop(b, 1000000)
517529
}

0 commit comments

Comments
 (0)