Last active
June 15, 2020 01:11
-
-
Save pacuna/0626e9b956e27737b387ccc0e418682e 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) Remove(item []byte) bool { | |
key, _ := hashstructure.Hash(item, nil) | |
l.head.lock() | |
pred := l.head | |
curr := pred.next | |
curr.lock() | |
for curr.key < key { | |
pred.unlock() | |
pred = curr | |
curr = curr.next | |
curr.lock() | |
} | |
if curr.key == key { | |
pred.next = curr.next | |
curr.unlock() | |
pred.unlock() | |
l.mu.Lock() | |
l.size-- | |
l.mu.Unlock() | |
return true | |
} | |
curr.unlock() | |
pred.unlock() | |
return false | |
} | |
func (l *list) Contains(item []byte) bool { | |
key, _ := hashstructure.Hash(item, nil) | |
l.head.lock() | |
pred := l.head | |
curr := pred.next | |
curr.lock() | |
for curr.key < key { | |
pred.unlock() | |
pred = curr | |
curr = curr.next | |
curr.lock() | |
} | |
if curr.key == key { | |
pred.unlock() | |
curr.unlock() | |
return true | |
} | |
curr.unlock() | |
pred.unlock() | |
return false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment