Skip to content

Commit e7f45f4

Browse files
risboo6909Boris Tatarintsev
authored and
Boris Tatarintsev
committed
Add uint, uint32 and uint64 support to typesafe.go
1 parent d662458 commit e7f45f4

File tree

2 files changed

+370
-0
lines changed

2 files changed

+370
-0
lines changed

typesafe.go

+289
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ func InInt64s(s []int64, v int64) bool {
1919
return ContainsInt64(s, v)
2020
}
2121

22+
// InUInts is an alias of ContainsUInt, returns true if an uint is present in a iteratee.
23+
func InUInts(s []uint, v uint) bool {
24+
return ContainsUInt(s, v)
25+
}
26+
27+
// InUInt32s is an alias of ContainsUInt32, returns true if an uint32 is present in a iteratee.
28+
func InUInt32s(s []uint32, v uint32) bool {
29+
return ContainsUInt32(s, v)
30+
}
31+
32+
// InUInt64s is an alias of ContainsUInt64, returns true if an uint64 is present in a iteratee.
33+
func InUInt64s(s []uint64, v uint64) bool {
34+
return ContainsUInt64(s, v)
35+
}
36+
2237
// InStrings is an alias of ContainsString, returns true if a string is present in a iteratee.
2338
func InStrings(s []string, v string) bool {
2439
return ContainsString(s, v)
@@ -198,6 +213,54 @@ func FilterInt64(s []int64, cb func(s int64) bool) []int64 {
198213
return results
199214
}
200215

216+
// FilterUInt iterates over a collection of uint, returning an array of
217+
// all uint elements predicate returns truthy for.
218+
func FilterUInt(s []uint, cb func(s uint) bool) []uint {
219+
results := []uint{}
220+
221+
for _, i := range s {
222+
result := cb(i)
223+
224+
if result {
225+
results = append(results, i)
226+
}
227+
}
228+
229+
return results
230+
}
231+
232+
// FilterUInt32 iterates over a collection of uint32, returning an array of
233+
// all uint32 elements predicate returns truthy for.
234+
func FilterUInt32(s []uint32, cb func(s uint32) bool) []uint32 {
235+
results := []uint32{}
236+
237+
for _, i := range s {
238+
result := cb(i)
239+
240+
if result {
241+
results = append(results, i)
242+
}
243+
}
244+
245+
return results
246+
}
247+
248+
// FilterUInt64 iterates over a collection of uint64, returning an array of
249+
// all uint64 elements predicate returns truthy for.
250+
func FilterUInt64(s []uint64, cb func(s uint64) bool) []uint64 {
251+
results := []uint64{}
252+
253+
for _, i := range s {
254+
result := cb(i)
255+
256+
if result {
257+
results = append(results, i)
258+
}
259+
}
260+
261+
return results
262+
}
263+
201264
// FilterString iterates over a collection of string, returning an array of
202265
// all string elements predicate returns truthy for.
203266
func FilterString(s []string, cb func(s string) bool) []string {
@@ -244,6 +307,37 @@ func ContainsInt64(s []int64, v int64) bool {
244307
return false
245308
}
246309

310+
// ContainsUInt returns true if an uint is present in a iteratee.
311+
func ContainsUInt(s []uint, v uint) bool {
312+
for _, vv := range s {
313+
if vv == v {
314+
return true
315+
}
316+
}
317+
return false
318+
}
319+
320+
// ContainsUInt32 returns true if an uint32 is present in a iteratee.
321+
func ContainsUInt32(s []uint32, v uint32) bool {
322+
for _, vv := range s {
323+
if vv == v {
324+
return true
325+
}
326+
}
327+
return false
328+
}
329+
330+
// ContainsUInt64 returns true if an uint64 is present in a iteratee.
331+
func ContainsUInt64(s []uint64, v uint64) bool {
332+
for _, vv := range s {
333+
if vv == v {
334+
return true
335+
}
336+
}
337+
return false
338+
}
339+
340+
247341
// ContainsString returns true if a string is present in a iteratee.
248342
func ContainsString(s []string, v string) bool {
249343
for _, vv := range s {
@@ -298,6 +392,30 @@ func SumInt(s []int) (sum int) {
298392
return
299393
}
300394

395+
// SumUInt32 sums a uint32 iteratee and returns the sum of all elements
396+
func SumUInt32(s []uint32) (sum uint32) {
397+
for _, v := range s {
398+
sum += v
399+
}
400+
return
401+
}
402+
403+
// SumUInt64 sums a uint64 iteratee and returns the sum of all elements
404+
func SumUInt64(s []uint64) (sum uint64) {
405+
for _, v := range s {
406+
sum += v
407+
}
408+
return
409+
}
410+
411+
// SumUInt sums a uint iteratee and returns the sum of all elements
412+
func SumUInt(s []uint) (sum uint) {
413+
for _, v := range s {
414+
sum += v
415+
}
416+
return
417+
}
418+
301419
// SumFloat64 sums a float64 iteratee and returns the sum of all elements
302420
func SumFloat64(s []float64) (sum float64) {
303421
for _, v := range s {
@@ -346,6 +464,30 @@ func ReverseInt64(s []int64) []int64 {
346464
return s
347465
}
348466

467+
// ReverseUInt reverses an array of int
468+
func ReverseUInt(s []uint) []uint {
469+
for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
470+
s[i], s[j] = s[j], s[i]
471+
}
472+
return s
473+
}
474+
475+
// ReverseUInt32 reverses an array of uint32
476+
func ReverseUInt32(s []uint32) []uint32 {
477+
for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
478+
s[i], s[j] = s[j], s[i]
479+
}
480+
return s
481+
}
482+
483+
// ReverseUInt64 reverses an array of uint64
484+
func ReverseUInt64(s []uint64) []uint64 {
485+
for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
486+
s[i], s[j] = s[j], s[i]
487+
}
488+
return s
489+
}
490+
349491
// ReverseFloat64 reverses an array of float64
350492
func ReverseFloat64(s []float64) []float64 {
351493
for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
@@ -398,6 +540,24 @@ func IndexOfInt64(a []int64, x int64) int {
398540
return indexOf(len(a), func(i int) bool { return a[i] == x })
399541
}
400542

543+
// IndexOfUInt gets the index at which the first occurrence of an uint value is found in array or return -1
544+
// if the value cannot be found
545+
func IndexOfUInt(a []uint, x uint) int {
546+
return indexOf(len(a), func(i int) bool { return a[i] == x })
547+
}
548+
549+
// IndexOfUInt32 gets the index at which the first occurrence of an uint32 value is found in array or return -1
550+
// if the value cannot be found
551+
func IndexOfUInt32(a []uint32, x uint32) int {
552+
return indexOf(len(a), func(i int) bool { return a[i] == x })
553+
}
554+
555+
// IndexOfUInt64 gets the index at which the first occurrence of an uint64 value is found in array or return -1
556+
// if the value cannot be found
557+
func IndexOfUInt64(a []uint64, x uint64) int {
558+
return indexOf(len(a), func(i int) bool { return a[i] == x })
559+
}
560+
401561
// IndexOfFloat64 gets the index at which the first occurrence of an float64 value is found in array or return -1
402562
// if the value cannot be found
403563
func IndexOfFloat64(a []float64, x float64) int {
@@ -437,6 +597,24 @@ func LastIndexOfInt64(a []int64, x int64) int {
437597
return lastIndexOf(len(a), func(i int) bool { return a[i] == x })
438598
}
439599

600+
// LastIndexOfUInt gets the index at which the first occurrence of an uint value is found in array or return -1
601+
// if the value cannot be found
602+
func LastIndexOfUInt(a []uint, x uint) int {
603+
return lastIndexOf(len(a), func(i int) bool { return a[i] == x })
604+
}
605+
606+
// LastIndexOfUInt32 gets the index at which the first occurrence of an uint32 value is found in array or return -1
607+
// if the value cannot be found
608+
func LastIndexOfUInt32(a []uint32, x uint32) int {
609+
return lastIndexOf(len(a), func(i int) bool { return a[i] == x })
610+
}
611+
612+
// LastIndexOfUInt64 gets the index at which the first occurrence of an uint64 value is found in array or return -1
613+
// if the value cannot be found
614+
func LastIndexOfUInt64(a []uint64, x uint64) int {
615+
return lastIndexOf(len(a), func(i int) bool { return a[i] == x })
616+
}
617+
440618
// LastIndexOfFloat64 gets the index at which the first occurrence of an float64 value is found in array or return -1
441619
// if the value cannot be found
442620
func LastIndexOfFloat64(a []float64, x float64) int {
@@ -521,6 +699,72 @@ func UniqInt(a []int) []int {
521699
return a[0:j]
522700
}
523701

702+
// UniqUInt32 creates an array of uint32 with unique values.
703+
func UniqUInt32(a []uint32) []uint32 {
704+
length := len(a)
705+
706+
seen := make(map[uint32]struct{}, length)
707+
j := 0
708+
709+
for i := 0; i < length; i++ {
710+
v := a[i]
711+
712+
if _, ok := seen[v]; ok {
713+
continue
714+
}
715+
716+
seen[v] = struct{}{}
717+
a[j] = v
718+
j++
719+
}
720+
721+
return a[0:j]
722+
}
723+
724+
// UniqUInt64 creates an array of uint64 with unique values.
725+
func UniqUInt64(a []uint64) []uint64 {
726+
length := len(a)
727+
728+
seen := make(map[uint64]struct{}, length)
729+
j := 0
730+
731+
for i := 0; i < length; i++ {
732+
v := a[i]
733+
734+
if _, ok := seen[v]; ok {
735+
continue
736+
}
737+
738+
seen[v] = struct{}{}
739+
a[j] = v
740+
j++
741+
}
742+
743+
return a[0:j]
744+
}
745+
746+
// UniqUInt creates an array of uint with unique values.
747+
func UniqUInt(a []uint) []uint {
748+
length := len(a)
749+
750+
seen := make(map[uint]struct{}, length)
751+
j := 0
752+
753+
for i := 0; i < length; i++ {
754+
v := a[i]
755+
756+
if _, ok := seen[v]; ok {
757+
continue
758+
}
759+
760+
seen[v] = struct{}{}
761+
a[j] = v
762+
j++
763+
}
764+
765+
return a[0:j]
766+
}
767+
524768
// UniqString creates an array of string with unique values.
525769
func UniqString(a []string) []string {
526770
length := len(a)
@@ -617,6 +861,36 @@ func ShuffleInt64(a []int64) []int64 {
617861
return a
618862
}
619863

864+
// ShuffleUInt creates an array of int shuffled values using Fisher–Yates algorithm
865+
func ShuffleUInt(a []uint) []uint {
866+
for i := range a {
867+
j := rand.Intn(i + 1)
868+
a[i], a[j] = a[j], a[i]
869+
}
870+
871+
return a
872+
}
873+
874+
// ShuffleUInt32 creates an array of uint32 shuffled values using Fisher–Yates algorithm
875+
func ShuffleUInt32(a []uint32) []uint32 {
876+
for i := range a {
877+
j := rand.Intn(i + 1)
878+
a[i], a[j] = a[j], a[i]
879+
}
880+
881+
return a
882+
}
883+
884+
// ShuffleUInt64 creates an array of uint64 shuffled values using Fisher–Yates algorithm
885+
func ShuffleUInt64(a []uint64) []uint64 {
886+
for i := range a {
887+
j := rand.Intn(i + 1)
888+
a[i], a[j] = a[j], a[i]
889+
}
890+
891+
return a
892+
}
893+
620894
// ShuffleString creates an array of string shuffled values using Fisher–Yates algorithm
621895
func ShuffleString(a []string) []string {
622896
for i := range a {
@@ -667,6 +941,21 @@ func DropInt64(s []int64, n int) []int64 {
667941
return s[n:]
668942
}
669943

944+
// DropUInt creates a slice with `n` ints dropped from the beginning.
945+
func DropUInt(s []uint, n uint) []uint {
946+
return s[n:]
947+
}
948+
949+
// DropUInt32 creates a slice with `n` int32s dropped from the beginning.
950+
func DropUInt32(s []uint32, n int) []uint32 {
951+
return s[n:]
952+
}
953+
954+
// DropUInt64 creates a slice with `n` int64s dropped from the beginning.
955+
func DropUInt64(s []uint64, n int) []uint64 {
956+
return s[n:]
957+
}
958+
670959
// DropFloat32 creates a slice with `n` float32s dropped from the beginning.
671960
func DropFloat32(s []float32, n int) []float32 {
672961
return s[n:]

0 commit comments

Comments
 (0)