Last active
June 15, 2020 01:07
-
-
Save pacuna/c4b36503bebd6167a62774c0d1e47492 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (l *list) Add(item []byte) bool { | |
var pred, curr *node | |
key, _ := hashstructure.Hash(item, nil) | |
l.mu.Lock() | |
defer l.mu.Unlock() | |
pred = l.head | |
curr = pred.next | |
for curr.key < key { | |
pred = curr | |
curr = curr.next | |
} | |
if key == curr.key { | |
return false | |
} | |
n := newNode(item) | |
n.next = curr | |
pred.next = n | |
l.size++ | |
return true | |
} | |
func (l *list) Remove(item []byte) bool { | |
var pred, curr *node | |
key, _ := hashstructure.Hash(item, nil) | |
l.mu.Lock() | |
defer l.mu.Unlock() | |
pred = l.head | |
curr = pred.next | |
for curr.key < key { | |
pred = curr | |
curr = curr.next | |
} | |
if key == curr.key { | |
pred.next = curr.next | |
return true | |
} | |
return false | |
} | |
func (l *list) Contains(item []byte) bool { | |
var pred, curr *node | |
key, _ := hashstructure.Hash(item, nil) | |
l.mu.Lock() | |
defer l.mu.Unlock() | |
pred = l.head | |
curr = pred.next | |
for curr.key < key { | |
pred = curr | |
curr = curr.next | |
} | |
return key == curr.key | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment