Skip to content

Commit d058770

Browse files
authored
#13 - implemented ValuesRev() methods (#28)
1 parent aa87a8a commit d058770

File tree

11 files changed

+234
-2
lines changed

11 files changed

+234
-2
lines changed

definitions.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ type Ordered[V any] interface {
115115
SearchRev(predicate Predicate[V]) (val V, found bool)
116116
Tail() (tail V, ok bool)
117117
TailOrDefault(defaultValue V) (tail V)
118-
// ValuesOrdered() iter.Seq2[int, V] // TODO
119-
// ValuesRev() iter.Seq[V] // TODO
118+
ValuesRev() iter.Seq[V]
120119
}
121120

122121
// Indexed interface indicates that given collection can be accessed by index.

map.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,16 @@ func (c *comfyMap[K, V]) Values() iter.Seq[Pair[K, V]] {
322322
}
323323
}
324324

325+
func (c *comfyMap[K, V]) ValuesRev() iter.Seq[Pair[K, V]] {
326+
return func(yield func(Pair[K, V]) bool) {
327+
for i := len(c.s) - 1; i >= 0; i-- {
328+
if !yield(c.s[i]) {
329+
break
330+
}
331+
}
332+
}
333+
}
334+
325335
// Private functions:
326336

327337
//nolint:unused

map_cases_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3353,6 +3353,91 @@ func testMapValuesBreak(t *testing.T, builder baseMapCollIntBuilder) {
33533353
}
33543354
}
33553355

