| 1 | package offers |
| 2 | |
| 3 | import "github.com/mesos/mesos-go/api/v1/lib" |
| 4 | |
| 5 | type ( |
| 6 | // Slice is a convenience type wrapper for a slice of mesos Offer messages |
| 7 | Slice []mesos.Offer |
| 8 | |
| 9 | // Index is a convenience type wrapper for a dictionary of Offer messages |
| 10 | Index map[interface{}]*mesos.Offer |
| 11 | |
| 12 | // KeyFunc generates a key used for indexing offers |
| 13 | KeyFunc func(*mesos.Offer) interface{} |
| 14 | ) |
| 15 | |
| 16 | // IDs extracts the ID field from a Slice of offers |
| 17 | func (offers Slice) IDs() []mesos.OfferID { |
| 18 | ids := make([]mesos.OfferID, len(offers)) |
| 19 | for i := range offers { |
| 20 | ids[i] = offers[i].ID |
| 21 | } |
| 22 | return ids |
| 23 | } |
| 24 | |
| 25 | // IDs extracts the ID field from a Index of offers |
| 26 | func (offers Index) IDs() []mesos.OfferID { |
| 27 | ids := make([]mesos.OfferID, 0, len(offers)) |
| 28 | for _, offer := range offers { |
| 29 | ids = append(ids, offer.GetID()) |
| 30 | } |
| 31 | return ids |
| 32 | } |
| 33 | |
| 34 | // Find returns the first Offer that passes the given filter function, or else nil if |
| 35 | // there are no passing offers. |
| 36 | func (offers Slice) Find(filter Filter) *mesos.Offer { |
| 37 | for i := range offers { |
| 38 | offer := &offers[i] |
| 39 | if filter.Accept(offer) { |
| 40 | return offer |
| 41 | } |
| 42 | } |
| 43 | return nil |
| 44 | } |
| 45 | |
| 46 | // Find returns the first Offer that passes the given filter function, or else nil if |
| 47 | // there are no passing offers. |
| 48 | func (offers Index) Find(filter Filter) *mesos.Offer { |
| 49 | for _, offer := range offers { |
| 50 | if filter.Accept(offer) { |
| 51 | return offer |
| 52 | } |
| 53 | } |
| 54 | return nil |
| 55 | } |
| 56 | |
| 57 | // Filter returns the subset of the Slice that matches the given filter. |
| 58 | func (offers Slice) Filter(filter Filter) (result Slice) { |
| 59 | if sz := len(result); sz > 0 { |
| 60 | result = make(Slice, 0, sz) |
| 61 | for i := range offers { |
| 62 | if filter.Accept(&offers[i]) { |
| 63 | result = append(result, offers[i]) |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | return |
| 68 | } |
| 69 | |
| 70 | // Filter returns the subset of the Index that matches the given filter. |
| 71 | func (offers Index) Filter(filter Filter) (result Index) { |
| 72 | if sz := len(result); sz > 0 { |
| 73 | result = make(Index, sz) |
| 74 | for id, offer := range offers { |
| 75 | if filter.Accept(offer) { |
| 76 | result[id] = offer |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | return |
| 81 | } |
| 82 | |
| 83 | // FilterNot returns the subset of the Slice that does not match the given filter. |
| 84 | func (offers Slice) FilterNot(filter Filter) Slice { return offers.Filter(not(filter)) } |
| 85 | |
| 86 | // FilterNot returns the subset of the Index that does not match the given filter. |
| 87 | func (offers Index) FilterNot(filter Filter) Index { return offers.Filter(not(filter)) } |
| 88 | |
| 89 | // DefaultKeyFunc indexes offers by their OfferID. |
| 90 | var DefaultKeyFunc = KeyFunc(KeyFuncByOfferID) |
| 91 | |
| 92 | func KeyFuncByOfferID(o *mesos.Offer) interface{} { return o.GetID() } |
| 93 | |
| 94 | // NewIndex returns a new Index constructed from the list of mesos offers. |
| 95 | // If the KeyFunc is nil then offers are indexed by DefaultKeyFunc. |
| 96 | // The values of the returned Index are pointers to (not copies of) the offers of the slice receiver. |
| 97 | func NewIndex(slice []mesos.Offer, kf KeyFunc) Index { |
| 98 | if slice == nil { |
| 99 | return nil |
| 100 | } |
| 101 | if kf == nil { |
| 102 | kf = DefaultKeyFunc |
| 103 | } |
| 104 | index := make(Index, len(slice)) |
| 105 | for i := range slice { |
| 106 | offer := &slice[i] |
| 107 | index[kf(offer)] = offer |
| 108 | } |
| 109 | return index |
| 110 | } |
| 111 | |
| 112 | // ToSlice returns a Slice from the offers in the Index. |
| 113 | // The returned slice will contain shallow copies of the offers from the Index. |
| 114 | func (offers Index) ToSlice() (slice Slice) { |
| 115 | if sz := len(offers); sz > 0 { |
| 116 | slice = make(Slice, 0, sz) |
| 117 | for _, offer := range offers { |
| 118 | slice = append(slice, *offer) |
| 119 | } |
| 120 | } |
| 121 | return |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | |
| 126 | type Reducer func(_, _ *Offer) *Offer |
| 127 | |
| 128 | func (slice Slice) Reduce(r Reducer) (result Offer) { |
| 129 | if r == nil { |
| 130 | return |
| 131 | } |
| 132 | acc := &result |
| 133 | for i := range slice { |
| 134 | acc = r(&result, &slice[i]) |
| 135 | } |
| 136 | if acc == nil { |
| 137 | result = Offer{} |
| 138 | } else { |
| 139 | result = *acc |
| 140 | } |
| 141 | return |
| 142 | } |
| 143 | |
| 144 | func (index Index) Reduce(r Reducer) (result *Offer) { |
| 145 | if r == nil { |
| 146 | return |
| 147 | } |
| 148 | for i := range index { |
| 149 | result = r(result, index[i]) |
| 150 | } |
| 151 | return |
| 152 | } |
| 153 | |
| 154 | func (slice Slice) GroupBy(kf KeyFunc) map[interface{}]Slice { |
| 155 | if kf == nil { |
| 156 | panic("keyFunc must not be nil") |
| 157 | } |
| 158 | if len(slice) == 0 { |
| 159 | return nil |
| 160 | } |
| 161 | result := make(map[interface{}]Slice) |
| 162 | for i := range slice { |
| 163 | groupKey := kf(&slice[i]) |
| 164 | result[groupKey] = append(result[groupKey], slice[i]) |
| 165 | } |
| 166 | return result |
| 167 | } |
| 168 | |
| 169 | func (index Index) GroupBy(kf KeyFunc) map[interface{}]Index { |
| 170 | if kf == nil { |
| 171 | panic("keyFunc must not be nil") |
| 172 | } |
| 173 | if len(index) == 0 { |
| 174 | return nil |
| 175 | } |
| 176 | result := make(map[interface{}]Index) |
| 177 | for i, offer := range index { |
| 178 | groupKey := kf(offer) |
| 179 | group, ok := result[groupKey] |
| 180 | if !ok { |
| 181 | group = make(Index) |
| 182 | result[groupKey] = group |
| 183 | } |
| 184 | group[i] = offer |
| 185 | } |
| 186 | return result |
| 187 | } |
| 188 | |
| 189 | func (index Index) Partition(f Filter) (accepted, rejected Index) { |
| 190 | if f == nil { |
| 191 | return index, nil |
| 192 | } |
| 193 | if len(index) > 0 { |
| 194 | accepted, rejected = make(Index), make(Index) |
| 195 | for id, offer := range index { |
| 196 | if f.Accept(offer) { |
| 197 | accepted[id] = offer |
| 198 | } else { |
| 199 | rejected[id] = offer |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | return |
| 204 | } |
| 205 | |
| 206 | func (s Slice) Partition(f Filter) (accepted, rejected []int) { |
| 207 | if f == nil { |
| 208 | accepted = make([]int, len(s)) |
| 209 | for i := range s { |
| 210 | accepted[i] = i |
| 211 | } |
| 212 | return |
| 213 | } |
| 214 | if sz := len(s); sz > 0 { |
| 215 | accepted, rejected = make([]int, 0, sz/2), make([]int, 0, sz/2) |
| 216 | for i := range s { |
| 217 | offer := &s[i] |
| 218 | if f.Accept(offer) { |
| 219 | accepted = append(accepted, i) |
| 220 | } else { |
| 221 | rejected = append(rejected, i) |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | return |
| 226 | } |
| 227 | |
| 228 | func (index Index) Reindex(kf KeyFunc) Index { |
| 229 | sz := len(index) |
| 230 | if kf == nil || sz == 0 { |
| 231 | return index |
| 232 | } |
| 233 | result := make(Index, sz) |
| 234 | for _, offer := range index { |
| 235 | key := kf(offer) |
| 236 | result[key] = offer |
| 237 | } |
| 238 | return result |
| 239 | } |
| 240 | */ |