Skip to content
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

Ideas for additional methods #35

Open
gammazero opened this issue Nov 15, 2024 · 1 comment
Open

Ideas for additional methods #35

gammazero opened this issue Nov 15, 2024 · 1 comment

Comments

@gammazero
Copy link
Owner

Continued from #28, from @rickb777

Here are some ideas for methods that you could add:

  • push and insert slices - i.e. internalise the loop needed and reduce loop overhead
  • add adaptors for the PopBack, PopFront and At methods to provide iterators (Go 1.23)
  • add Full() bool method - a shortcut for q.Len() == q.Cap() which would be useful for anyone wanting to impose a maximum size or avoid resizing
@gammazero
Copy link
Owner Author

gammazero commented Nov 15, 2024

@rickb777
A PushSlice and InsertSlice are probably not needed, since they seem like an uncommon operation and their purpose, aside from some syntactic convenience, is to avoid possible multiple resizes when adding a number of items. The caller can already do this using the Grow function:

q.Grow(len(elems))
for i := range elems {
    q.PushBack(elems[i])
}

Adding iterator adaptors IterPopFront and IterPopBack, might be unnecessary when the caller can already do:

for q.Len() != 0 {
    process(q.PopFront())
}

The iterator looks like:

for item := range IterPopFront() {
    process(item)
}

So it does not really simplify the code. However, it may still be worth adding these iterators to eliminate intermediate resizes during iteration. Somewhat like the inverse of Grow. See PR #32.

As for a Full function, it seems like it is syntactic sugar for something that is likely not very common. I think if q.Len() == q.Cap() is a bit more explicit. If a size limit is desired them it might be better wrap Deque in a type that checks if Len is at the limit when adding items. The wrapper's PushBask can then return a bool or error to indicate that the limit has been reached. If it is necessary to block on a write to a full Deque, or a read from an empty Deque, then channelqueue does exactly this. WDYT?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant