From: nomennescio Date: Sat, 14 Oct 2023 00:07:18 +0000 (+0200) Subject: Optimized minimum and maximum for cycles and element-repeats X-Git-Url: https://gitweb.factorcode.org/gitweb.cgi?p=factor.git;a=commitdiff_plain;h=c8dc61f1783f1e375bd7b0de11bbc4ac2d444f69 Optimized minimum and maximum for cycles and element-repeats --- diff --git a/extra/sequences/repeating/repeating-tests.factor b/extra/sequences/repeating/repeating-tests.factor index 1a31125d9d..291b12d840 100644 --- a/extra/sequences/repeating/repeating-tests.factor +++ b/extra/sequences/repeating/repeating-tests.factor @@ -1,14 +1,27 @@ -USING: sequences.repeating tools.test ; +USING: literals sequences sequences.repeating tools.test ; +{ { 1 } } [ { 1 2 3 } 1 cycle ] unit-test { { 1 2 3 1 2 } } [ { 1 2 3 } 5 cycle ] unit-test { { 1 2 3 1 2 3 1 2 3 } } [ { 1 2 3 } 9 cycle ] unit-test +{ $[ { 1 2 } minimum ] } [ { 1 2 3 } 2 minimum ] unit-test +{ $[ { 1 2 3 1 2 } minimum ] } [ { 1 2 3 } 5 minimum ] unit-test +{ $[ { 1 2 3 1 2 3 1 2 3 } minimum ] } [ { 1 2 3 } 9 minimum ] unit-test +{ $[ { 1 2 } maximum ] } [ { 1 2 3 } 2 maximum ] unit-test +{ $[ { 1 2 3 1 2 } maximum ] } [ { 1 2 3 } 5 maximum ] unit-test +{ $[ { 1 2 3 1 2 3 1 2 3 } maximum ] } [ { 1 2 3 } 9 maximum ] unit-test + { { } } [ { 1 2 3 } 0 repeat-elements ] unit-test { { 1 2 3 } } [ { 1 2 3 } 1 repeat-elements ] unit-test { { 1 1 2 2 3 3 } } [ { 1 2 3 } 2 repeat-elements ] unit-test { { 1 1 1 2 2 2 3 3 3 } } [ { 1 2 3 } 3 repeat-elements ] unit-test { { 1 1 1 1 2 2 2 2 3 3 3 3 } } [ { 1 2 3 } 4 repeat-elements ] unit-test +{ $[ { 1 1 1 2 2 2 3 3 3 } minimum ] } [ { 1 2 3 } 3 minimum ] unit-test +{ $[ { 1 1 1 1 2 2 2 2 3 3 3 3 } minimum ] } [ { 1 2 3 } 4 minimum ] unit-test +{ $[ { 1 1 1 2 2 2 3 3 3 } maximum ] } [ { 1 2 3 } 3 maximum ] unit-test +{ $[ { 1 1 1 1 2 2 2 2 3 3 3 3 } maximum ] } [ { 1 2 3 } 4 maximum ] unit-test + { { } } [ { 1 2 3 } 0 repeat ] unit-test { { 1 2 3 } } [ { 1 2 3 } 1 repeat ] unit-test { { 1 2 3 1 2 3 } } [ { 1 2 3 } 2 repeat ] unit-test diff --git a/extra/sequences/repeating/repeating.factor b/extra/sequences/repeating/repeating.factor index 71c0c5f7c9..b2eb049cfd 100644 --- a/extra/sequences/repeating/repeating.factor +++ b/extra/sequences/repeating/repeating.factor @@ -1,5 +1,6 @@ ! Copyright (C) 2008 Alex Chapman ! Copyright (C) 2012 John Benediktsson +! Copyright (C) 2023 nomennescio ! See https://factorcode.org/license.txt for BSD license USING: accessors circular kernel math sequences ; IN: sequences.repeating @@ -27,6 +28,15 @@ M: cycles virtual-exemplar circular>> ; INSTANCE: cycles virtual-sequence +> ] [ circular>> length ] bi >= ; inline + +PRIVATE> + +M: cycles minimum dup full-cycle? [ circular>> minimum ] [ (minimum) ] if ; inline +M: cycles maximum dup full-cycle? [ circular>> maximum ] [ (maximum) ] if ; inline + TUPLE: element-repeats < sequence-view { times integer read-only } ;