summaryrefslogtreecommitdiff
path: root/ds
diff options
context:
space:
mode:
authorPaul Buetow <pbuetow@mimecast.com>2020-08-08 13:01:36 +0100
committerPaul Buetow <pbuetow@mimecast.com>2020-08-08 13:01:36 +0100
commit390333bb314f6cb25adc5716ea383112860ed342 (patch)
tree2b87d5741d84b2cd2d7c74eaaa0f522c3a8a221c /ds
parentdeaa4e1c33cd2c1c75f698881918688055abfa51 (diff)
add parallelquick and so on
Diffstat (limited to 'ds')
-rw-r--r--ds/integer.go42
-rw-r--r--ds/integer_test.go18
2 files changed, 41 insertions, 19 deletions
diff --git a/ds/integer.go b/ds/integer.go
index 296e5bb..e8936c8 100644
--- a/ds/integer.go
+++ b/ds/integer.go
@@ -12,12 +12,16 @@ type Integer struct {
func RandomIntegers(length, max int) ArrayList {
a := make(ArrayList, length)
for i := 0; i < length; i++ {
- a[i] = Integer{rand.Intn(max)}
+ if max > 0 {
+ a[i] = Integer{rand.Intn(max)}
+ continue
+ }
+ a[i] = Integer{rand.Int()}
}
return a
}
-func SortedIntegers(length int) ArrayList {
+func AscendingIntegers(length int) ArrayList {
a := make(ArrayList, length)
for i := 0; i < length; i++ {
a[i] = Integer{i}
@@ -25,7 +29,7 @@ func SortedIntegers(length int) ArrayList {
return a
}
-func ReverseSortedIntegers(length int) ArrayList {
+func DescendingIntegers(length int) ArrayList {
a := make(ArrayList, length)
j := length
for i := 0; i < length; i++ {
@@ -64,24 +68,24 @@ func (i Integer) HigherEqual(j Elem) bool {
}
func (i Integer) Compare(j Elem) int {
- jVal := j.Int()
- switch {
- case i.Val < jVal:
- return -1
- case i.Val > jVal:
- return 1
- }
+ jVal := j.Int()
+ switch {
+ case i.Val < jVal:
+ return -1
+ case i.Val > jVal:
+ return 1
+ }
return 0
}
func (i Integer) CompareCB(j Elem, lowerCB, higherCB, equalsCB func()) {
- jVal := j.Int()
- switch {
- case i.Val < jVal:
- lowerCB()
- case i.Val > jVal:
- higherCB()
- default:
- equalsCB()
- }
+ jVal := j.Int()
+ switch {
+ case i.Val < jVal:
+ lowerCB()
+ case i.Val > jVal:
+ higherCB()
+ default:
+ equalsCB()
+ }
}
diff --git a/ds/integer_test.go b/ds/integer_test.go
index 9be55eb..fe9cb90 100644
--- a/ds/integer_test.go
+++ b/ds/integer_test.go
@@ -21,4 +21,22 @@ func TestCompare(t *testing.T) {
if res != 0 {
t.Errorf("%v must be equal to %v, but got %v", i, j, res)
}
+
+ i = Integer{23}
+ j = Integer{23}
+ if !i.HigherEqual(j) {
+ t.Errorf("Unpexpected %v.HigherEqual(%v) == false",
+ i,j)
+ }
+
+ i = Integer{23}
+ j = Integer{42}
+ if i.HigherEqual(j) {
+ t.Errorf("Unpexpected %v.HigherEqual(%v) == true",
+ i,j)
+ }
+ if !j.HigherEqual(i) {
+ t.Errorf("Unpexpected %v.HigherEqual(%v) == false",
+ j,i)
+ }
}