@@ -17,17 +17,21 @@ making it suitable both for highest performance (in-place), and for LRU caches (
1717
1818See the available types by underlying storage
1919
20- | Type | Slice | Map | List | List+sync.Pool | List+int. pool | Recommended |
21- | ------------| :-----:| :---:| :----:| :--------------:| :--------------:| ----------------------|
22- | OrderedMap | Y | | | | | Slice with size hint |
23- | Queue | Y | | Y | Y | Y | Slice with size hint |
24- | Set | | Y | | | | Map with size hint |
25- | Stack | Y | | Y | Y | Y | Slice with size hint |
26-
27- ** CAVEAT** : In order to optimize performance,
20+ | Type | Slice | Map | List | List+sync.Pool | List+int. pool | Recommended |
21+ | ---------------| :-----:| :---:| :----:| :--------------:| :--------------:| ----------------------|
22+ | OrderedMap | Y | | | | | Slice with size hint |
23+ | Queue | Y | | Y | Y | Y | Slice with size hint |
24+ | WaitableQueue | Y | | | | | Slice with size hint |
25+ | Set | | Y | | | | Map with size hint |
26+ | Stack | Y | | Y | Y | Y | Slice with size hint |
27+
28+
29+ ** CAVEAT** : In order to optimize performance, except for WaitableQueue,
2830all of these implementations are unsafe for concurrent execution,
2931so they need protection in concurrency situations.
3032
33+ WaitableQueue being designed for concurrent code, on the other hand, is concurrency-safe.
34+
3135Generally speaking, in terms of performance:
3236
3337- Slice > list+internal pool > plain List > list+sync.Pool
@@ -51,23 +55,44 @@ om := orderedmap.NewSlice[Key, Value](sizeHint, stable)
5155om.Store (k, v)
5256om.Range (func (k K , v V ) bool { fmt.Println (k, v); return true })
5357v , loaded := om.Load (k)
54- if !loaded { fmt.Printf (" No entry for key %v \n " , k)}
58+ if !loaded {
59+ fmt.Fprintf (w, " No entry for key %v \n " , k)
60+ }
5561om.Delete (k) // Idempotent: does not fail on nonexistent keys.
5662```
5763
58- ### Queues
64+ ### Classic Queues without flow control
5965
6066``` go
6167var e Element
6268q := queue.NewSliceQueue [Element](sizeHint)
6369q.Enqueue (e)
6470if lq , ok := q.(container.Countable ); ok {
65- fmt.Printf ( " elements in queue: %d \n " , lq.Len ())
71+ fmt.Fprintf (w, " elements in queue: %d \n " , lq.Len ())
6672}
6773for i := 0 ; i < 2 ; i++ {
68- e , ok := q.Dequeue ()
69- fmt.Printf (" Element: %v , ok: %t \n " , e, ok)
74+ e , ok := q.Dequeue ()
75+ fmt.Fprintf (w, " Element: %v , ok: %t \n " , e, ok)
76+ }
77+ ```
78+
79+ ### WaitableQueue: a concurrent queue with flow control
80+
81+ ``` go
82+ var e Element
83+ q , _ := queue.NewWaitableQueue [Element](sizeHint, lowWatermark, highWatermark)
84+ go func () {
85+ wqs := q.Enqueue (e)
86+ if lq , ok := q.(container.Countable ); ok {
87+ fmt.Fprintf (w, " elements in queue: %d , status: %s \n " , lq.Len (), wqs)
88+ }
89+ }
90+ <- q.WaitChan () // Wait for elements to be available to dequeue
91+ for i := 0 ; i < 2 ; i++ { // Then dequeue them
92+ e , ok , wqs := q.Dequeue () // Non-blocking, ok will be true for the first and false for the second
93+ fmt.Fprintf (w, " Element: %v , ok: %t , status: %s \n " , e, ok, wqs)
7094}
95+ q.Close () // Only needed if consumers may still be waiting on <-q.WaitChan
7196```
7297
7398### Sets
@@ -78,10 +103,10 @@ s := set.NewBasicMap[Element](sizeHint)
78103s.Add (e)
79104s.Add (e)
80105if cs , ok := q.(container.Countable ); ok {
81- fmt.Printf ( " elements in set: %d \n " , cs.Len ()) // 1
106+ fmt.Fprintf (w, " elements in set: %d \n " , cs.Len ()) // 1
82107}
83108for e := range s.Items () {
84- fmt.Fprintln (w, e)
109+ fmt.Fprintln (w, e)
85110}
86111
87112```
@@ -92,11 +117,11 @@ fmt.Fprintln(w, e)
92117s := stack.NewSliceStack [Element](sizeHint)
93118s.Push (e)
94119if ls , ok := s.(container.Countable ); ok {
95- fmt.Printf (" elements in stack: %d \n " , ls.Len ())
120+ fmt.Printf (" elements in stack: %d \n " , ls.Len ())
96121}
97122for i := 0 ; i < 2 ; i++ {
98- e , ok := s.Pop ()
99- fmt.Printf (" Element: %v , ok: %t \n " , e, ok)
123+ e , ok := s.Pop ()
124+ fmt.Printf (" Element: %v , ok: %t \n " , e, ok)
100125}
101126```
102127
0 commit comments