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
b1374b6a
authored
2013-12-12 22:40:29 +0800
by
astaxie
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
move safemap to utils & add many helpful slice function
1 parent
19119e99
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
133 additions
and
2 deletions
safemap.go → utils/safemap.go
safemap_test.go → utils/safemap_test.go
utils/slice.go
safemap.go
→
utils/
safemap.go
View file @
b1374b6
package
beego
package
utils
import
(
"sync"
...
...
safemap_test.go
→
utils/
safemap_test.go
View file @
b1374b6
package
beego
package
utils
import
(
"testing"
...
...
utils/slice.go
View file @
b1374b6
package
utils
import
(
"math/rand"
"time"
)
type
reducetype
func
(
interface
{})
interface
{}
type
filtertype
func
(
interface
{})
bool
func
InSlice
(
v
string
,
sl
[]
string
)
bool
{
for
_
,
vv
:=
range
sl
{
if
vv
==
v
{
...
...
@@ -8,3 +16,126 @@ func InSlice(v string, sl []string) bool {
}
return
false
}
func
InSliceIface
(
v
interface
{},
sl
[]
interface
{})
bool
{
for
_
,
vv
:=
range
sl
{
if
vv
==
v
{
return
true
}
}
return
false
}
func
SliceRandList
(
min
,
max
int
)
[]
int
{
if
max
<
min
{
min
,
max
=
max
,
min
}
length
:=
max
-
min
+
1
t0
:=
time
.
Now
()
rand
.
Seed
(
int64
(
t0
.
Nanosecond
()))
list
:=
rand
.
Perm
(
length
)
for
index
,
_
:=
range
list
{
list
[
index
]
+=
min
}
return
list
}
func
SliceMerge
(
slice1
,
slice2
[]
interface
{})
(
c
[]
interface
{})
{
c
=
append
(
slice1
,
slice2
...
)
return
}
func
SliceReduce
(
slice
[]
interface
{},
a
reducetype
)
(
dslice
[]
interface
{})
{
for
_
,
v
:=
range
slice
{
dslice
=
append
(
dslice
,
a
(
v
))
}
return
}
func
SliceRand
(
a
[]
interface
{})
(
b
interface
{})
{
randnum
:=
rand
.
Intn
(
len
(
a
))
b
=
a
[
randnum
]
return
}
func
SliceSum
(
intslice
[]
int64
)
(
sum
int64
)
{
for
_
,
v
:=
range
intslice
{
sum
+=
v
}
return
}
func
SliceFilter
(
slice
[]
interface
{},
a
filtertype
)
(
ftslice
[]
interface
{})
{
for
_
,
v
:=
range
slice
{
if
a
(
v
)
{
ftslice
=
append
(
ftslice
,
v
)
}
}
return
}
func
SliceDiff
(
slice1
,
slice2
[]
interface
{})
(
diffslice
[]
interface
{})
{
for
_
,
v
:=
range
slice1
{
if
!
InSliceIface
(
v
,
slice2
)
{
diffslice
=
append
(
diffslice
,
v
)
}
}
return
}
func
SliceIntersect
(
slice1
,
slice2
[]
interface
{})
(
diffslice
[]
interface
{})
{
for
_
,
v
:=
range
slice1
{
if
!
InSliceIface
(
v
,
slice2
)
{
diffslice
=
append
(
diffslice
,
v
)
}
}
return
}
func
SliceChunk
(
slice
[]
interface
{},
size
int
)
(
chunkslice
[][]
interface
{})
{
if
size
>=
len
(
slice
)
{
chunkslice
=
append
(
chunkslice
,
slice
)
return
}
end
:=
size
for
i
:=
0
;
i
<=
(
len
(
slice
)
-
size
);
i
+=
size
{
chunkslice
=
append
(
chunkslice
,
slice
[
i
:
end
])
end
+=
size
}
return
}
func
SliceRange
(
start
,
end
,
step
int64
)
(
intslice
[]
int64
)
{
for
i
:=
start
;
i
<=
end
;
i
+=
step
{
intslice
=
append
(
intslice
,
i
)
}
return
}
func
SlicePad
(
slice
[]
interface
{},
size
int
,
val
interface
{})
[]
interface
{}
{
if
size
<=
len
(
slice
)
{
return
slice
}
for
i
:=
0
;
i
<
(
size
-
len
(
slice
));
i
++
{
slice
=
append
(
slice
,
val
)
}
return
slice
}
func
SliceUnique
(
slice
[]
interface
{})
(
uniqueslice
[]
interface
{})
{
for
_
,
v
:=
range
slice
{
if
!
InSliceIface
(
v
,
uniqueslice
)
{
uniqueslice
=
append
(
uniqueslice
,
v
)
}
}
return
}
func
SliceShuffle
(
slice
[]
interface
{})
[]
interface
{}
{
for
i
:=
0
;
i
<
len
(
slice
);
i
++
{
a
:=
rand
.
Intn
(
len
(
slice
))
b
:=
rand
.
Intn
(
len
(
slice
))
slice
[
a
],
slice
[
b
]
=
slice
[
b
],
slice
[
a
]
}
return
slice
}
...
...
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