Skip to content

Commit

Permalink
Add NumIdGenerator[T Integer] type.
Browse files Browse the repository at this point in the history
Add IntegerRange[T Integer] type.

Signed-off-by: aliwoto <[email protected]>
  • Loading branch information
aliwoto committed May 16, 2022
1 parent 107b6a0 commit ff09989
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 56 deletions.
7 changes: 7 additions & 0 deletions ssg/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"time"
"unicode"

"github.com/AnimeKaizoku/ssg/ssg/rangeValues"
"github.com/AnimeKaizoku/ssg/ssg/shellUtils"
"github.com/AnimeKaizoku/ssg/ssg/strongParser"
)
Expand Down Expand Up @@ -429,6 +430,12 @@ func NewSafeEMap[TKey comparable, TValue any]() *SafeEMap[TKey, TValue] {
}
}

func NewNumIdGenerator[T rangeValues.Integer]() *NumIdGenerator[T] {
return &NumIdGenerator[T]{
mut: &sync.Mutex{},
}
}

func IsAllLower(value string) bool {
return strings.ToLower(value) == value
}
Expand Down
28 changes: 28 additions & 0 deletions ssg/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -1493,3 +1493,31 @@ func (e *EndpointError) Error() string {
}

//---------------------------------------------------------

// Next method returns the next id and will move the current cursor to
// the next id and will return that.
// current integer won't be available after this method call.
func (n *NumIdGenerator[T]) Next() T {
n.mut.Lock()
defer n.mut.Unlock()
n.current++
return n.current
}

// Set method will set the current value to the given value.
// please do notice that with calling n.Next() method, you will get
// the next id and not the current value.
func (n *NumIdGenerator[T]) Set(current T) {
n.mut.Lock()
defer n.mut.Unlock()
n.current = current
}

// Reset method will set the current value to 0.
func (n *NumIdGenerator[T]) Reset() {
n.mut.Lock()
defer n.mut.Unlock()
n.current = 0
}

//---------------------------------------------------------
46 changes: 4 additions & 42 deletions ssg/rangeValues/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (

//---------------------------------------------------------

func (r *RangeInt32) IsInRange(value int32) bool {
func (r *IntegerRange[T]) IsInRange(value T) bool {
return r.Min <= value && r.Max >= value
}

func (r *RangeInt32) IsValueInRange(value *RangeInt32) bool {
func (r *IntegerRange[T]) IsValueInRange(value *IntegerRange[T]) bool {
if value == nil {
return false
}
Expand All @@ -20,46 +20,8 @@ func (r *RangeInt32) IsValueInRange(value *RangeInt32) bool {
(r.Min <= value.Min && r.Max >= value.Max)
}

func (r *RangeInt32) GetRandom() int32 {
return rand.Int31n(r.Max-r.Min) + r.Min
}

//---------------------------------------------------------

func (r *RangeInt) IsInRange(value int) bool {
return r.Min <= value && r.Max >= value
}

func (r *RangeInt) IsValueInRange(value *RangeInt) bool {
if value == nil {
return false
}

return value == r ||
(r.Min <= value.Min && r.Max >= value.Max)
}

func (r *RangeInt) GetRandom() int {
return rand.Intn(r.Max-r.Min) + r.Min
}

//---------------------------------------------------------

func (r *RangeInt64) IsInRange(value int64) bool {
return r.Min <= value && r.Max >= value
}

func (r *RangeInt64) IsValueInRange(value *RangeInt64) bool {
if value == nil {
return false
}

return value == r ||
(r.Min <= value.Min && r.Max >= value.Max)
}

func (r *RangeInt64) GetRandom() int64 {
return rand.Int63n(r.Max-r.Min) + r.Min
func (r *IntegerRange[T]) GetRandom() T {
return T(rand.Int63n(int64(r.Max-r.Min)) + int64(r.Min))
}

//---------------------------------------------------------
Expand Down
16 changes: 5 additions & 11 deletions ssg/rangeValues/types.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package rangeValues

type RangeInt32 struct {
Min int32
Max int32
type Integer interface {
int64 | int | int32 | int16 | int8 | uint64 | uint | uint32 | uint16 | uint8
}

type RangeInt struct {
Min int
Max int
}

type RangeInt64 struct {
Min int64
Max int64
type IntegerRange[T Integer] struct {
Min T
Max T
}

type RangeFloat64 struct {
Expand Down
11 changes: 8 additions & 3 deletions ssg/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ type SafeMap[TKey comparable, TValue any] struct {
_default TValue
}

type NumIdGenerator[T rangeValues.Integer] struct {
current T
mut *sync.Mutex
}

// SafeEMap is a safe map of type TIndex to pointers of type TValue.
// this map is completely thread safe and is using internal lock when
// getting and setting variables.
Expand Down Expand Up @@ -103,9 +108,9 @@ type EndpointError struct {
Date string `json:"date"`
}

type RangeInt = rangeValues.RangeInt
type RangeInt32 = rangeValues.RangeInt32
type RangeInt64 = rangeValues.RangeInt64
type RangeInt = rangeValues.IntegerRange[int]
type RangeInt32 = rangeValues.IntegerRange[int32]
type RangeInt64 = rangeValues.IntegerRange[int64]
type RangeFloat64 = rangeValues.RangeFloat64

type ExecuteCommandResult = shellUtils.ExecuteCommandResult
Expand Down

0 comments on commit ff09989

Please sign in to comment.