Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
Goshimmer_without_tipselection
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
COLLET Ismael
Goshimmer_without_tipselection
Commits
0e840e1d
Commit
0e840e1d
authored
5 years ago
by
Hans Moog
Browse files
Options
Downloads
Patches
Plain Diff
Feat: started experimenting with a weakmap implementation (tests)
parent
091efb85
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
packages/datastructure/weakmap_test.go
+92
-0
92 additions, 0 deletions
packages/datastructure/weakmap_test.go
with
92 additions
and
0 deletions
packages/datastructure/weakmap_test.go
0 → 100644
+
92
−
0
View file @
0e840e1d
package
datastructure
import
(
"fmt"
"testing"
)
import
"runtime"
import
"unsafe"
import
"errors"
import
"time"
func
TestWeakmap
(
t
*
testing
.
T
)
{
generateA
(
6
)
fmt
.
Println
(
""
)
debugWeakMap
(
6
)
fmt
.
Println
(
""
)
runtime
.
GC
()
fmt
.
Println
(
"GC runned."
)
time
.
Sleep
(
1
*
time
.
Second
)
debugWeakMap
(
6
)
}
func
generateA
(
n
int
)
{
for
i
:=
0
;
i
<
n
;
i
++
{
a
:=
NewA
()
WMap
.
Add
(
a
)
fmt
.
Println
(
"Added to WM A with id ="
,
a
.
Id
)
}
}
func
debugWeakMap
(
n
int
)
{
for
i
:=
0
;
i
<
n
;
i
++
{
if
WMap
.
Has
(
i
)
{
fmt
.
Println
(
"Has id ="
,
i
)
}
else
{
fmt
.
Println
(
"Hasn't id ="
,
i
)
}
}
}
var
aId
int
func
NewA
()
*
A
{
aId
++
return
&
A
{
aId
}
}
var
WMap
=
&
WeakMap
{
weakMap
:
make
(
map
[
int
]
uintptr
)}
type
WeakMap
struct
{
weakMap
map
[
int
]
uintptr
}
type
A
struct
{
Id
int
}
func
(
w
*
WeakMap
)
Add
(
a
*
A
)
{
runtime
.
SetFinalizer
(
a
,
finalizer
)
w
.
weakMap
[
a
.
Id
]
=
uintptr
(
unsafe
.
Pointer
(
a
))
}
func
(
w
*
WeakMap
)
Get
(
id
int
)
(
*
A
,
error
)
{
if
!
w
.
Has
(
id
)
{
return
nil
,
errors
.
New
(
""
)
}
a
:=
(
*
A
)(
unsafe
.
Pointer
(
w
.
weakMap
[
id
]))
return
a
,
nil
}
func
(
w
*
WeakMap
)
Has
(
id
int
)
bool
{
_
,
ok
:=
w
.
weakMap
[
id
]
return
ok
}
func
(
w
*
WeakMap
)
Remove
(
a
*
A
)
{
delete
(
w
.
weakMap
,
a
.
Id
)
}
func
finalizer
(
a
*
A
)
{
fmt
.
Println
(
a
.
Id
)
WMap
.
Remove
(
a
)
}
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