File: sets.go

package info (click to toggle)
golang-github-tideland-golib 4.24.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,144 kB
  • sloc: makefile: 4
file content (194 lines) | stat: -rw-r--r-- 4,014 bytes parent folder | download | duplicates (3)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Tideland Go Library - Collections - Sets
//
// Copyright (C) 2015 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.

package collections

//--------------------
// IMPORTS
//--------------------

import (
	"fmt"

	"github.com/tideland/golib/errors"
)

//--------------------
// SET
//--------------------

// set implements the Set interface.
type set struct {
	values map[interface{}]struct{}
}

// NewSet creates a set with the passed values
// as initial content.
func NewSet(vs ...interface{}) Set {
	s := &set{make(map[interface{}]struct{})}
	s.Add(vs...)
	return s
}

// Add implements the Set interface.
func (s *set) Add(vs ...interface{}) {
	for _, v := range vs {
		s.values[v] = struct{}{}
	}
}

// Remove implements the Set interface.
func (s *set) Remove(vs ...interface{}) {
	for _, v := range vs {
		delete(s.values, v)
	}
}

// Contains implements the Set interface.
func (s *set) Contains(v interface{}) bool {
	_, ok := s.values[v]
	return ok
}

// All implements the Set interface.
func (s *set) All() []interface{} {
	all := []interface{}{}
	for v := range s.values {
		all = append(all, v)
	}
	return all
}

// FindAll implements the Set interface.
func (s *set) FindAll(f func(v interface{}) (bool, error)) ([]interface{}, error) {
	found := []interface{}{}
	for v := range s.values {
		ok, err := f(v)
		if err != nil {
			return nil, errors.Annotate(err, ErrFindAll, errorMessages)
		}
		if ok {
			found = append(found, v)
		}
	}
	return found, nil
}

// DoAll implements the Set interface.
func (s *set) DoAll(f func(v interface{}) error) error {
	for v := range s.values {
		if err := f(v); err != nil {
			return errors.Annotate(err, ErrDoAll, errorMessages)
		}
	}
	return nil
}

// Len implements the Set interface.
func (s *set) Len() int {
	return len(s.values)
}

// Deflate implements the Set interface.
func (s *set) Deflate() {
	s.values = make(map[interface{}]struct{})
}

// Deflate implements the Stringer interface.
func (s *set) String() string {
	all := s.All()
	return fmt.Sprintf("%v", all)
}

//--------------------
// STRING SET
//--------------------

// stringSet implements the StringSet interface.
type stringSet struct {
	values map[string]struct{}
}

// NewStringSet creates a string set with the passed values
// as initial content.
func NewStringSet(vs ...string) StringSet {
	s := &stringSet{make(map[string]struct{})}
	s.Add(vs...)
	return s
}

// Add implements the StringSet interface.
func (s *stringSet) Add(vs ...string) {
	for _, v := range vs {
		s.values[v] = struct{}{}
	}
}

// Remove implements the StringSet interface.
func (s *stringSet) Remove(vs ...string) {
	for _, v := range vs {
		delete(s.values, v)
	}
}

// Contains implements the StringSet interface.
func (s *stringSet) Contains(v string) bool {
	_, ok := s.values[v]
	return ok
}

// All implements the StringSet interface.
func (s *stringSet) All() []string {
	all := []string{}
	for v := range s.values {
		all = append(all, v)
	}
	return all
}

// FindAll implements the StringSet interface.
func (s *stringSet) FindAll(f func(v string) (bool, error)) ([]string, error) {
	found := []string{}
	for v := range s.values {
		ok, err := f(v)
		if err != nil {
			return nil, errors.Annotate(err, ErrFindAll, errorMessages)
		}
		if ok {
			found = append(found, v)
		}
	}
	return found, nil
}

// DoAll implements the StringSet interface.
func (s *stringSet) DoAll(f func(v string) error) error {
	for v := range s.values {
		if err := f(v); err != nil {
			return errors.Annotate(err, ErrDoAll, errorMessages)
		}
	}
	return nil
}

// Len implements the StringSet interface.
func (s *stringSet) Len() int {
	return len(s.values)
}

// Deflate implements the StringSet interface.
func (s *stringSet) Deflate() {
	s.values = make(map[string]struct{})
}

// Deflate implements the Stringer interface.
func (s *stringSet) String() string {
	all := s.All()
	return fmt.Sprintf("%v", all)
}

// EOF