Skip to content

Commit

Permalink
std: remove unused utils
Browse files Browse the repository at this point in the history
  • Loading branch information
pulsejet committed Jan 14, 2025
1 parent d1c6b28 commit d65e75b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 25 deletions.
6 changes: 2 additions & 4 deletions std/encoding/name_pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"hash"
"io"
"strings"

"github.com/named-data/ndnd/std/utils"
)

type Name []Component
Expand Down Expand Up @@ -204,7 +202,7 @@ func (n Name) Append(rest ...Component) Name {
}

func (n Name) Compare(rhs Name) int {
for i := 0; i < utils.Min(len(n), len(rhs)); i++ {
for i := 0; i < min(len(n), len(rhs)); i++ {
if ret := n[i].Compare(rhs[i]); ret != 0 {
return ret
}
Expand All @@ -220,7 +218,7 @@ func (n Name) Compare(rhs Name) int {
}

func (n NamePattern) Compare(rhs NamePattern) int {
for i := 0; i < utils.Min(len(n), len(rhs)); i++ {
for i := 0; i < min(len(n), len(rhs)); i++ {
if ret := n[i].Compare(rhs[i]); ret != 0 {
return ret
}
Expand Down
2 changes: 1 addition & 1 deletion std/schema/rdr/rdr.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (n *SegmentedNode) Provide(mNode schema.MatchedNode, content enc.Wire, need
pktContent := enc.Wire{}
remSize := n.SegmentSize
for remSize > 0 && wireIdx < len(content) && bufferIdx < len(content[wireIdx]) {
curSize := int(utils.Min(uint64(len(content[wireIdx])-bufferIdx), remSize))
curSize := int(min(uint64(len(content[wireIdx])-bufferIdx), remSize))
pktContent = append(pktContent, content[wireIdx][bufferIdx:bufferIdx+curSize])
bufferIdx += curSize
remSize -= uint64(curSize)
Expand Down
4 changes: 2 additions & 2 deletions std/schema/svs/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func (n *SvsNode) aggregate(remoteSv *spec_svs.StateVector) {
SeqNo: cur.SeqNo,
})
} else {
n.aggSv.Entries[li].SeqNo = utils.Max(n.aggSv.Entries[li].SeqNo, cur.SeqNo)
n.aggSv.Entries[li].SeqNo = max(n.aggSv.Entries[li].SeqNo, cur.SeqNo)
}
}
}
Expand Down Expand Up @@ -312,7 +312,7 @@ func (n *SvsNode) onAttach(event *schema.Event) any {
n.state = SyncSteady
n.missChan = make(chan MissingData, n.ChannelSize)
// The first sync Interest should be sent out ASAP
n.cancelSyncTimer = n.timer.Schedule(utils.Min(n.getSyncIntv(), 100*time.Millisecond), n.onSyncTimer)
n.cancelSyncTimer = n.timer.Schedule(min(n.getSyncIntv(), 100*time.Millisecond), n.onSyncTimer)

n.stopChan = make(chan struct{}, 1)
if len(n.OnMissingData.Val()) > 0 {
Expand Down
22 changes: 4 additions & 18 deletions std/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,14 @@ func ConvIntPtr[A, B constraints.Integer](a *A) *B {
}
}

func Min[T constraints.Ordered](a, b T) T {
if a < b {
return a
}
return b
}

func Max[T constraints.Ordered](a, b T) T {
if a > b {
return a
}
return b
}

func MakeTimestamp(t time.Time) uint64 {
return uint64(t.UnixNano() / int64(time.Millisecond))
}

func ConvertNonce(nonce []byte) *uint64 {
ret := uint64(0)
for _, v := range nonce {
ret = ret*256 + uint64(v)
x := uint64(0)
for _, b := range nonce {
x = (x << 8) | uint64(b)
}
return &ret
return &x
}
33 changes: 33 additions & 0 deletions std/utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package utils_test

import (
"testing"
"time"

"github.com/named-data/ndnd/std/utils"
"github.com/stretchr/testify/require"
)

func TestIdPtr(t *testing.T) {
utils.SetTestingT(t)

p := utils.IdPtr(uint64(42))
require.Equal(t, uint64(42), *p)
}

func TestMakeTimestamp(t *testing.T) {
utils.SetTestingT(t)

date := time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC)
require.Equal(t, uint64(1609459200000), utils.MakeTimestamp(date))
}

func TestConvertNonce(t *testing.T) {
utils.SetTestingT(t)

nonce := []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}
require.Equal(t, uint64(0x010203040506), *utils.ConvertNonce(nonce))

nonce = []byte{0x42, 0x1C, 0xE1, 0x4B, 0x99, 0xFA, 0xA3}
require.Equal(t, uint64(0x421ce14b99faa3), *utils.ConvertNonce(nonce))
}

0 comments on commit d65e75b

Please sign in to comment.