mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
bug fix for CCList.range_by
This commit is contained in:
parent
fb97af680b
commit
f57d7267ac
1 changed files with 13 additions and 7 deletions
|
|
@ -793,23 +793,29 @@ module Idx = struct
|
|||
end
|
||||
|
||||
let range_by ~step i j =
|
||||
if step = 0 then raise (Invalid_argument "CCList.range_by");
|
||||
let rec up i j acc =
|
||||
if i>j then acc else up i (j-step) (j::acc)
|
||||
and down i j acc =
|
||||
if i<j then acc else down i (j-step) (j::acc)
|
||||
let rec range i j acc =
|
||||
if i=j then i::acc else range i (j-step) (j::acc)
|
||||
in
|
||||
let j = (j - i) / step * step + i in
|
||||
if step > 0 then up i j [] else down i j []
|
||||
if step = 0 then
|
||||
raise (Invalid_argument "CCList.range_by")
|
||||
else if (if step > 0 then i>j else i<j) then
|
||||
[]
|
||||
else
|
||||
range i ((j-i)/step*step + i) []
|
||||
|
||||
(* note: the last test checks that no error occurs due to overflows. *)
|
||||
(*$T
|
||||
range_by ~step:1 0 0 = [0]
|
||||
range_by ~step:1 5 0 = []
|
||||
range_by ~step:2 1 0 = []
|
||||
range_by ~step:2 0 4 = [0;2;4]
|
||||
range_by ~step:2 0 5 = [0;2;4]
|
||||
range_by ~step:~-1 0 0 = [0]
|
||||
range_by ~step:~-1 0 5 = []
|
||||
range_by ~step:~-2 0 1 = []
|
||||
range_by ~step:~-2 5 1 = [5;3;1]
|
||||
range_by ~step:~-2 5 0 = [5;3;1]
|
||||
range_by ~step:max_int 0 2 = [0]
|
||||
*)
|
||||
|
||||
let range i j =
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue