Skip to content

Commit

Permalink
Remove unused array sizes and slices of arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
jonastheis committed Nov 6, 2023
1 parent 9711af4 commit deb6d44
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 390 deletions.
22 changes: 0 additions & 22 deletions serializer/serializable.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/binary"
"encoding/json"
"fmt"
"sort"

"github.com/iotaledger/hive.go/ierrors"
)
Expand Down Expand Up @@ -369,27 +368,6 @@ func (l LexicalOrdered40ByteArrays) Swap(i, j int) {
l[i], l[j] = l[j], l[i]
}

// RemoveDupsAndSortByLexicalOrderArrayOf32Bytes returns a new SliceOfArraysOf32Bytes sorted by lexical order and without duplicates.
func RemoveDupsAndSortByLexicalOrderArrayOf32Bytes(slice SliceOfArraysOf32Bytes) SliceOfArraysOf32Bytes {
seen := make(map[string]struct{})
orderedArray := make(LexicalOrdered32ByteArrays, len(slice))

uniqueElements := 0
for _, v := range slice {
k := string(v[:])
if _, has := seen[k]; has {
continue
}
seen[k] = struct{}{}
orderedArray[uniqueElements] = v
uniqueElements++
}
orderedArray = orderedArray[:uniqueElements]
sort.Sort(orderedArray)

return orderedArray
}

// SortedSerializables are Serializables sorted by their serialized form.
type SortedSerializables Serializables

Expand Down
93 changes: 0 additions & 93 deletions serializer/serializable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package serializer_test
import (
"fmt"
"math/rand"
"sort"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -203,98 +202,6 @@ func TestDeserializeA(t *testing.T) {
assert.Equal(t, seriA[serializer.SmallTypeDenotationByteSize:], objA.Key[:])
}

func TestLexicalOrderedByteSlices(t *testing.T) {
type test struct {
name string
source serializer.LexicalOrderedByteSlices
target serializer.LexicalOrderedByteSlices
}
tests := []test{
{
name: "ok - order by first ele",
source: serializer.LexicalOrderedByteSlices{
{3, 2, 1},
{2, 3, 1},
{1, 2, 3},
},
target: serializer.LexicalOrderedByteSlices{
{1, 2, 3},
{2, 3, 1},
{3, 2, 1},
},
},
{
name: "ok - order by last ele",
source: serializer.LexicalOrderedByteSlices{
{1, 1, 3},
{1, 1, 2},
{1, 1, 1},
},
target: serializer.LexicalOrderedByteSlices{
{1, 1, 1},
{1, 1, 2},
{1, 1, 3},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sort.Sort(tt.source)
assert.Equal(t, tt.target, tt.source)
})
}
}

func TestRemoveDupsAndSortByLexicalOrderArrayOf32Bytes(t *testing.T) {
type test struct {
name string
source serializer.LexicalOrdered32ByteArrays
target serializer.LexicalOrdered32ByteArrays
}
tests := []test{
{
name: "ok - dups removed and order by first ele",
source: serializer.LexicalOrdered32ByteArrays{
{3, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32},
{3, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32},
{2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32},
{2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32},
},
target: serializer.LexicalOrdered32ByteArrays{
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32},
{2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32},
{3, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32},
},
},
{
name: "ok - dups removed and order by last ele",
source: serializer.LexicalOrdered32ByteArrays{
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 34},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 34},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 33},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32},
},
target: serializer.LexicalOrdered32ByteArrays{
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 33},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 34},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.source = serializer.RemoveDupsAndSortByLexicalOrderArrayOf32Bytes(tt.source)
assert.Equal(t, tt.target, tt.source)
})
}
}

func TestSerializationMode_HasMode(t *testing.T) {
type args struct {
mode serializer.DeSerializationMode
Expand Down
Loading

0 comments on commit deb6d44

Please sign in to comment.