Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
张磊
/
FileStorageBeego
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
f78ceb82
authored
2013-03-28 15:25:49 +0800
by
astaxie
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
add safemap and test
1 parent
1b715bcb
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
82 additions
and
0 deletions
safemap.go
safemap_test.go
safemap.go
0 → 100644
View file @
f78ceb8
package
beego
import
(
"sync"
)
type
BeeMap
struct
{
lock
*
sync
.
RWMutex
bm
map
[
interface
{}]
interface
{}
}
func
NewBeeMap
()
*
BeeMap
{
return
&
BeeMap
{
lock
:
new
(
sync
.
RWMutex
),
bm
:
make
(
map
[
interface
{}]
interface
{}),
}
}
//Get from maps return the k's value
func
(
m
*
BeeMap
)
Get
(
k
interface
{})
interface
{}
{
m
.
lock
.
RLock
()
defer
m
.
lock
.
RUnlock
()
if
val
,
ok
:=
m
.
bm
[
k
];
ok
{
return
val
}
return
nil
}
// Maps the given key and value. Returns false
// if the key is already in the map and changes nothing.
func
(
m
*
BeeMap
)
Set
(
k
interface
{},
v
interface
{})
bool
{
m
.
lock
.
Lock
()
defer
m
.
lock
.
Unlock
()
if
val
,
ok
:=
m
.
bm
[
k
];
!
ok
{
m
.
bm
[
k
]
=
v
}
else
if
val
!=
v
{
m
.
bm
[
k
]
=
v
}
else
{
return
false
}
return
true
}
// Returns true if k is exist in the map.
func
(
m
*
BeeMap
)
Check
(
k
interface
{})
bool
{
m
.
lock
.
RLock
()
defer
m
.
lock
.
RUnlock
()
if
_
,
ok
:=
m
.
bm
[
k
];
!
ok
{
return
false
}
return
true
}
func
(
m
*
BeeMap
)
Delete
(
k
interface
{})
{
m
.
lock
.
Lock
()
defer
m
.
lock
.
Unlock
()
delete
(
m
.
bm
,
k
)
}
safemap_test.go
0 → 100644
View file @
f78ceb8
package
beego
import
(
"testing"
)
func
Test_beemap
(
t
*
testing
.
T
)
{
bm
:=
NewBeeMap
()
if
!
bm
.
Set
(
"astaxie"
,
1
)
{
t
.
Error
(
"set Error"
)
}
if
!
bm
.
Check
(
"astaxie"
)
{
t
.
Error
(
"check err"
)
}
if
v
:=
bm
.
Get
(
"astaxie"
);
v
.
(
int
)
!=
1
{
t
.
Error
(
"get err"
)
}
bm
.
Delete
(
"astaxie"
)
if
bm
.
Check
(
"astaxie"
)
{
t
.
Error
(
"delete err"
)
}
}
Write
Preview
Styling with
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment