From 16478480203416206232be7e2d521c0fe1ea3c1e Mon Sep 17 00:00:00 2001 From: Mohammadjafari80 Date: Thu, 4 Feb 2021 19:21:56 +0330 Subject: [PATCH 1/4] implement Resize function --- data-structures/hash-tables/ht.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/data-structures/hash-tables/ht.go b/data-structures/hash-tables/ht.go index 45141cc..e3e5587 100644 --- a/data-structures/hash-tables/ht.go +++ b/data-structures/hash-tables/ht.go @@ -4,6 +4,7 @@ package ht import ( + list "algorithms-3/data-structures/linked-list" "errors" "github.com/arnauddri/algorithms/data-structures/linked-list" "math" @@ -115,3 +116,17 @@ func hashCode(s string) int { } return int(math.Abs(float64(hash))) } + +// Resizes table to desired capacity. ( if Possible ) +func (ht *HashTable) Resize(newCap int) error { + size := ht.Size + if ht.Capacity == newCap { + return errors.New("current capacity is as same the input number") + } + if newCap >= size { + ht.Capacity = newCap + } else { + return errors.New("there is not enough capacity to hold items. please enter a larger number") + } + return nil +} From 08f37f9fe0336dfebfc53b020f62f1710f4ce6ca Mon Sep 17 00:00:00 2001 From: pourya-momtaz Date: Thu, 4 Feb 2021 20:52:19 +0330 Subject: [PATCH 2/4] implemented resize function test --- data-structures/hash-tables/ht.go | 3 +-- data-structures/hash-tables/ht_test.go | 29 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/data-structures/hash-tables/ht.go b/data-structures/hash-tables/ht.go index e3e5587..6ad1c02 100644 --- a/data-structures/hash-tables/ht.go +++ b/data-structures/hash-tables/ht.go @@ -4,9 +4,8 @@ package ht import ( - list "algorithms-3/data-structures/linked-list" + list "algorithms2/data-structures/linked-list" "errors" - "github.com/arnauddri/algorithms/data-structures/linked-list" "math" ) diff --git a/data-structures/hash-tables/ht_test.go b/data-structures/hash-tables/ht_test.go index c4c59ec..6e121a2 100644 --- a/data-structures/hash-tables/ht_test.go +++ b/data-structures/hash-tables/ht_test.go @@ -55,3 +55,32 @@ func TestHash(t *testing.T) { t.Error() } } + +func TestHashTable_Resize(t *testing.T) { + ht := New(1000) + ht.Put("foo", "bar") + ht.Put("fiz", "buzz") + ht.Put("bruce", "wayne") + ht.Put("peter", "parker") + ht.Put("clark", "kent") + + actual := ht.Resize(10) + + if actual != nil { + t.Errorf("resul = %v, want nil", actual) + } + + actual = ht.Resize(10) + expected := "current capacity is as same the input number" + + if actual.Error() != expected { + t.Errorf("resul = %v, want %v", actual, expected) + } + + actual = ht.Resize(4) + expected = "there is not enough capacity to hold items. please enter a larger number" + + if actual.Error() != expected { + t.Errorf("resul = %v, want %v", actual, expected) + } +} From ba58e52da4ec2379cd81c252a1caae351d1eaf54 Mon Sep 17 00:00:00 2001 From: Mohammadjafari80 Date: Thu, 4 Feb 2021 21:04:39 +0330 Subject: [PATCH 3/4] make paths relative --- data-structures/hash-tables/ht.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data-structures/hash-tables/ht.go b/data-structures/hash-tables/ht.go index 6ad1c02..71004d2 100644 --- a/data-structures/hash-tables/ht.go +++ b/data-structures/hash-tables/ht.go @@ -4,7 +4,7 @@ package ht import ( - list "algorithms2/data-structures/linked-list" + list "../linked-list" "errors" "math" ) From 6589458ed219d090ee268dbe176d5a8993756f17 Mon Sep 17 00:00:00 2001 From: Mohammadjafari80 Date: Thu, 4 Feb 2021 21:07:01 +0330 Subject: [PATCH 4/4] make paths relative to gitHub url --- data-structures/hash-tables/ht.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data-structures/hash-tables/ht.go b/data-structures/hash-tables/ht.go index 71004d2..e94282e 100644 --- a/data-structures/hash-tables/ht.go +++ b/data-structures/hash-tables/ht.go @@ -4,8 +4,8 @@ package ht import ( - list "../linked-list" "errors" + list "github.com/arnauddri/algorithms/data-structures/linked-list" "math" )