Generic Data Structures
Auto-scalable Ring Buffer, Double Ended Queue
Ringbuf is auto-scalable ring buffer. Ringbuf also implements deque (Double Ended Queue).
It’s better to implement deque based on ringbuf instead of slice or list.
- In contrast to slice, ringbuf can reuse memory block of removed elements, and therefore less memory allocation and less GC.
Another disadvantage of slice is that memory block can not be recycled in time. Even though a part of a slice’s underlying array won’t be reused, that part can not be recycled instantly, because the underlying array is referenced by the slice as a whole. - In contrast to list, ringbuf has continuous memory space, no front and back pointers, and therefore higher access speed and less memory footprint.
Import
API:
PushBack, PushFront, PeekBack, PeekFront, PopBack, PopFront, Len, Range
Sample:
ringbuf
↗
Queue
Queue is FIFO, a subset of deque.
Import
API:
Enq, Peek, Deq, Len, Range
Sample:
queue
↗
Stack
Stack is LIFO, another subset of deque.
Import
API:
Push, Peek, Pop, Len, Range
Sample:
stack
↗
Heap
Import
API:
Push, Pop, Len, Range
Sample:
heap
↗
Set
Import
API:
Add, Delete, Len, Contain, Equal, Range, Union, Intersect, Diff
Sample:
set
↗
Benchmark
Note: In each of the following benchmark results, the second colume (N) represents collection size, i.e., the number of elements in a collection.
Ring Buffer, Double Ended Queue:
Queue:
Stack:
Heap:
Set: