Skip to content

Commit

Permalink
feat: adds SortedSet implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Karpovych committed Jan 20, 2021
1 parent 1e62373 commit 0a9a067
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 7 deletions.
6 changes: 3 additions & 3 deletions set.go2
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ func NewSet[T comparable]() *Set[T] {
return &Set[T]{table: map[T]bool{}}
}

// Size returns the number of elements in the container.
// Len returns the number of elements in the container.
// Complexity - O(1).
func (s *Set[T]) Size() int {
func (s *Set[T]) Len() int {
return len(s.table)
}

// IsEmpty checks if there are elements in Set.
// Complexity - O(1).
// Returns true if the set is empty, false otherwise.
func (s *Set[T]) IsEmpty() bool {
return s.Size() == 0
return s.Len() == 0
}

// NotEmpty checks if there are no elements in Set.
Expand Down
7 changes: 3 additions & 4 deletions set_test.go2
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ const addsCount = 3000
func TestNewSet(t *testing.T) {
s := NewSet[int]()

checkSize(s, 0, t)
checkIsEmpty(s, true, t)
checkSet[int](s, 0, true, t)
}

func TestAdd(t *testing.T) {
Expand Down Expand Up @@ -96,8 +95,8 @@ func checkSet[T comparable](s *Set[T], size int, isEmpty bool, t *testing.T) {
}

func checkSize[T comparable](s *Set[T], size int, t *testing.T) {
if size != s.Size() {
t.Errorf("Expected set size %d, got %d", size, s.Size())
if size != s.Len() {
t.Errorf("Expected set size %d, got %d", size, s.Len())
}
}

Expand Down
59 changes: 59 additions & 0 deletions sortedset.go2
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)
}
117 changes: 117 additions & 0 deletions sortedset_test.go2
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())
}
}

0 comments on commit 0a9a067

Please sign in to comment.