good morning!!!!
Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
B
bor
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
open
bor
Commits
5cdfee51
Commit
5cdfee51
authored
May 26, 2014
by
Jeffrey Wilcke
Browse files
Options
Downloads
Patches
Plain Diff
New Trie iterator
parent
4c7bd75c
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
ethutil/encoding.go
+15
-0
15 additions, 0 deletions
ethutil/encoding.go
ethutil/trie.go
+46
-0
46 additions, 0 deletions
ethutil/trie.go
ethutil/trie_test.go
+10
-12
10 additions, 12 deletions
ethutil/trie_test.go
with
71 additions
and
12 deletions
ethutil/encoding.go
+
15
−
0
View file @
5cdfee51
...
@@ -59,3 +59,18 @@ func CompactHexDecode(str string) []int {
...
@@ -59,3 +59,18 @@ func CompactHexDecode(str string) []int {
return
hexSlice
return
hexSlice
}
}
func
DecodeCompact
(
key
[]
int
)
string
{
base
:=
"0123456789abcdef"
var
str
string
for
_
,
v
:=
range
key
{
if
v
<
16
{
str
+=
string
(
base
[
v
])
}
}
res
,
_
:=
hex
.
DecodeString
(
str
)
return
string
(
res
)
}
This diff is collapsed.
Click to expand it.
ethutil/trie.go
+
46
−
0
View file @
5cdfee51
...
@@ -442,6 +442,8 @@ type TrieIterator struct {
...
@@ -442,6 +442,8 @@ type TrieIterator struct {
shas
[][]
byte
shas
[][]
byte
values
[]
string
values
[]
string
lastNode
[]
byte
}
}
func
(
t
*
Trie
)
NewIterator
()
*
TrieIterator
{
func
(
t
*
Trie
)
NewIterator
()
*
TrieIterator
{
...
@@ -513,3 +515,47 @@ func (it *TrieIterator) Key() string {
...
@@ -513,3 +515,47 @@ func (it *TrieIterator) Key() string {
func
(
it
*
TrieIterator
)
Value
()
string
{
func
(
it
*
TrieIterator
)
Value
()
string
{
return
""
return
""
}
}
type
EachCallback
func
(
key
string
,
node
*
Value
)
func
(
it
*
TrieIterator
)
Each
(
cb
EachCallback
)
{
it
.
fetchNode
(
nil
,
NewValue
(
it
.
trie
.
Root
)
.
Bytes
(),
cb
)
}
func
(
it
*
TrieIterator
)
fetchNode
(
key
[]
int
,
node
[]
byte
,
cb
EachCallback
)
{
it
.
iterateNode
(
key
,
it
.
trie
.
cache
.
Get
(
node
),
cb
)
}
func
(
it
*
TrieIterator
)
iterateNode
(
key
[]
int
,
currentNode
*
Value
,
cb
EachCallback
)
{
if
currentNode
.
Len
()
==
2
{
k
:=
CompactDecode
(
currentNode
.
Get
(
0
)
.
Str
())
if
currentNode
.
Get
(
1
)
.
Str
()
==
""
{
it
.
iterateNode
(
key
,
currentNode
.
Get
(
1
),
cb
)
}
else
{
pk
:=
append
(
key
,
k
...
)
if
k
[
len
(
k
)
-
1
]
==
16
{
cb
(
DecodeCompact
(
pk
),
currentNode
.
Get
(
1
))
}
else
{
it
.
fetchNode
(
pk
,
currentNode
.
Get
(
1
)
.
Bytes
(),
cb
)
}
}
}
else
{
for
i
:=
0
;
i
<
currentNode
.
Len
();
i
++
{
pk
:=
append
(
key
,
i
)
if
i
==
16
&&
currentNode
.
Get
(
i
)
.
Len
()
!=
0
{
cb
(
DecodeCompact
(
pk
),
currentNode
.
Get
(
i
))
}
else
{
if
currentNode
.
Get
(
i
)
.
Str
()
==
""
{
it
.
iterateNode
(
pk
,
currentNode
.
Get
(
i
),
cb
)
}
else
{
val
:=
currentNode
.
Get
(
i
)
.
Str
()
if
val
!=
""
{
it
.
fetchNode
(
pk
,
[]
byte
(
val
),
cb
)
}
}
}
}
}
}
This diff is collapsed.
Click to expand it.
ethutil/trie_test.go
+
10
−
12
View file @
5cdfee51
...
@@ -154,7 +154,7 @@ func TestTrieDeleteWithValue(t *testing.T) {
...
@@ -154,7 +154,7 @@ func TestTrieDeleteWithValue(t *testing.T) {
}
}
func
TestTrie
Iterator
(
t
*
testing
.
T
)
{
func
TestTrie
Purge
(
t
*
testing
.
T
)
{
_
,
trie
:=
New
()
_
,
trie
:=
New
()
trie
.
Update
(
"c"
,
LONG_WORD
)
trie
.
Update
(
"c"
,
LONG_WORD
)
trie
.
Update
(
"ca"
,
LONG_WORD
)
trie
.
Update
(
"ca"
,
LONG_WORD
)
...
@@ -171,16 +171,14 @@ func TestTrieIterator(t *testing.T) {
...
@@ -171,16 +171,14 @@ func TestTrieIterator(t *testing.T) {
}
}
}
}
func
Test
Hashes
(
t
*
testing
.
T
)
{
func
Test
TrieIt
(
t
*
testing
.
T
)
{
_
,
trie
:=
New
()
_
,
trie
:=
New
()
trie
.
Update
(
"cat"
,
"dog"
)
trie
.
Update
(
"c"
,
LONG_WORD
)
trie
.
Update
(
"ca"
,
"dude"
)
trie
.
Update
(
"ca"
,
LONG_WORD
)
trie
.
Update
(
"doge"
,
"1234567890abcdefghijklmnopqrstuvwxxzABCEFGHIJKLMNOPQRSTUVWXYZ"
)
trie
.
Update
(
"cat"
,
LONG_WORD
)
trie
.
Update
(
"dog"
,
"test"
)
trie
.
Update
(
"test"
,
"1234567890abcdefghijklmnopqrstuvwxxzABCEFGHIJKLMNOPQRSTUVWXYZ"
)
it
:=
trie
.
NewIterator
()
fmt
.
Printf
(
"%x
\n
"
,
trie
.
Root
)
it
.
Each
(
func
(
key
string
,
node
*
Value
)
{
trie
.
Delete
(
"dog"
)
fmt
.
Println
(
key
,
":"
,
node
.
Str
())
fmt
.
Printf
(
"%x
\n
"
,
trie
.
Root
)
})
trie
.
Delete
(
"test"
)
fmt
.
Printf
(
"%x
\n
"
,
trie
.
Root
)
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment