github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/util/sets/byte.go (about) 1 /* 2 Copyright 2015 The Kubernetes Authors All rights reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 // This file was autogenerated by the command: 18 // $ cmd/libs/go2idl/set-gen/set-gen 19 // Do not edit it manually! 20 21 package sets 22 23 import ( 24 "reflect" 25 "sort" 26 ) 27 28 // sets.Byte is a set of bytes, implemented via map[byte]struct{} for minimal memory consumption. 29 type Byte map[byte]Empty 30 31 // New creates a Byte from a list of values. 32 func NewByte(items ...byte) Byte { 33 ss := Byte{} 34 ss.Insert(items...) 35 return ss 36 } 37 38 // ByteKeySet creates a Byte from a keys of a map[byte](? extends interface{}). 39 // If the value passed in is not actually a map, this will panic. 40 func ByteKeySet(theMap interface{}) Byte { 41 v := reflect.ValueOf(theMap) 42 ret := Byte{} 43 44 for _, keyValue := range v.MapKeys() { 45 ret.Insert(keyValue.Interface().(byte)) 46 } 47 return ret 48 } 49 50 // Insert adds items to the set. 51 func (s Byte) Insert(items ...byte) { 52 for _, item := range items { 53 s[item] = Empty{} 54 } 55 } 56 57 // Delete removes all items from the set. 58 func (s Byte) Delete(items ...byte) { 59 for _, item := range items { 60 delete(s, item) 61 } 62 } 63 64 // Has returns true if and only if item is contained in the set. 65 func (s Byte) Has(item byte) bool { 66 _, contained := s[item] 67 return contained 68 } 69 70 // HasAll returns true if and only if all items are contained in the set. 71 func (s Byte) HasAll(items ...byte) bool { 72 for _, item := range items { 73 if !s.Has(item) { 74 return false 75 } 76 } 77 return true 78 } 79 80 // HasAny returns true if any items are contained in the set. 81 func (s Byte) HasAny(items ...byte) bool { 82 for _, item := range items { 83 if s.Has(item) { 84 return true 85 } 86 } 87 return false 88 } 89 90 // Difference returns a set of objects that are not in s2 91 // For example: 92 // s1 = {a1, a2, a3} 93 // s2 = {a1, a2, a4, a5} 94 // s1.Difference(s2) = {a3} 95 // s2.Difference(s1) = {a4, a5} 96 func (s Byte) Difference(s2 Byte) Byte { 97 result := NewByte() 98 for key := range s { 99 if !s2.Has(key) { 100 result.Insert(key) 101 } 102 } 103 return result 104 } 105 106 // Union returns a new set which includes items in either s1 or s2. 107 // For example: 108 // s1 = {a1, a2} 109 // s2 = {a3, a4} 110 // s1.Union(s2) = {a1, a2, a3, a4} 111 // s2.Union(s1) = {a1, a2, a3, a4} 112 func (s1 Byte) Union(s2 Byte) Byte { 113 result := NewByte() 114 for key := range s1 { 115 result.Insert(key) 116 } 117 for key := range s2 { 118 result.Insert(key) 119 } 120 return result 121 } 122 123 // Intersection returns a new set which includes the item in BOTH s1 and s2 124 // For example: 125 // s1 = {a1, a2} 126 // s2 = {a2, a3} 127 // s1.Intersection(s2) = {a2} 128 func (s1 Byte) Intersection(s2 Byte) Byte { 129 var walk, other Byte 130 result := NewByte() 131 if s1.Len() < s2.Len() { 132 walk = s1 133 other = s2 134 } else { 135 walk = s2 136 other = s1 137 } 138 for key := range walk { 139 if other.Has(key) { 140 result.Insert(key) 141 } 142 } 143 return result 144 } 145 146 // IsSuperset returns true if and only if s1 is a superset of s2. 147 func (s1 Byte) IsSuperset(s2 Byte) bool { 148 for item := range s2 { 149 if !s1.Has(item) { 150 return false 151 } 152 } 153 return true 154 } 155 156 // Equal returns true if and only if s1 is equal (as a set) to s2. 157 // Two sets are equal if their membership is identical. 158 // (In practice, this means same elements, order doesn't matter) 159 func (s1 Byte) Equal(s2 Byte) bool { 160 return len(s1) == len(s2) && s1.IsSuperset(s2) 161 } 162 163 type sortableSliceOfByte []byte 164 165 func (s sortableSliceOfByte) Len() int { return len(s) } 166 func (s sortableSliceOfByte) Less(i, j int) bool { return lessByte(s[i], s[j]) } 167 func (s sortableSliceOfByte) Swap(i, j int) { s[i], s[j] = s[j], s[i] } 168 169 // List returns the contents as a sorted byte slice. 170 func (s Byte) List() []byte { 171 res := make(sortableSliceOfByte, 0, len(s)) 172 for key := range s { 173 res = append(res, key) 174 } 175 sort.Sort(res) 176 return []byte(res) 177 } 178 179 // Returns a single element from the set. 180 func (s Byte) PopAny() (byte, bool) { 181 for key := range s { 182 s.Delete(key) 183 return key, true 184 } 185 var zeroValue byte 186 return zeroValue, false 187 } 188 189 // Len returns the size of the set. 190 func (s Byte) Len() int { 191 return len(s) 192 } 193 194 func lessByte(lhs, rhs byte) bool { 195 return lhs < rhs 196 }