Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
G
gostnops
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wei.xuan
gostnops
Commits
b75faeb2
Commit
b75faeb2
authored
Nov 28, 2019
by
zhanghuiren
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
support java data structrue
parent
660039bc
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
96 additions
and
4 deletions
+96
-4
integer.go
math/big/integer.go
+50
-4
integer_test.go
math/big/integer_test.go
+46
-0
No files found.
math/big/integer.go
View file @
b75faeb2
...
...
@@ -18,6 +18,7 @@
package
gxbig
import
(
"encoding/binary"
"fmt"
"math/big"
)
...
...
@@ -27,7 +28,13 @@ type Integer struct {
bigInt
big
.
Int
// for hessian
Value
string
Signum
int32
Mag
[]
int
FirstNonzeroIntNum
int
LowestSetBit
int
BitLength
int
BitCount
int
}
func
(
Integer
)
JavaClassName
()
string
{
...
...
@@ -45,10 +52,45 @@ func (i *Integer) FromString(s string) error {
return
nil
}
// FromBytes set data from a 10-bases number bytes
func
(
i
*
Integer
)
FromBytes
(
bytes
[]
byte
)
error
{
// FromMag set data from a array of big-endian unsigned uint32
// @see https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html#BigInteger-int-byte:A-
func
(
i
*
Integer
)
FromSignAndMag
(
signum
int32
,
mag
[]
int
)
{
if
signum
==
0
&&
len
(
mag
)
==
0
{
return
}
i
.
Signum
=
signum
i
.
Mag
=
mag
bytes
:=
make
([]
byte
,
4
*
len
(
i
.
Mag
))
for
j
:=
0
;
j
<
len
(
i
.
Mag
);
j
++
{
binary
.
BigEndian
.
PutUint32
(
bytes
[
j
*
4
:
(
j
+
1
)
*
4
],
uint32
(
i
.
Mag
[
j
]))
}
i
.
bigInt
=
*
i
.
bigInt
.
SetBytes
(
bytes
)
return
nil
if
i
.
Signum
==
-
1
{
i
.
bigInt
.
Neg
(
&
i
.
bigInt
)
}
}
func
(
i
*
Integer
)
GetSignAndMag
()
(
signum
int32
,
mag
[]
int
)
{
signum
=
int32
(
i
.
bigInt
.
Sign
())
bytes
:=
i
.
bigInt
.
Bytes
()
outOf4
:=
len
(
bytes
)
%
4
if
outOf4
>
0
{
bytes
=
append
(
make
([]
byte
,
4
-
outOf4
),
bytes
...
)
}
size
:=
len
(
bytes
)
/
4
mag
=
make
([]
int
,
size
)
for
i
:=
0
;
i
<
size
;
i
++
{
mag
[
i
]
=
int
(
binary
.
BigEndian
.
Uint32
(
bytes
[
i
*
4
:
(
i
+
1
)
*
4
]))
}
return
}
// GetBigInt getter
...
...
@@ -64,3 +106,7 @@ func (i *Integer) SetBigInt(bigInt big.Int) {
func
(
i
*
Integer
)
String
()
string
{
return
i
.
bigInt
.
String
()
}
func
(
i
*
Integer
)
Bytes
()
[]
byte
{
return
i
.
bigInt
.
Bytes
()
}
math/big/integer_test.go
View file @
b75faeb2
...
...
@@ -18,6 +18,7 @@
package
gxbig
import
(
"reflect"
"testing"
)
...
...
@@ -52,3 +53,48 @@ func TestInteger(t *testing.T) {
})
}
}
func
TestInteger_FromSignAndMag
(
t
*
testing
.
T
)
{
type
args
struct
{
signum
int32
mag
[]
int
}
tests
:=
[]
struct
{
name
string
digit
string
args
args
}{
{
`0`
,
`0`
,
args
{
0
,
[]
int
{}}},
{
`1`
,
`1`
,
args
{
1
,
[]
int
{
1
}}},
{
`2147483647`
,
`2147483647`
,
args
{
1
,
[]
int
{
2147483647
}}},
{
`4294967295`
,
`4294967295`
,
args
{
1
,
[]
int
{
4294967295
}}},
{
`4294967296`
,
`4294967296`
,
args
{
1
,
[]
int
{
1
,
0
}}},
{
`4294967298`
,
`4294967298`
,
args
{
1
,
[]
int
{
1
,
2
}}},
{
`1x2x3`
,
`18446744082299486211`
,
args
{
1
,
[]
int
{
1
,
2
,
3
}}},
{
`-1`
,
`-1`
,
args
{
-
1
,
[]
int
{
1
}}},
{
`-4294967296`
,
`-4294967296`
,
args
{
-
1
,
[]
int
{
1
,
0
}}},
{
`-4294967298`
,
`-4294967298`
,
args
{
-
1
,
[]
int
{
1
,
2
}}},
{
`-1x2x3`
,
`-18446744082299486211`
,
args
{
-
1
,
[]
int
{
1
,
2
,
3
}}},
}
for
_
,
tt
:=
range
tests
{
t
.
Run
(
tt
.
name
,
func
(
t
*
testing
.
T
)
{
i
:=
&
Integer
{}
i
.
FromSignAndMag
(
tt
.
args
.
signum
,
tt
.
args
.
mag
)
if
i
.
String
()
!=
tt
.
digit
{
t
.
Errorf
(
"digit %s got = %s"
,
tt
.
digit
,
i
.
String
())
}
s
:=
&
Integer
{}
err
:=
s
.
FromString
(
tt
.
digit
)
if
err
!=
nil
{
t
.
Error
(
"FromString error = "
,
err
)
}
sign
,
mag
:=
s
.
GetSignAndMag
()
if
!
(
sign
==
tt
.
args
.
signum
&&
reflect
.
DeepEqual
(
mag
,
tt
.
args
.
mag
))
{
t
.
Error
(
"want "
,
tt
.
args
.
signum
,
tt
.
args
.
mag
,
"got"
,
sign
,
mag
)
}
})
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
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 comment