-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcollection.go
41 lines (30 loc) · 1002 Bytes
/
collection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package bricks
import (
"cmp"
"context"
)
// Collection is the set of objects
type Collection[T any] interface {
Contains(e T) bool
}
type ClosedInterval[T cmp.Ordered] [2]T // [min, max]
func (tt ClosedInterval[T]) Contains(e T) bool {
return e >= tt[0] && e <= tt[1]
}
type OpenInterval[T cmp.Ordered] [2]T // (min, max)
func (tt OpenInterval[T]) Contains(e T) bool {
return e > tt[0] && e < tt[1]
}
type LeftOpenInterval[T cmp.Ordered] [2]T // (min, max]
func (tt LeftOpenInterval[T]) Contains(e T) bool {
return e > tt[0] && e <= tt[1]
}
type RightOpenInterval[T cmp.Ordered] [2]T // [min, max)
func (tt RightOpenInterval[T]) Contains(e T) bool {
return e >= tt[0] && e < tt[1]
}
// Bag is a container which items are non-returnable once pulled. Implementation of Pull must be thread-safe.
type Bag[Value any] interface {
// Pull takes out an item in case of existence. It should return ErrReachedEnd when nothing remained to return.
Pull(ctx context.Context) (*Value, error)
}