diff --git a/v2/README.md b/v2/README.md index bcbde72..84fbe41 100644 --- a/v2/README.md +++ b/v2/README.md @@ -81,12 +81,6 @@ func (dq *Deque[T]) Front() (_ T, ok bool) Front returns the first value of dq if any. The return value ok indicates whether it succeeded. -func (dq *Deque[T]) IsEmpty() bool - IsEmpty returns whether dq is empty. - -func (dq *Deque[T]) Len() int - Len returns the number of values in dq. - func (dq *Deque[T]) Enqueue(v T) Enqueue is an alias of PushBack. @@ -106,15 +100,38 @@ func (dq *Deque[T]) DequeueManyWithBuffer(max int, buf []T) []T DequeueManyWithBuffer is similar to DequeueMany except that it uses buf to store the removed values as long as it has enough space. +func (dq *Deque[T]) Insert(idx int, v T) + Insert inserts a new value v before the value at idx. + + Insert may cause the split of a chunk inside dq. Because the size of a chunk + is fixed, the amount of time taken by Insert has a reasonable limit. + +func (dq *Deque[T]) Remove(idx int) + Remove removes the value at idx. It panics if idx is out of range. + +func (dq *Deque[T]) Replace(idx int, v T) + Replace replaces the value at idx with v. It panics if idx is out of range. + +func (dq *Deque[T]) Swap(idx1, idx2 int) + Swap exchanges the two values at idx1 and idx2. It panics if idx1 or idx2 is + out of range. + +func (dq *Deque[T]) Clear() + Clear removes all the values from dq. + +func (dq *Deque[T]) IsEmpty() bool + IsEmpty returns whether dq is empty. + +func (dq *Deque[T]) Len() int + Len returns the number of values in dq. + func (dq *Deque[T]) Range(f func(i int, v T) bool) - Range iterates all the values in dq. + Range iterates all the values in dq. Do NOT add values to dq or remove + values from dq during Range. func (dq *Deque[T]) Peek(idx int) T Peek returns the value at idx. It panics if idx is out of range. -func (dq *Deque[T]) Replace(idx int, v T) - Replace replaces the value at idx. It panics if idx is out of range. - func (dq *Deque[T]) Dump() []T Dump returns all the values in dq. ``` \ No newline at end of file diff --git a/v2/deque.go b/v2/deque.go index dde8d56..a9d39a5 100644 --- a/v2/deque.go +++ b/v2/deque.go @@ -412,7 +412,7 @@ func (dq *Deque[T]) Dump() []T { return vals } -// Range iterates all the values in dq. Do NOT add or remove elements during Range. +// Range iterates all the values in dq. Do NOT add values to dq or remove values from dq during Range. func (dq *Deque[T]) Range(f func(i int, v T) bool) { var i int for _, c := range dq.chunks { @@ -501,7 +501,7 @@ func (dq *Deque[T]) Swap(idx1, idx2 int) { // Insert inserts a new value v before the value at idx. // // Insert may cause the split of a chunk inside dq. Because the size of a chunk is fixed, -// the amount of time taken by Insert does not grow with the length of dq. +// the amount of time taken by Insert has a reasonable limit. func (dq *Deque[T]) Insert(idx int, v T) { if idx <= 0 { dq.PushFront(v) @@ -743,6 +743,7 @@ func (dq *Deque[T]) mergeChunks(j int) { } } +// Clear removes all the values from dq. func (dq *Deque[T]) Clear() { var defVal T for _, c := range dq.chunks {