Import Upstream version 20180207
[hcoop/debian/mlton.git] / lib / mlton / basic / vector.sig
CommitLineData
7f918cf1
CE
1(* Copyright (C) 2009 Matthew Fluet.
2 * Copyright (C) 1999-2006 Henry Cejtin, Matthew Fluet, Suresh
3 * Jagannathan, and Stephen Weeks.
4 *
5 * MLton is released under a BSD-style license.
6 * See the file MLton-LICENSE for details.
7 *)
8
9signature VECTOR_STRUCTS =
10 sig
11 type 'a t
12 exception New
13
14 val length: 'a t -> int
15 val sub: 'a t * int -> 'a
16 val unfoldi: int * 'b * (int * 'b -> 'a * 'b) -> 'a t * 'b
17 end
18
19signature VECTOR =
20 sig
21 include VECTOR_STRUCTS
22
23 val compare: 'a t * 'a t * ('a * 'a -> order) -> order
24 val concat: 'a t list -> 'a t
25 val concatV: 'a t t -> 'a t
26 val contains: 'a t * 'a * ('a * 'a -> bool) -> bool
27 val copy: 'a t -> 'a t
28 val dropPrefix: 'a t * int -> 'a t
29 val dropSuffix: 'a t * int -> 'a t
30 val equals: 'a t * 'b t * ('a * 'b -> bool) -> bool
31 val exists: 'a t * ('a -> bool) -> bool
32 val existsi: 'a t * (int * 'a -> bool) -> bool
33 val existsR: 'a t * int * int * ('a -> bool) -> bool
34 val fold2: 'a t * 'b t * 'c * ('a * 'b * 'c -> 'c) -> 'c
35 val fold3From:
36 'a t * 'b t * 'c t * int * 'd * ('a * 'b * 'c * 'd -> 'd) -> 'd
37 val fold3: 'a t * 'b t * 'c t * 'd * ('a * 'b * 'c * 'd -> 'd) -> 'd
38 datatype ('a, 'b) continue =
39 Continue of 'a
40 | Done of 'b
41 (* fold' (v, i, b, f, g)
42 * folds over v starting at index i with state b, applying f to each
43 * index, vector element, and state, continuing depending on what f
44 * returns. If the end of the vector is reached, g is applied to the
45 * state.
46 *)
47
48 val first: 'a t -> 'a
49 val fold':
50 'a t * int * 'b * (int * 'a * 'b -> ('b, 'c) continue) * ('b -> 'c)
51 -> 'c
52 val fold: 'a t * 'b * ('a * 'b -> 'b) -> 'b
53 val foldFrom: 'a t * int * 'b * ('a * 'b -> 'b) -> 'b
54 val foldi: 'a t * 'b * (int * 'a * 'b -> 'b) -> 'b
55 val foldi2From: 'a t * 'b t * int * 'c * (int * 'a * 'b * 'c -> 'c) -> 'c
56 val foldr: 'a t * 'b * ('a * 'b -> 'b) -> 'b
57 val foldri: 'a t * 'b * (int * 'a * 'b -> 'b) -> 'b
58 val foldr2: 'a t * 'b t * 'c * ('a * 'b * 'c -> 'c) -> 'c
59 val forall: 'a t * ('a -> bool) -> bool
60 val forall2: 'a t * 'b t * ('a * 'b -> bool) -> bool
61 val foralli: 'a t * (int * 'a -> bool) -> bool
62 val foreach: 'a t * ('a -> unit) -> unit
63 val foreachi: 'a t * (int * 'a -> unit) -> unit
64 val foreachi2: 'a t * 'b t * (int * 'a * 'b -> unit) -> unit
65 val foreachr: 'a t * ('a -> unit) -> unit
66 val foreachri: 'a t * (int * 'a -> unit) -> unit
67 val foreach2: 'a t * 'b t * ('a * 'b -> unit) -> unit
68 val foreachR: 'a t * int * int * ('a -> unit) -> unit
69 val foreach3: 'a t * 'b t * 'c t * ('a * 'b * 'c -> unit) -> unit
70 val fromArray: 'a array -> 'a t
71 val fromList: 'a list -> 'a t
72 val fromListMap: 'a list * ('a -> 'b) -> 'b t
73 val fromListRev: 'a list -> 'a t
74 val indexi: 'a t * (int * 'a -> bool) -> int option
75 val index: 'a t * ('a -> bool) -> int option
76 val indices: bool t -> int t
77 val isEmpty: 'a t -> bool
78 val isSorted: 'a t * ('a * 'a -> bool) -> bool
79 (* isSortedRange (v, l, u, <=) checks if [l, u) is sorted. *)
80 val isSortedRange: 'a t * int * int * ('a * 'a -> bool) -> bool
81 val isSubsequence: 'a t * 'b t * ('a * 'b -> bool) -> bool
82 val keepAll: 'a t * ('a -> bool) -> 'a t
83 val keepAllMap: 'a t * ('a -> 'b option) -> 'b t
84 val keepAllMapi: 'a t * (int * 'a -> 'b option) -> 'b t
85 val keepAllMap2: 'a t * 'b t * ('a * 'b -> 'c option) -> 'c t
86 val keepAllSome: 'a option t -> 'a t
87 val last: 'a t -> 'a
88 val layout: ('a -> Layout.t) -> 'a t -> Layout.t
89 val loop: 'a t * ('a -> 'b option) * (unit -> 'b) -> 'b
90 val loopi: 'a t * (int * 'a -> 'b option) * (unit -> 'b) -> 'b
91 val map: 'a t * ('a -> 'b) -> 'b t
92 val map2: 'a t * 'b t * ('a * 'b -> 'c) -> 'c t
93 val map3: 'a t * 'b t * 'c t * ('a * 'b * 'c -> 'd) -> 'd t
94 val mapAndFold: 'a t * 'b * ('a * 'b -> 'c * 'b) -> 'c t * 'b
95 val map2AndFold: 'a t * 'b t * 'c * ('a * 'b * 'c -> 'd * 'c) -> 'd t * 'c
96 val mapi: 'a t * (int * 'a -> 'b) -> 'b t
97 val new: int * 'a -> 'a t
98 val new0: unit -> 'a t
99 val new1: 'a -> 'a t
100 val new2: 'a * 'a -> 'a t
101 val new3: 'a * 'a * 'a -> 'a t
102 val new4: 'a * 'a * 'a * 'a -> 'a t
103 val new5: 'a * 'a * 'a * 'a * 'a -> 'a t
104 val new6: 'a * 'a * 'a * 'a * 'a * 'a -> 'a t
105 val partition: 'a t * ('a -> bool) -> {no: 'a t, yes: 'a t}
106 val partitioni: 'a t * (int * 'a -> bool) -> {no: 'a t, yes: 'a t}
107 val peek: 'a t * ('a -> bool) -> 'a option
108 val peeki: 'a t * (int * 'a -> bool) -> (int * 'a) option
109 val peekMap: 'a t * ('a -> 'b option) -> 'b option
110 val peekMapi: 'a t * ('a -> 'b option) -> (int * 'b) option
111 val prefix: 'a t * int -> 'a t
112 val randomElement: 'a t -> 'a
113 val removeDuplicates: 'a t * ('a * 'a -> bool) -> 'a t
114 val removeFirst: 'a t * ('a -> bool) -> 'a t
115 val rev: 'a t -> 'a t
116 val size: 'a t -> int
117 val splitLast: 'a t -> 'a t * 'a
118 val tabulate: int * (int -> 'a) -> 'a t
119 val tabulator: int * (('a -> unit) -> unit) -> 'a t
120 val toArray: 'a t -> 'a array
121 val toList: 'a t -> 'a list
122 val toListMap: 'a t * ('a -> 'b) -> 'b list
123 val toListRev: 'a t -> 'a list
124 val toString: ('a -> string) -> 'a t -> string
125 val unzip: ('a * 'b) t -> 'a t * 'b t
126 val unzip3: ('a * 'b * 'c) t -> 'a t * 'b t * 'c t
127 val zip: 'a t * 'b t -> ('a * 'b) t
128 end
129
130functor TestVector (S: VECTOR): sig end =
131struct
132
133open S
134
135val _ =
136 Assert.assert
137 ("TestVector", fn () =>
138 let
139 fun check ls =
140 List.concat ls = toList (concat (List.map (ls, fromList)))
141 andalso List.concat ls = toList (concatV (fromListMap (ls, fromList)))
142 in
143 List.forall
144 ([[],
145 [[]],
146 [[], [1]],
147 [[1], []],
148 [[1], [], [2]],
149 [[1, 2], [3, 4]]],
150 check)
151 end)
152
153end