@@ -11,51 +11,66 @@ import (
1111// of live patterns.
1212type State interface {
1313 // Add adds a new pattern or updates an old pattern.
14+ //
15+ // Note that multiple patterns can be associated with the same
16+ // X.
1417 Add (x quamina.X , pattern string ) error
1518
1619 Delete (x quamina.X ) (bool , error )
1720
1821 // Iterate calls the given function for every stored pattern.
1922 Iterate (func (x quamina.X , pattern string ) error ) error
2023
21- // Get returns the pattern for the given X.
24+ // Contains returns true if x is in the live set; false
25+ // otherwise.
2226 //
2327 // Since a pattern can't be the empty string, that zero value
2428 // indicates no corresponding pattern.
25- Get (x quamina.X ) (string , error )
29+ Contains (x quamina.X ) (bool , error )
2630}
2731
32+ type (
33+ stringSet map [string ]nothing
34+ nothing struct {}
35+ )
36+
37+ var na = nothing {}
38+
2839// MemState is a State that is just a map (with a RWMutex).
2940//
3041// Since the State implementation can be provided to the
3142// application, we're keeping things simple here initially.
3243type MemState struct {
3344 lock sync.RWMutex
34- m map [quamina.X ]string
45+ m map [quamina.X ]stringSet
3546}
3647
3748func NewMemState () * MemState {
3849 // Accept initial size as a parameter?
3950 return & MemState {
40- m : make (map [quamina.X ]string ),
51+ m : make (map [quamina.X ]stringSet ),
4152 }
4253}
4354
4455var ErrExists = fmt .Errorf ("pattern already exists for that X" )
4556
4657func (s * MemState ) Add (x quamina.X , pattern string ) error {
4758 s .lock .Lock ()
48- // We don't care if the X is already there.
49- s .m [x ] = pattern
59+ ps , have := s .m [x ]
60+ if ! have {
61+ ps = make (stringSet )
62+ s .m [x ] = ps
63+ }
64+ ps [pattern ] = na
5065 s .lock .Unlock ()
5166 return nil
5267}
5368
54- func (s * MemState ) Get (x quamina.X ) (string , error ) {
69+ func (s * MemState ) Contains (x quamina.X ) (bool , error ) {
5570 s .lock .RLock ()
56- p := s .m [x ]
71+ _ , have := s .m [x ]
5772 s .lock .RUnlock ()
58- return p , nil
73+ return have , nil
5974}
6075
6176func (s * MemState ) Delete (x quamina.X ) (bool , error ) {
@@ -72,9 +87,11 @@ func (s *MemState) Delete(x quamina.X) (bool, error) {
7287func (s * MemState ) Iterate (f func (x quamina.X , pattern string ) error ) error {
7388 s .lock .RLock ()
7489 var err error
75- for x , p := range s .m {
76- if err = f (x , p ); err != nil {
77- break
90+ for x , ps := range s .m {
91+ for p := range ps {
92+ if err = f (x , p ); err != nil {
93+ break
94+ }
7895 }
7996 }
8097 s .lock .RUnlock ()
0 commit comments