1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge remote-tracking branch 'fork/stack_set' into move-networking

Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)
This commit is contained in:
Michael Crosby 2014-01-22 16:13:59 -08:00
commit e048c28159
2 changed files with 63 additions and 1 deletions

View file

@ -5,11 +5,11 @@ import (
"errors"
"github.com/dotcloud/docker/pkg/netlink"
"net"
"sort"
"sync"
)
type networkSet map[iPNet]iPSet
type iPSet map[string]struct{}
type iPNet struct {
IP string

View file

@ -0,0 +1,62 @@
package ipallocator
import (
"sync"
)
// iPSet is a thread-safe sorted set and a stack.
type iPSet struct {
sync.RWMutex
set []string
}
// Push takes a string and adds it to the set. If the elem aready exists, it has no effect.
func (s *iPSet) Push(elem string) {
s.RLock()
for i, e := range s.set {
if e == elem {
s.RUnlock()
return
}
}
s.RUnlock()
s.Lock()
s.set = append(s.set, elem)
// Make sure the list is always sorted
sort.Strings(s.set)
s.Unlock()
}
// Pop returns the first elemen from the list and removes it.
// If the list is empty, it returns an empty string
func (s *iPSet) Pop() string {
s.RLock()
for i, e := range s.set {
ret := e
s.RUnlock()
s.Lock()
s.set = append(s.set[:i], s.set[i+1:]...)
s.Unlock()
return e
}
s.RUnlock()
return ""
}
// Remove removes an element from the list.
// If the element is not found, it has no effect.
func (s *iPSet) Remove(elem string) {
for i, e := range s.set {
if e == elem {
s.set = append(s.set[:i], s.set[i+1:]...)
return
}
}
}
// Len returns the length of the list.
func (s *iPSet) Len() int {
return len(s.set)
}