-
Notifications
You must be signed in to change notification settings - Fork 12
Adiciona método Contains e marca InArray como deprecado #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,55 @@ | ||
| package set | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
| ) | ||
|
|
||
| func BenchmarkLinkedHashSet_Contains_vs_InArray(b *testing.B) { | ||
| total := len(giantGenericSlice) | ||
| step := total / 5 | ||
| sizes := []int{step, 2 * step, 3 * step, 4 * step, total} | ||
|
|
||
| for _, n := range sizes { | ||
| b.Run(fmt.Sprintf("N=%d", n), func(b *testing.B) { | ||
| set := NewLinkedHashSet[string]() | ||
| set.Add(giantGenericSlice[:n]...) | ||
|
|
||
| foundTarget := giantGenericSlice[n/2] | ||
| notFoundTarget := "___not_present___" | ||
|
|
||
| b.Run("Found/Contains", func(b *testing.B) { | ||
| b.ReportAllocs() | ||
| var sink bool | ||
| for i := 0; i < b.N; i++ { | ||
| sink = set.Contains(foundTarget) | ||
| } | ||
| _ = sink | ||
| }) | ||
| b.Run("Found/InArray", func(b *testing.B) { | ||
| b.ReportAllocs() | ||
| var sink bool | ||
| for i := 0; i < b.N; i++ { | ||
| sink = set.InArray(foundTarget) | ||
| } | ||
| _ = sink | ||
| }) | ||
| b.Run("NotFound/Contains", func(b *testing.B) { | ||
| b.ReportAllocs() | ||
| var sink bool | ||
| for i := 0; i < b.N; i++ { | ||
| sink = set.Contains(notFoundTarget) | ||
| } | ||
| _ = sink | ||
| }) | ||
| b.Run("NotFound/InArray", func(b *testing.B) { | ||
| b.ReportAllocs() | ||
| var sink bool | ||
| for i := 0; i < b.N; i++ { | ||
| sink = set.InArray(notFoundTarget) | ||
| } | ||
| _ = sink | ||
| }) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Olá! A mudança na assinatura do método
Geté uma boa melhoria. No entanto, identifiquei um problema crítico de corretude e performance na implementação dolinkedHashMapque invalida a premissa de complexidade O(1) para oGete, consequentemente, para o novo métodoContains.Tratamento de Colisão em
Put: O métodoPut(não presente neste diff, mas parte do contexto) não trata colisões de hash. Ele simplesmente retorna se um hash já existe (if _, ok := l.table[hash]; ok { return }). Isso significa que se duas chaves diferentes produzirem o mesmo hash, a segunda chave e seu valor serão descartados, o que é um bug grave.Lógica de Busca em
Get: O laçofor(linhas 71-76) itera sobre a lista de ordem de inserção (tmp.after), não sobre uma cadeia de colisões. Isso é incorreto para resolver colisões e degrada a performance de O(1) para O(n) no pior caso, pois pode percorrer grande parte da lista.Para que o
Get(e oContains) tenha a performance esperada, olinkedHashMapprecisa ser corrigido para tratar colisões de hash adequadamente, por exemplo, usando encadeamento separado (separate chaining) para cada bucket no mapa.Dado que este problema afeta diretamente o objetivo principal do seu PR, recomendo fortemente que a implementação do
linkedHashMapseja corrigida.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tá certo, a intenção dessa lib é implementar uma estrutura de Conjunto, sem itens repetidos