forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Add word removal to the Go/struct/trie (TheAlgorithms#380)
Co-authored-by: xavier <xavier@Fedora33OnDellXPS>
- Loading branch information
Showing
5 changed files
with
372 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// package set implements a Set using a golang map. | ||
// This implies that only the types that are accepted as valid map keys can be used as set elements. | ||
// For instance, do not try to Add a slice, or the program will panic. | ||
// | ||
package set | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func ExampleSet() { | ||
|
||
set := New(1, 2, 3) | ||
fmt.Println(set.Len()) // 3 | ||
set.Add(3) | ||
fmt.Println(set.Len()) // 3 | ||
set.Add(4) | ||
fmt.Println(set.Len()) // 4 | ||
|
||
// output: | ||
// 3 | ||
// 3 | ||
// 4 | ||
} |
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,116 @@ | ||
package trie | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"testing" | ||
) | ||
|
||
// CAUTION : make sure to limit the benchamrks to 3000 iterations, | ||
// or removal will mostly process an empty Trie, giving absurd results. | ||
|
||
// go test -v -bench=. -benchmem -benchtime=3000x | ||
|
||
// === RUN TestTrieInsert | ||
// --- PASS: TestTrieInsert (0.00s) | ||
// === RUN TestTrieRemove | ||
// --- PASS: TestTrieRemove (0.00s) | ||
// === RUN ExampleNode | ||
// --- PASS: ExampleNode (0.00s) | ||
// goos: linux | ||
// goarch: amd64 | ||
// pkg: github.com/TheAlgorithms/Go/structure/trie | ||
// BenchmarkTrie_Insert | ||
// BenchmarkTrie_Insert-8 3000 1559881 ns/op 1370334 B/op 25794 allocs/op | ||
// BenchmarkTrie_Find_non_existant | ||
// BenchmarkTrie_Find_non_existant-8 3000 59.1 ns/op 0 B/op 0 allocs/op | ||
// BenchmarkTrie_Find_existant | ||
// BenchmarkTrie_Find_existant-8 3000 238 ns/op 0 B/op 0 allocs/op | ||
// BenchmarkTrie_Remove_lazy | ||
// BenchmarkTrie_Remove_lazy-8 3000 126 ns/op 0 B/op 0 allocs/op | ||
// BenchmarkTrie_Remove_and_Compact | ||
// BenchmarkTrie_Remove_and_Compact-8 3000 213945 ns/op 0 B/op 0 allocs/op | ||
// PASS | ||
// ok github.com/TheAlgorithms/Go/structure/trie 5.355s | ||
|
||
func BenchmarkTrie_Insert(b *testing.B) { | ||
|
||
// prepare random words for insertion | ||
insert := make([]string, 3000) | ||
for i := 0; i < len(insert); i++ { | ||
insert[i] = fmt.Sprintf("%f", rand.Float64()) | ||
} | ||
|
||
// Reset timer and run benchmark for insertion | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
n := NewNode() | ||
n.Insert(insert...) | ||
} | ||
} | ||
|
||
func BenchmarkTrie_Find_non_existant(b *testing.B) { | ||
|
||
// prepare random words for insertion | ||
insert := make([]string, 3000) | ||
for i := 0; i < len(insert); i++ { | ||
insert[i] = fmt.Sprintf("%f", rand.Float64()) | ||
} | ||
n := NewNode() | ||
n.Insert(insert...) | ||
|
||
// Reset timer and run benchmark for finding non existing | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
n.Find("0.3213213244346546546546565465465") // does not exists | ||
} | ||
} | ||
|
||
func BenchmarkTrie_Find_existant(b *testing.B) { | ||
// prepare and insert random words | ||
insert := make([]string, 3000) | ||
for i := 0; i < len(insert); i++ { | ||
insert[i] = fmt.Sprintf("%f", rand.Float64()) | ||
} | ||
n := NewNode() | ||
n.Insert(insert...) | ||
|
||
// Reset timer and run benchmark for finding existing words | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
n.Find(insert[i%3000]) // always exists ! | ||
} | ||
} | ||
|
||
func BenchmarkTrie_Remove_lazy(b *testing.B) { | ||
// prepare and insert random words | ||
insert := make([]string, 3000) | ||
for i := 0; i < len(insert); i++ { | ||
insert[i] = fmt.Sprintf("%f", rand.Float64()) | ||
} | ||
n := NewNode() | ||
n.Insert(insert...) | ||
|
||
// Reset timer and run benchmark for lazily removing words | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
n.Remove(insert[i%3000]) // exists, at least until removed ... | ||
} | ||
} | ||
|
||
func BenchmarkTrie_Remove_and_Compact(b *testing.B) { | ||
// prepare and insert random words | ||
insert := make([]string, 3000) | ||
for i := 0; i < len(insert); i++ { | ||
insert[i] = fmt.Sprintf("%f", rand.Float64()) | ||
} | ||
n := NewNode() | ||
n.Insert(insert...) | ||
|
||
// Reset timer and run benchmark for removing words and immediately compacting | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
n.Remove(insert[i%3000]) | ||
n.Compact() | ||
} | ||
} |
Oops, something went wrong.