2013-02-07 10:38:01 +00:00
|
|
|
// Copyright 2013 Prometheus Team
|
2012-11-26 19:11:34 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2012-11-24 11:33:34 +00:00
|
|
|
package utility
|
|
|
|
|
2014-03-28 10:58:47 +00:00
|
|
|
// Set is a type which models a set.
|
|
|
|
type Set map[interface{}]struct{}
|
2012-11-24 11:33:34 +00:00
|
|
|
|
2014-03-28 10:58:47 +00:00
|
|
|
// Add adds an item to the set.
|
2012-11-24 11:33:34 +00:00
|
|
|
func (s Set) Add(v interface{}) {
|
2014-03-28 10:58:47 +00:00
|
|
|
s[v] = struct{}{}
|
2012-11-24 11:33:34 +00:00
|
|
|
}
|
|
|
|
|
2014-03-28 10:58:47 +00:00
|
|
|
// Remove removes an item from the set.
|
2012-11-24 11:33:34 +00:00
|
|
|
func (s Set) Remove(v interface{}) {
|
|
|
|
delete(s, v)
|
|
|
|
}
|
|
|
|
|
2014-03-28 10:58:47 +00:00
|
|
|
// Elements returns a slice containing all elements in the set.
|
2012-11-24 11:33:34 +00:00
|
|
|
func (s Set) Elements() []interface{} {
|
|
|
|
result := make([]interface{}, 0, len(s))
|
|
|
|
|
2012-11-29 19:55:30 +00:00
|
|
|
for k := range s {
|
2012-11-24 11:33:34 +00:00
|
|
|
result = append(result, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2014-03-28 10:58:47 +00:00
|
|
|
// Has returns true if an element is contained in the set.
|
2012-11-24 11:33:34 +00:00
|
|
|
func (s Set) Has(v interface{}) bool {
|
|
|
|
_, p := s[v]
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2014-03-28 10:58:47 +00:00
|
|
|
// Intersection returns a new set with items that exist in both sets.
|
2012-11-24 11:33:34 +00:00
|
|
|
func (s Set) Intersection(o Set) Set {
|
2014-03-28 10:58:47 +00:00
|
|
|
result := Set{}
|
2012-11-24 11:33:34 +00:00
|
|
|
|
2012-11-29 19:55:30 +00:00
|
|
|
for k := range s {
|
2012-11-24 11:33:34 +00:00
|
|
|
if o.Has(k) {
|
2014-03-28 10:58:47 +00:00
|
|
|
result[k] = struct{}{}
|
2012-11-24 11:33:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|