Skip to content

Commit 3e9bcb2

Browse files
committed
Added remove by value
Signed-off-by: Afek Berger <[email protected]>
1 parent c09fe97 commit 3e9bcb2

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

error.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const (
66
QueueErrorCodeIndexOutOfBounds = "index-out-of-bounds"
77
QueueErrorCodeFullCapacity = "full-capacity"
88
QueueErrorCodeInternalChannelClosed = "internal-channel-closed"
9+
QueueErrorCodeValueNotFound = "value-not-found"
910
)
1011

1112
type QueueError struct {

fifo_queue.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,22 @@ func (st *FIFO) IsLocked() bool {
259259

260260
return st.isLocked
261261
}
262+
263+
func (st *FIFO) RemoveByValue(value interface{}) error {
264+
if st.isLocked {
265+
return NewQueueError(QueueErrorCodeLockedQueue, "The queue is locked")
266+
}
267+
268+
st.rwmutex.Lock()
269+
defer st.rwmutex.Unlock()
270+
271+
// Find the first occurrence of the value
272+
for i, item := range st.slice {
273+
if item == value {
274+
st.slice = append(st.slice[:i], st.slice[i+1:]...)
275+
return nil
276+
}
277+
}
278+
279+
return NewQueueError(QueueErrorCodeValueNotFound, fmt.Sprintf("value not found in queue: %v", value))
280+
}

0 commit comments

Comments
 (0)