-
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.
- Loading branch information
Roman Karpovych
committed
Jan 20, 2021
1 parent
1e62373
commit 0a9a067
Showing
4 changed files
with
182 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package gtl | ||
|
||
// SortedSet is ordered set based on red-black tree | ||
// It can contain comparable elements only | ||
type SortedSet[T any] struct { | ||
t *Tree[T] | ||
} | ||
|
||
// NewSortedSet Constructs new SortedSet with given comparator | ||
// which will be used for elements ordering. | ||
func NewSortedSet[T any](comparator func(a, b T) int) *SortedSet[T] { | ||
return &SortedSet[T]{t: NewTree[T](comparator)} | ||
} | ||
|
||
// Len returns the number of elements in the container. | ||
// Complexity - O(1). | ||
func (s *SortedSet[T]) Len() int { | ||
return s.t.Len() | ||
} | ||
|
||
// IsEmpty checks if there are elements in the Set. | ||
// Complexity - O(1). | ||
// Returns true if the set is empty, false otherwise. | ||
func (s *SortedSet[T]) IsEmpty() bool { | ||
return s.Len() == 0 | ||
} | ||
|
||
// NotEmpty checks if there are no elements in the SortedSet. | ||
// Complexity - O(1). | ||
// Returns true if there are elements in the set, false otherwise. | ||
func (s *SortedSet[T]) NotEmpty() bool { | ||
return !s.IsEmpty() | ||
} | ||
|
||
// Add inserts the element into the SortedSet. | ||
// Has no effect if the element already exist. | ||
// Complexity - O(1). | ||
func (s *SortedSet[T]) Add(element T) { | ||
if _, exist := s.t.Search(element); exist { | ||
return | ||
} | ||
|
||
s.t.Insert(element) | ||
} | ||
|
||
// Contains checks if SortedSet contains given element. | ||
// Complexity - O(1). | ||
// returns true if SortedSet includes the element, false otherwise. | ||
func (s *SortedSet[T]) Contains(element T) bool { | ||
_, contains := s.t.Search(element) | ||
return contains | ||
} | ||
|
||
// Delete deletes the element from set if it contains an element | ||
// does nothing otherwise. | ||
// Complexity - O(1). | ||
func (s *SortedSet[T]) Delete(element T) { | ||
s.t.Delete(element) | ||
} |
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,117 @@ | ||
// Copyright 2020. The GTL Authors. All rights reserved. | ||
// https://github.com/modern-dev/gtl | ||
// Use of this source code is governed by the MIT | ||
// license that can be found in the LICENSE file. | ||
|
||
package gtl | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
const insertsCount = 3000 | ||
|
||
func sortedSetIntComparator(a, b int) int { | ||
return a - b | ||
} | ||
|
||
func TestNewSortedSet(t *testing.T) { | ||
s := NewSortedSet[int](sortedSetIntComparator) | ||
|
||
checkSortedSet[int](s, 0, true, t) | ||
} | ||
|
||
func TestSortedSetAdd(t *testing.T) { | ||
s := NewSortedSet[int](sortedSetIntComparator) | ||
|
||
checkSortedSet[int](s, 0, true, t) | ||
for i := 0; i < insertsCount; i++ { | ||
s.Add(i) | ||
checkSortedSet[int](s, i+1, false, t) | ||
} | ||
} | ||
|
||
func TestSortedSetIsEmpty(t *testing.T) { | ||
s := NewSortedSet[int](sortedSetIntComparator) | ||
|
||
checkSortedSetIsEmpty[int](s, true, t) | ||
|
||
s.Add(1) | ||
s.Add(42) | ||
|
||
checkSortedSetIsEmpty[int](s, false, t) | ||
} | ||
|
||
func TestSortedSetNotEmpty(t *testing.T) { | ||
s := NewSortedSet[int](sortedSetIntComparator) | ||
|
||
checkSortedSetNotEmpty[int](s, false, t) | ||
|
||
s.Add(0) | ||
s.Add(35) | ||
|
||
checkSortedSetNotEmpty[int](s, true, t) | ||
} | ||
|
||
func TestSortedSetContains(t *testing.T) { | ||
s := NewSortedSet[int](sortedSetIntComparator) | ||
|
||
checkSortedSet[int](s, 0, true, t) | ||
|
||
for i := 0; i < insertsCount/2; i = i + 2 { | ||
s.Add(i) | ||
|
||
if s.Contains(i) != true { | ||
t.Errorf("Expected set to contain %d", i) | ||
} | ||
|
||
if s.Contains(i+1) == true { | ||
t.Errorf("Expected set to not contain %d", i+1) | ||
} | ||
} | ||
} | ||
|
||
func TestSortedSetDelete(t *testing.T) { | ||
s := NewSortedSet[int](sortedSetIntComparator) | ||
|
||
for i := 0; i < insertsCount; i++ { | ||
s.Add(i) | ||
} | ||
|
||
checkSortedSet[int](s, insertsCount, false, t) | ||
|
||
for i := 0; i < insertsCount; i++ { | ||
checkSortedSet[int](s, insertsCount-i, false, t) | ||
s.Delete(i) | ||
} | ||
|
||
checkSortedSet[int](s, 0, true, t) | ||
|
||
for i := 0; i < insertsCount; i++ { | ||
checkSortedSet[int](s, 0, true, t) | ||
s.Delete(i) | ||
} | ||
} | ||
|
||
func checkSortedSet[T comparable](s *SortedSet[T], size int, isEmpty bool, t *testing.T) { | ||
checkSortedSetSize(s, size, t) | ||
checkSortedSetIsEmpty(s, isEmpty, t) | ||
} | ||
|
||
func checkSortedSetSize[T comparable](s *SortedSet[T], size int, t *testing.T) { | ||
if size != s.Len() { | ||
t.Errorf("Expected set size %d, got %d", size, s.Len()) | ||
} | ||
} | ||
|
||
func checkSortedSetIsEmpty[T comparable](s *SortedSet[T], isEmpty bool, t *testing.T) { | ||
if isEmpty != s.IsEmpty() { | ||
t.Errorf("Expected IsEmpty to be %v, got %v", isEmpty, s.IsEmpty()) | ||
} | ||
} | ||
|
||
func checkSortedSetNotEmpty[T comparable](s *SortedSet[T], notEmpty bool, t *testing.T) { | ||
if notEmpty != s.NotEmpty() { | ||
t.Errorf("Expected NotEmpty to be %v, got %v", notEmpty, s.NotEmpty()) | ||
} | ||
} |