Initial snarf.
[shack.git] / libmojave / util / lm_listbuf.ml
blob3b5afdc59a1dc7e8054673a3e4fdde396346cacd
1 (*
2 Simple imperative list buffer utility (used for instruction buffers)
3 Copyright (C) 2002,2001 Justin David Smith, Caltech
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation,
8 version 2.1 of the License.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 Additional permission is given to link this library with the
20 OpenSSL project's "OpenSSL" library, and with the OCaml runtime,
21 and you may distribute the linked executables. See the file
22 LICENSE.libmojave for more details.
26 (*** Type definitions ***)
28 type 'a t =
29 Empty
30 | FwdList of 'a list
31 | RevList of 'a list
34 (*** Functions ***)
36 let empty = Empty
38 let of_elt elt = RevList [elt]
40 let of_list ls = FwdList ls
42 let to_list buf =
43 match buf with
44 Empty ->
46 | FwdList buf ->
47 buf
48 | RevList buf ->
49 List.rev buf
51 let add buf elt =
52 match buf with
53 Empty ->
54 RevList [elt]
55 | FwdList buf ->
56 RevList (elt :: List.rev buf)
57 | RevList buf ->
58 RevList (elt :: buf)
60 let add_list buf ls =
61 let rec add_list buf = function
62 elt :: ls ->
63 add_list (elt :: buf) ls
64 | [] ->
65 RevList buf
67 match buf with
68 Empty ->
69 FwdList ls
70 | FwdList buf ->
71 add_list (List.rev buf) ls
72 | RevList buf ->
73 add_list buf ls
75 let add_rev_list buf ls =
76 let rec add_rev_list buf = function
77 elt :: ls ->
78 elt :: (add_rev_list buf ls)
79 | [] ->
80 buf
82 match buf with
83 Empty ->
84 RevList ls
85 | FwdList buf ->
86 RevList (add_rev_list (List.rev buf) ls)
87 | RevList buf ->
88 RevList (add_rev_list buf ls)
90 let add_listbuf buf ls =
91 let rec add_listbuf_rev buf = function
92 elt :: ls ->
93 elt :: (add_listbuf_rev buf ls)
94 | [] ->
95 buf
97 let add_listbuf_rev buf ls = RevList (add_listbuf_rev buf ls) in
98 match buf, ls with
99 Empty, _ ->
101 | _, Empty ->
103 | _, FwdList ls ->
104 add_list buf ls
105 | FwdList buf, RevList ls ->
106 add_listbuf_rev (List.rev buf) ls
107 | RevList buf, RevList ls ->
108 add_listbuf_rev buf ls