Skip to content

Commit

Permalink
update use case
Browse files Browse the repository at this point in the history
  • Loading branch information
rabbitprincess committed Jan 10, 2023
1 parent ad08525 commit 4a31e89
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 18 deletions.
16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
// IntelliSense를 사용하여 가능한 특성에 대해 알아보세요.
// 기존 특성에 대한 설명을 보려면 가리킵니다.
// 자세한 내용을 보려면 https://go.microsoft.com/fwlink/?linkid=830387을(를) 방문하세요.
"version": "0.2.0",
"configurations": [

{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}"
}
]
}
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,37 @@
- Supports up to 96 integers and 32 decimal places
- implement ericlagergren/decimal ( github.com/ericlagergren/decimal )


## Quick Start

```go

package main

import (
"fmt"

"github.com/gokch/snum_sort/snum"
"github.com/gokch/snum_sort/sort"
)

func main() {
sn1 := snum.New("123456789.987654321")
fmt.Println("snum :", sn1)

st1 := sort.New(sn1)
fmt.Println("sort :", st1)

enc, _ := st1.MarshalJSON()
fmt.Println("json :", enc)

st1New := sort.New(0)
st1New.UnmarshalJSON(enc)
fmt.Println("new :", st1New)
}

```

# max length
- positive : 65 byte (header 1 bt + body 64 bt)
- negative : 66 byte (header 1 bt + body 64 bt + 0xFF 1 bt)
Expand Down
24 changes: 23 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
package snum_sort
package main

import (
"fmt"

"github.com/gokch/snum_sort/snum"
"github.com/gokch/snum_sort/sort"
)

func main() {
sn1 := snum.New("123456789.987654321")
fmt.Println("snum :", sn1)

st1 := sort.New(sn1)
fmt.Println("sort :", st1)

enc, _ := st1.MarshalJSON()
fmt.Println("json :", string(enc))

st1New := sort.New(0)
st1New.UnmarshalJSON(enc)
fmt.Println("new :", st1New)
}
2 changes: 1 addition & 1 deletion snum/get_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (t *Snum) TrimDigit(lenInteger, lenDecimal int) error {
// 후처리 - 정수
if lenIntegerNow > lenInteger {
errInteger = fmt.Errorf("Integer limit exceeded | input : %d | limit : %d", lenIntegerNow, lenInteger) // 에러 처리
pt_snum := NewSnum(0)
pt_snum := New(0)

pt_snum.decimal.SetUint64(10)
pt_snum.Pow(int64(lenInteger))
Expand Down
4 changes: 2 additions & 2 deletions snum/snum.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type SnumConst interface {
*Snum | int | int32 | int64 | uint | uint32 | uint64 | string
}

func NewSnum[T SnumConst](num T) *Snum {
func New[T SnumConst](num T) *Snum {
snum := &Snum{}
snum.Init()

Expand Down Expand Up @@ -66,7 +66,7 @@ func (t *Snum) Init() {
}

func (t *Snum) Copy() *Snum {
ret := NewSnum(0)
ret := New(0)
ret.decimal.Copy(t.decimal)
return ret
}
Expand Down
10 changes: 5 additions & 5 deletions snum/snum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ import (

func Test_snum(t *testing.T) {
{
snum := NewSnum(0)
snum := New(0)
fmt.Println(snum)
}
{
var num int = -1
snum := NewSnum(num)
snum := New(num)
fmt.Println(snum.String())
}
{
str := "1.2345"
snum := NewSnum(str)
snum := New(str)
fmt.Println(snum.String())
}
{
snum := NewSnum(1)
snum2 := NewSnum(snum)
snum := New(1)
snum2 := New(snum)
fmt.Println(snum2.String())
}
}
6 changes: 3 additions & 3 deletions snum/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ import (
)

func (t *Snum) IsZero() bool {
if t.Cmp(NewSnum(0)) != 0 {
if t.Cmp(New(0)) != 0 {
return false
}
return true
}

func (t *Snum) IsZeroUnder() bool {
if t.Cmp(NewSnum(0)) < 0 {
if t.Cmp(New(0)) < 0 {
return false
}
return true
}

func (t *Snum) IsZeroOver() bool {
if t.Cmp(NewSnum(0)) > 0 {
if t.Cmp(New(0)) > 0 {
return false
}
return true
Expand Down
4 changes: 2 additions & 2 deletions sort/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"github.com/gokch/snum_sort/snum"
)

func NewSnumSort[T snum.SnumConst](num T) *SnumSort {
func New[T snum.SnumConst](num T) *SnumSort {
return &SnumSort{
Snum: *snum.NewSnum(num),
Snum: *snum.New(num),
}
}

Expand Down
8 changes: 4 additions & 4 deletions sort/sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
// string -> bigint -> binary -> bigint -> string
func TestSort_encode_decode(t *testing.T) {
fn := func(input string) {
numSort := NewSnumSort(input)
numSort := New(input)

enc := numSort.Encode()
err := numSort.Decode(enc)
Expand Down Expand Up @@ -186,7 +186,7 @@ func Test_sort(t *testing.T) {
sorts := make([]*Input, 0, 100)

input := func(snum string) {
sorted := NewSnumSort(snum)
sorted := New(snum)
bt := sorted.Encode()

data := &Input{snum: snum, encode: bt}
Expand Down Expand Up @@ -277,13 +277,13 @@ func TestSort_header(t *testing.T) {

func TestSort_json(t *testing.T) {
fn_test := func(sn string) {
snumSort := NewSnumSort(snum.NewSnum(sn))
snumSort := New(snum.New(sn))

enc, err := json.MarshalIndent(&snumSort, "", "\t")
fmt.Println(sn, string(enc))
require.NoError(t, err)

snumSortNew := NewSnumSort(snum.NewSnum(0))
snumSortNew := New(snum.New(0))
err = json.Unmarshal(enc, &snumSortNew)
require.NoError(t, err)

Expand Down

0 comments on commit 4a31e89

Please sign in to comment.