3356+
func getMapValuesRevCases(builder baseMapCollIntBuilder) []baseMapTestCase {
3357+
return []baseMapTestCase{
3358+
{
3359+
name: "ValuesRev() on empty collection",
3360+
coll: builder.Empty(),
3361+
want1: []Pair[int, int](nil),
3362+
},
3363+
{
3364+
name: "ValuesRev() on one-item collection",
3365+
coll: builder.One(),
3366+
want1: []Pair[int, int]{NewPair(1, 111)},
3367+
},
3368+
{
3369+
name: "ValuesRev() on three-item collection",
3370+
coll: builder.Three(),
3371+
want1: []Pair[int, int]{NewPair(3, 333), NewPair(2, 222), NewPair(1, 111)},
3372+
},
3373+
}
3374+
}
3375+
3376+
func testMapValuesRev(t *testing.T, builder baseMapCollIntBuilder) {
3377+
cases := getMapValuesRevCases(builder)
3378+
for _, tt := range cases {
3379+
t.Run(tt.name, func(t *testing.T) {
3380+
got := slices.Collect(tt.coll.ValuesRev())
3381+
if !reflect.DeepEqual(got, tt.want1) {
3382+
t.Errorf("ValuesRev() = %v, want1 = %v", got, tt.want1)
3383+
}
3384+
})
3385+
}
3386+
}
3387+
3388+
func getMapValuesRevBreakCases(builder baseMapCollIntBuilder) []*baseMapTestCase {
3389+
return []*baseMapTestCase{
3390+
{
3391+
name: "ValuesRev() on three-item collection, break immediately",
3392+
coll: builder.Three(),
3393+
args: baseMapIntArgs{
3394+
predicate: func(_ int, p Pair[int, int]) bool {
3395+
return false
3396+
},
3397+
},
3398+
want1: []Pair[int, int](nil),
3399+
},
3400+
{
3401+
name: "ValuesRev() on three-item collection, break at middle",
3402+
coll: builder.Three(),
3403+
args: baseMapIntArgs{
3404+
predicate: func(_ int, p Pair[int, int]) bool {
3405+
return p.Key() > 2
3406+
},
3407+
},
3408+
want1: []Pair[int, int]{NewPair(3, 333)},
3409+
},
3410+
{
3411+
name: "ValuesRev() on three-item collection, break after middle",
3412+
coll: builder.Three(),
3413+
args: baseMapIntArgs{
3414+
predicate: func(_ int, p Pair[int, int]) bool {
3415+
return p.Key() >= 2
3416+
},
3417+
},
3418+
want1: []Pair[int, int]{NewPair(3, 333), NewPair(2, 222)},
3419+
},
3420+
}
3421+
}
3422+
3423+
func testMapValuesRevBreak(t *testing.T, builder baseMapCollIntBuilder) {
3424+
cases := getMapValuesRevBreakCases(builder)
3425+
for _, tt := range cases {
3426+
t.Run(tt.name, func(t *testing.T) {
3427+
got := []Pair[int, int](nil)
3428+
for v := range tt.coll.ValuesRev() {
3429+
if !tt.args.predicate(-1, v) {
3430+
break
3431+
}
3432+
got = append(got, v)
3433+
}
3434+
if !reflect.DeepEqual(got, tt.want1) {
3435+
t.Errorf("ValuesRev() = %v, want1 = %v", got, tt.want1)
3436+
}
3437+
})
3438+
}
3439+
}
3440+
33563441
func getMapCopyCases(builder baseMapCollIntBuilder) []baseMapTestCase {
33573442
return []baseMapTestCase{
33583443
{

map_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,11 @@ func Test_comfyMap_Values(t *testing.T) {
328328
testMapValuesRef(t, &comfyMapIntBuilder[mapInternal[int, int]]{})
329329
}
330330

331+
func Test_comfyMap_ValuesRev(t *testing.T) {
332+
testMapValuesRev(t, &comfyMapIntBuilder[mapInternal[int, int]]{})
333+
testMapValuesRevBreak(t, &comfyMapIntBuilder[mapInternal[int, int]]{})
334+
}
335+
331336
func Test_comfyMap_copy(t *testing.T) {
332337
testMapCopy(t, &comfyMapIntBuilder[mapInternal[int, int]]{})
333338
testMapCopyDontPreserveRef(t, &comfyMapIntBuilder[mapInternal[int, int]]{})

mapcmp.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,16 @@ func (c *comfyCmpMap[K, V]) Values() iter.Seq[Pair[K, V]] {
405405
}
406406
}
407407

408+
func (c *comfyCmpMap[K, V]) ValuesRev() iter.Seq[Pair[K, V]] {
409+
return func(yield func(Pair[K, V]) bool) {
410+
for i := len(c.s) - 1; i >= 0; i-- {
411+
if !yield(c.s[i]) {
412+
break
413+
}
414+
}
415+
}
416+
}
417+
408418
// Private:
409419

410420
func (c *comfyCmpMap[K, V]) copy() baseInternal[Pair[K, V]] {

mapcmp_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,11 @@ func Test_comfyCmpMap_Values(t *testing.T) {
392392
testMapValuesRef(t, &comfyCmpMapIntBuilder[mapInternal[int, int]]{})
393393
}
394394

395+
func Test_comfyCmpMap_ValuesRev(t *testing.T) {
396+
testMapValuesRev(t, &comfyCmpMapIntBuilder[mapInternal[int, int]]{})
397+
testMapValuesRevBreak(t, &comfyCmpMapIntBuilder[mapInternal[int, int]]{})
398+
}
399+
395400
func Test_comfyCmpMap_copy(t *testing.T) {
396401
testMapCopy(t, &comfyCmpMapIntBuilder[mapInternal[int, int]]{})
397402
testMapCopyDontPreserveRef(t, &comfyCmpMapIntBuilder[mapInternal[int, int]]{})

ordered_cases_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package coll
22

33
import (
44
"reflect"
5+
"slices"
56
"testing"
67
)
78

@@ -765,3 +766,88 @@ func testTailOrDefault(t *testing.T, builder orderedCollIntBuilder) {
765766
})
766767
}
767768
}
769+
770+
func getValuesRevCases(builder orderedCollIntBuilder) []orderedTestCase {
771+
return []orderedTestCase{
772+
{
773+
name: "ValuesRev() on empty collection",
774+
coll: builder.Empty(),
775+
want1: []int(nil),
776+
},
777+
{
778+
name: "ValuesRev() on one-item collection",
779+
coll: builder.One(),
780+
want1: []int{111},
781+
},
782+
{
783+
name: "ValuesRev() on three-item collection",
784+
coll: builder.Three(),
785+
want1: []int{333, 222, 111},
786+
},
787+
}
788+
}
789+
790+
func testValuesRev(t *testing.T, builder orderedCollIntBuilder) {
791+
cases := getValuesRevCases(builder)
792+
for _, tt := range cases {
793+
t.Run(tt.name, func(t *testing.T) {
794+
got := slices.Collect(tt.coll.ValuesRev())
795+
if !reflect.DeepEqual(got, tt.want1) {
796+
t.Errorf("ValuesRev() = %v, want1 %v", got, tt.want1)
797+
}
798+
})
799+
}
800+
}
801+
802+
func getValuesRevBreakCases(builder orderedCollIntBuilder) []*orderedTestCase {
803+
return []*orderedTestCase{
804+
{
805+
name: "ValuesRev() on three-item collection, break immediately",
806+
coll: builder.Three(),
807+
args: orderedIntArgs{
808+
predicate: func(_ int, v int) bool {
809+
return false
810+
},
811+
},
812+
want1: []int(nil),
813+
},
814+
{
815+
name: "ValuesRev() on three-item collection, break at middle",
816+
coll: builder.Three(),
817+
args: orderedIntArgs{
818+
predicate: func(_ int, v int) bool {
819+
return v > 222
820+
},
821+
},
822+
want1: []int{333},
823+
},
824+
{
825+
name: "ValuesRev() on three-item collection, break after middle",
826+
coll: builder.Three(),
827+
args: orderedIntArgs{
828+
predicate: func(_ int, v int) bool {
829+
return v >= 222
830+
},
831+
},
832+
want1: []int{333, 222},
833+
},
834+
}
835+
}
836+
837+
func testValuesRevBreak(t *testing.T, builder orderedCollIntBuilder) {
838+
cases := getValuesRevBreakCases(builder)
839+
for _, tt := range cases {
840+
t.Run(tt.name, func(t *testing.T) {
841+
got := []int(nil)
842+
for v := range tt.coll.ValuesRev() {
843+
if !tt.args.predicate(-1, v) {
844+
break
845+
}
846+
got = append(got, v)
847+
}
848+
if !reflect.DeepEqual(got, tt.want1) {
849+
t.Errorf("ValuesRev() = %v, want1 %v", got, tt.want1)
850+
}
851+
})
852+
}
853+
}

sequence.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,16 @@ func (c *comfySeq[V]) Values() iter.Seq[V] {
254254
}
255255
}
256256

257+
func (c *comfySeq[V]) ValuesRev() iter.Seq[V] {
258+
return func(yield func(V) bool) {
259+
for i := len(c.s) - 1; i >= 0; i-- {
260+
if !yield(c.s[i]) {
261+
break
262+
}
263+
}
264+
}
265+
}
266+
257267
// Private:
258268

259269
//nolint:unused

sequence_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,11 @@ func Test_comfySeq_Values(t *testing.T) {
276276
testValuesBreak(t, &comfySeqIntBuilder[baseInternal[int]]{})
277277
}
278278

279+
func Test_comfySeq_ValuesRev(t *testing.T) {
280+
testValuesRev(t, &comfySeqIntBuilder[orderedInternal[int]]{})
281+
testValuesRevBreak(t, &comfySeqIntBuilder[orderedInternal[int]]{})
282+
}
283+
279284
func Test_comfySeq_copy(t *testing.T) {
280285
testCopy(t, &comfySeqIntBuilder[baseInternal[int]]{})
281286
}

sequencecmp.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,18 @@ func (c *comfyCmpSeq[V]) Values() iter.Seq[V] {
348348
}
349349
}
350350

351+
func (c *comfyCmpSeq[V]) ValuesRev() iter.Seq[V] {
352+
return func(yield func(V) bool) {
353+
for i := len(c.s) - 1; i >= 0; i-- {
354+
if !yield(c.s[i]) {
355+
break
356+
}
357+
}
358+
}
359+
}
360+
361+
// Private:
362+
351363
//nolint:unused
352364
func (c *comfyCmpSeq[V]) copy() baseInternal[V] {
353365
ccl := &comfyCmpSeq[V]{

0 commit comments

Comments
 (0)