Skip to content

Commit

Permalink
add comments and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
disksing committed Nov 27, 2019
1 parent bf475bf commit c3967b9
Show file tree
Hide file tree
Showing 7 changed files with 252 additions and 13 deletions.
31 changes: 22 additions & 9 deletions algorithm.go
Expand Up @@ -102,7 +102,7 @@ func FindEnd(first, last, sFirst, sLast ForwardReader) ForwardReader {
return FindEndBy(first, last, sFirst, sLast, _eq)
}

// FindEnd searches for the last occurrence of the sequence [sFirst, sLast) in
// FindEndBy searches for the last occurrence of the sequence [sFirst, sLast) in
// the range [first, last). If [sFirst, sLast) is empty or such sequence is
// found, last is returned. Elements are compared using the given binary
// predicate pred.
Expand Down Expand Up @@ -281,30 +281,36 @@ func FillN(first ForwardWriter, count int, v Any) {
}
}

// Transform
// Transform applies the given function to the range [first, last) and stores
// the result in another range, beginning at dFirst.
func Transform(first, last ForwardReader, dFirst ForwardWriter, op UnaryOperation) ForwardWriter {
for ; _ne(first, last); dFirst, first = NextWriter(dFirst), NextReader(first) {
dFirst.Write(op(first.Read()))
}
return dFirst
}

// TransformBinary
// TransformBinary applies the given function to the two ranges [first, last),
// [first2, first2+last-first) and stores the result in another range, beginning
// at dFirst.
func TransformBinary(first1, last1, first2 ForwardReader, dFirst ForwardWriter, op BinaryOperation) ForwardWriter {
for ; _ne(first1, last1); dFirst, first1, first2 = NextWriter(dFirst), NextReader(first1), NextReader(first2) {
dFirst.Write(op(first1.Read(), first2.Read()))
}
return dFirst
}

// Generate
// Generate assigns each element in range [first, last) a value generated by the
// given function object g.
func Generate(first, last ForwardWriter, g Generator) {
for ; _ne(first, last); first = NextWriter(first) {
first.Write(g())
}
}

// GenerateN
// GenerateN assigns values, generated by given function object g, to the first
// count elements in the range beginning at first, if count>0. Does nothing
// otherwise.
func GenerateN(first ForwardWriter, count int, g Generator) ForwardWriter {
for ; count > 0; count-- {
first.Write(g())
Expand All @@ -313,12 +319,15 @@ func GenerateN(first ForwardWriter, count int, g Generator) ForwardWriter {
return first
}

// Remove
// Remove removes all elements equal to v from the range [first, last) and
// returns a past-the-end iterator for the new end of the range.
func Remove(first, last ForwardReadWriter, v Any) ForwardReadWriter {
return RemoveIf(first, last, _eq1(v))
}

// RemoveIf
// RemoveIf removes all elements which predicate function returns true from the
// range [first, last) and returns a past-the-end iterator for the new end of
// the range.
func RemoveIf(first, last ForwardReadWriter, pred UnaryPredicate) ForwardReadWriter {
first = FindIf(first, last, pred).(ForwardReadWriter)
if _ne(first, last) {
Expand All @@ -332,12 +341,16 @@ func RemoveIf(first, last ForwardReadWriter, pred UnaryPredicate) ForwardReadWri
return first
}

// RemoveCopy
// RemoveCopy copies elements from the range [first, last), to another range
// beginning at dFirst, omitting the elements equal to v. Source and destination
// ranges cannot overlap.
func RemoveCopy(first, last ForwardReader, dFirst ForwardWriter, v Any) ForwardWriter {
return RemoveCopyIf(first, last, dFirst, _eq1(v))
}

// RemoveCopyIf
// RemoveCopyIf copies elements from the range [first, last), to another range
// beginning at dFirst, omitting the elements which predicate function returns
// true. Source and destination ranges cannot overlap.
func RemoveCopyIf(first, last ForwardReader, dFirst ForwardWriter, pred UnaryPredicate) ForwardWriter {
for ; _ne(first, last); first = NextReader(first) {
if !pred(first.Read()) {
Expand Down
76 changes: 76 additions & 0 deletions algorithm_test.go
Expand Up @@ -284,6 +284,82 @@ func TestFillN(t *testing.T) {
}
}

func TestTransform(t *testing.T) {
a := randIntSlice()
var b []int
for _, x := range a {
b = append(b, x*2)
}
Transform(begin(a), end(a), begin(a), func(x Any) Any { return x.(int) * 2 })
sliceEqual(assert.New(t), a, b)
}

func TestTransformBinary(t *testing.T) {
a, b := randIntSlice(), randIntSlice()
if len(a) > len(b) {
a, b = b, a
}
c := make([]int, len(a))
TransformBinary(begin(a), end(a), begin(b), begin(c), func(x, y Any) Any { return x.(int) * y.(int) })
for i := range a {
a[i] *= b[i]
}
sliceEqual(assert.New(t), a, c)
}

func TestGenerate(t *testing.T) {
assert := assert.New(t)
var i int
g := func() Any { i++; return i }
a := randIntSlice()
Generate(begin(a), end(a), g)
for i := range a {
assert.Equal(i+1, a[i])
}
}

func TestGenerateN(t *testing.T) {
assert := assert.New(t)
var i int
g := func() Any { i++; return i }
a := randIntSlice()
b := append(a[:0:0], a...)
n := rand.Intn(len(a) + 1)
GenerateN(begin(a), n, g)
for i := range a {
if i < n {
assert.Equal(i+1, a[i])
} else {
assert.Equal(a[i], b[i])
}
}
}

func TestRemove(t *testing.T) {
assert := assert.New(t)
a := randIntSlice()
b := append(a[:0:0], a...)
c := append(a[:0:0], a...)
var d, e []int
f := func(x Any) bool { return x.(int)%2 == 0 }

count1 := Count(begin(a), end(a), 1)
countf := CountIf(begin(a), end(a), f)
SliceErase(&a, Remove(begin(a), end(a), 1))
SliceErase(&b, RemoveIf(begin(b), end(b), f))
RemoveCopy(begin(c), end(c), SliceBackInserter(&d), 1)
RemoveCopyIf(begin(c), end(c), SliceBackInserter(&e), f)

assert.Equal(Count(begin(a), end(a), 1), 0)
assert.True(NoneOf(begin(b), end(b), f))
assert.Equal(Count(begin(d), end(d), 1), 0)
assert.True(NoneOf(begin(e), end(e), f))
assert.Equal(len(a), len(c)-count1)
assert.Equal(len(b), len(c)-countf)
assert.Equal(len(d), len(c)-count1)
assert.Equal(len(e), len(c)-countf)
}

func TestMinmax(t *testing.T) {
assert := assert.New(t)
a, b := randInt(), randInt()
Expand Down
1 change: 1 addition & 0 deletions go.sum
Expand Up @@ -5,6 +5,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

0 comments on commit c3967b9

Please sign in to comment.