Skip to content

Added remove by value #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const (
QueueErrorCodeIndexOutOfBounds = "index-out-of-bounds"
QueueErrorCodeFullCapacity = "full-capacity"
QueueErrorCodeInternalChannelClosed = "internal-channel-closed"
QueueErrorCodeValueNotFound = "value-not-found"
)

type QueueError struct {
Expand Down
19 changes: 19 additions & 0 deletions fifo_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,22 @@ func (st *FIFO) IsLocked() bool {

return st.isLocked
}

func (st *FIFO) RemoveByValue(value interface{}) error {
if st.isLocked {
return NewQueueError(QueueErrorCodeLockedQueue, "The queue is locked")
}

st.rwmutex.Lock()
defer st.rwmutex.Unlock()

// Find the first occurrence of the value
for i, item := range st.slice {
if item == value {
st.slice = append(st.slice[:i], st.slice[i+1:]...)
return nil
}
}

return NewQueueError(QueueErrorCodeValueNotFound, fmt.Sprintf("value not found in queue: %v", value))
}