mirror of https://github.com/docker/docs.git
Use int32 instead of string for ip set
Docker-DCO-1.1-Signed-off-by: Guillaume J. Charmes <guillaume.charmes@docker.com> (github: creack)
This commit is contained in:
parent
18df387bf8
commit
3e3abdd770
|
|
@ -1,17 +1,18 @@
|
||||||
package ipallocator
|
package ipallocator
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sort"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
// iPSet is a thread-safe sorted set and a stack.
|
// iPSet is a thread-safe sorted set and a stack.
|
||||||
type iPSet struct {
|
type iPSet struct {
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
set []string
|
set []int32
|
||||||
}
|
}
|
||||||
|
|
||||||
// Push takes a string and adds it to the set. If the elem aready exists, it has no effect.
|
// 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) {
|
func (s *iPSet) Push(elem int32) {
|
||||||
s.RLock()
|
s.RLock()
|
||||||
for i, e := range s.set {
|
for i, e := range s.set {
|
||||||
if e == elem {
|
if e == elem {
|
||||||
|
|
@ -24,18 +25,18 @@ func (s *iPSet) Push(elem string) {
|
||||||
s.Lock()
|
s.Lock()
|
||||||
s.set = append(s.set, elem)
|
s.set = append(s.set, elem)
|
||||||
// Make sure the list is always sorted
|
// Make sure the list is always sorted
|
||||||
sort.Strings(s.set)
|
sort.Ints(s.set)
|
||||||
s.Unlock()
|
s.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pop is an alias to PopFront()
|
// Pop is an alias to PopFront()
|
||||||
func (s *iPSet) Pop() string {
|
func (s *iPSet) Pop() int32 {
|
||||||
return s.PopFront()
|
return s.PopFront()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pop returns the first elemen from the list and removes it.
|
// Pop returns the first elemen from the list and removes it.
|
||||||
// If the list is empty, it returns an empty string
|
// If the list is empty, it returns 0
|
||||||
func (s *iPSet) PopFront() string {
|
func (s *iPSet) PopFront() int32 {
|
||||||
s.RLock()
|
s.RLock()
|
||||||
|
|
||||||
for i, e := range s.set {
|
for i, e := range s.set {
|
||||||
|
|
@ -53,7 +54,7 @@ func (s *iPSet) PopFront() string {
|
||||||
// PullBack retrieve the last element of the list.
|
// PullBack retrieve the last element of the list.
|
||||||
// The element is not removed.
|
// The element is not removed.
|
||||||
// If the list is empty, an empty element is returned.
|
// If the list is empty, an empty element is returned.
|
||||||
func (s *iPSet) PullBack() string {
|
func (s *iPSet) PullBack() int32 {
|
||||||
if len(s.set) == 0 {
|
if len(s.set) == 0 {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
@ -61,7 +62,7 @@ func (s *iPSet) PullBack() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exists checks if the given element present in the list.
|
// Exists checks if the given element present in the list.
|
||||||
func (s *iPSet) Exists(elem string) bool {
|
func (s *iPSet) Exists(elem int32) bool {
|
||||||
for _, e := range s.set {
|
for _, e := range s.set {
|
||||||
if e == elem {
|
if e == elem {
|
||||||
return true
|
return true
|
||||||
|
|
@ -72,7 +73,7 @@ func (s *iPSet) Exists(elem string) bool {
|
||||||
|
|
||||||
// Remove removes an element from the list.
|
// Remove removes an element from the list.
|
||||||
// If the element is not found, it has no effect.
|
// If the element is not found, it has no effect.
|
||||||
func (s *iPSet) Remove(elem string) {
|
func (s *iPSet) Remove(elem int32) {
|
||||||
for i, e := range s.set {
|
for i, e := range s.set {
|
||||||
if e == elem {
|
if e == elem {
|
||||||
s.set = append(s.set[:i], s.set[i+1:]...)
|
s.set = append(s.set[:i], s.set[i+1:]...)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue