-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Devansh Singh <[email protected]>
- Loading branch information
1 parent
84b2f39
commit a504921
Showing
5 changed files
with
68 additions
and
9 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package zset | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
) | ||
|
||
var ErrElementNotExists = errors.New("the element does not exist in set") | ||
|
||
type ZSet struct { | ||
Mutex *sync.RWMutex | ||
Elements *RBTree | ||
} | ||
|
||
func NewZSet() ZSet { | ||
return ZSet{ | ||
Mutex: &sync.RWMutex{}, | ||
Elements: NewRBTree(), | ||
} | ||
} | ||
|
||
func (z *ZSet) Size() int { | ||
z.Mutex.RLock() | ||
defer z.Mutex.RUnlock() | ||
|
||
return z.Elements.Count | ||
} | ||
|
||
func (z *ZSet) Add(element string) { | ||
z.Mutex.Lock() | ||
defer z.Mutex.Unlock() | ||
|
||
z.Elements.insert(element) | ||
} | ||
|
||
func (z *ZSet) Exists(element string) bool { | ||
z.Mutex.RLock() | ||
defer z.Mutex.RUnlock() | ||
|
||
_, ok := z.Elements.search(element) | ||
return ok | ||
} | ||
|
||
func (z *ZSet) Remove(element string) error { | ||
if ok := z.Exists(element); !ok { | ||
return ErrElementNotExists | ||
} | ||
|
||
z.Mutex.Lock() | ||
defer z.Mutex.Unlock() | ||
z.Elements.delete(element) | ||
|
||
return nil | ||
} | ||
|
||
func (z *ZSet) Members() []string { | ||
z.Mutex.RLock() | ||
defer z.Mutex.RUnlock() | ||
|
||
return z.Elements.members() | ||
} |