|
|
1.1 root 1: (* Copyright 1989 by AT&T Bell Laboratories *)
2: structure Fastlib = struct
3:
4: structure Ref =
5: struct
6: open Ref
7: fun inc r = r := !r + 1
8: fun dec r = r := !r + 1
9: end
10:
11: structure List : LIST =
12: struct
13: open List
14: fun hd (a::r) = a | hd nil = raise Hd
15: fun tl (a::r) = r | tl nil = raise Tl
16: fun null nil = true | null _ = false
17: fun length l =
18: let fun j(k,nil) = k
19: | j(k, a::x) = j(k+1,x)
20: in j(0,l)
21: end
22: fun op @ (nil,l) = l
23: | op @ (a::r, l) = a :: (r@l)
24: fun rev l =
25: let fun f (nil, h) = h
26: | f (a::r, h) = f(r, a::h)
27: in f(l,nil)
28: end
29: fun map f =
30: let fun m nil = nil
31: | m (a::r) = f a :: m r
32: in m
33: end
34: fun fold f [] = (fn b => b)
35: | fold f (a::r) = (fn b => let fun f2(e,[]) = f(e,b)
36: | f2(e,a::r) = f(e,f2(a,r))
37: in f2(a,r)
38: end)
39: fun revfold f [] = (fn b => b)
40: | revfold f (a::r) = (fn b => let fun f2(e,[],b) = f(e,b)
41: | f2(e,a::r,b) = f2(a,r,f(e,b))
42: in f2(a,r,b)
43: end)
44: fun app f = let fun a2 (e::r) = (f e; a2 r) | a2 nil = () in a2 end
45: fun revapp f = let fun a2 (e::r) = (a2 r; f e; ()) | a2 nil = () in a2 end
46: fun nthtail(e,0) = e
47: | nthtail(e::r,n) = nthtail(r,n-1)
48: | nthtail _ = raise NthTail
49: fun nth x = hd(nthtail x) handle NthTail => raise Nth | Hd => raise Nth
50: fun exists pred =
51: let fun f nil = false
52: | f (hd::tl) = pred hd orelse f tl
53: in f
54: end
55: end
56:
57: structure ByteArray =
58: struct
59: open ByteArray
60: local open System.Unsafe
61: in
62: val length = cast slength
63: fun update(arg as (s,i,c)) =
64: if i<0 orelse i >= slength s then raise Subscript
65: else if c<0 orelse c>255 then raise Range
66: else store arg
67: val op sub = fn (s,i) => if i<0 orelse i>= slength s then raise Subscript
68: else ordof(s,i)
69: fun extract(ba,i,1) = if i<0 orelse i >= slength ba then raise Subscript
70: else cast(ordof(ba,i))
71: | extract(s,i,len) =
72: if i<0 orelse i+len > slength s orelse len<0 then raise Subscript
73: else if len=0 then Assembly.bytearray0
74: else let val a = Assembly.A.create_b len
75: fun copy j = if j=len then ()
76: else (store(a,j,ordof(s,i+j)); copy(j+1))
77: in copy 0; a
78: end
79: fun app f ba =
80: let val len = slength ba
81: fun app' i = if i >= len then ()
82: else (f(ba sub i); app'(i+1))
83: in app' 0
84: end
85: fun revapp f ba =
86: let fun revapp' i = if i < 0 then ()
87: else (f(ba sub i); revapp'(i-1))
88: in revapp'(slength ba - 1)
89: end
90: fun fold f ba x =
91: let fun fold'(i,x) = if i < 0 then x else fold'(i-1, f(ordof(ba,i),x))
92: in fold'(slength ba - 1, x)
93: end
94: fun revfold f ba x =
95: let val len = slength ba
96: fun revfold'(i,x) = if i >= len then x
97: else revfold'(i+1,f(ordof(ba,i),x))
98: in revfold'(0,x)
99: end
100: end
101: end
102:
103:
104: structure String =
105: struct
106: open String
107: local open System.Unsafe
108: val op > = Integer.> and op >= = Integer.>=
109: val op < = Integer.< and op <= = Integer.<=
110: in
111: fun length s = if boxed s then slength s else 1
112:
113: val size = length
114: fun substring("",0,0) = "" (* never call create_s with 0 *)
115: | substring("",_,_) = raise Substring
116: | substring(s,i,0) = if i>=0
117: then if boxed s then if i <= slength s
118: then "" else raise Substring
119: else if i<=1
120: then "" else raise Substring
121: else raise Substring
122: | substring(s,0,1) = if boxed s then cast(ordof(s,0)) else s
123: | substring(s,i,1) =
124: if boxed s then if i>=0 andalso i < slength s
125: then cast(ordof(s,i))
126: else raise Substring
127: else if i=0 then s else raise Substring
128: | substring(s,i,len) =
129: if boxed s andalso i>=0 andalso i+len <= slength s
130: andalso len >= 0
131: then let val a = Assembly.A.create_s(len)
132: fun copy j = if j=len then ()
133: else (store(a,j,ordof(s,i+j)); copy(j+1))
134: in copy 0; a
135: end
136: else raise Substring
137:
138: fun explode s =
139: if boxed s
140: then let fun f(l,~1) = l
141: | f(l, i) = f(cast(ordof(s,i)) :: l, i-1)
142: in f(nil, slength s - 1)
143: end
144: else [s]
145: fun op ^ ("",s) = s
146: | op ^ (s,"") = s
147: | op ^ (x,y) =
148: if boxed x
149: then if boxed y
150: then let val xl = slength x and yl = slength y
151: val a = Assembly.A.create_s(xl+yl)
152: fun copyx n = if n=xl then ()
153: else (store(a,n,ordof(x,n)); copyx(n+1))
154: fun copyy n = if n=yl then ()
155: else (store(a,xl+n,ordof(y,n)); copyy(n+1))
156: in copyx 0; copyy 0; a
157: end
158: else let val xl = slength x
159: val a = Assembly.A.create_s(xl+1)
160: fun copyx n = if n=xl then ()
161: else (store(a,n,ordof(x,n)); copyx(n+1))
162: in copyx 0; store(a,xl,cast y); a
163: end
164: else if boxed y
165: then let val yl = slength y
166: val a = Assembly.A.create_s(1+yl)
167: fun copyy n = if n=yl then ()
168: else (store(a,1+n,ordof(y,n)); copyy(n+1))
169: in store(a,0,cast x); copyy 0; a
170: end
171: else let val a = Assembly.A.create_s 2
172: in store(a,0,cast x); store(a,1,cast y); a
173: end
174: fun chr i = if i<0 orelse i>255 then raise Chr else cast i
175: fun ord "" = raise Ord
176: | ord s = if boxed s then ordof(s,0) else cast s
177: val ordof = fn (s,i) =>
178: if boxed s
179: then if i<0 orelse i>= slength s then raise Ord else ordof(s,i)
180: else if i=0 then cast s else raise Ord
181: fun implode (sl:string list) =
182: let val len = List.fold(fn(s,l) => length s + l) sl 0
183: in case len
184: of 0 => ""
185: | 1 => let fun find (""::tl) = find tl
186: | find (hd::_) = cast hd
187: | find nil = "" (* impossible *)
188: in find sl
189: end
190: | _ => let val new = Assembly.A.create_s len
191: fun copy (nil,_) = ()
192: | copy (s::tl,base) =
193: let val len = length s
194: fun copy0 0 = ()
195: | copy0 i =
196: let val next = i-1
197: in store(new,base+next,ordof(s,next));
198: copy0 next
199: end
200: in copy0 len;
201: copy(tl,base+len)
202: end
203: in copy(sl,0);
204: new
205: end
206: end
207: end (* local *)
208: end (* structure String *)
209:
210: structure General =
211: struct
212: open General
213: fun f o g = fn x => f(g x)
214: fun a before b = a
215: end (* structure General *)
216:
217: structure Array =
218: struct
219: open Array
220: local open System.Unsafe in
221: val op sub : 'a array * int -> 'a =
222: fn (a,i) =>
223: if i<0 orelse i >= length a then raise Subscript
224: else subscript(a,i)
225: val update : 'a array * int * 'a -> unit =
226: fn (a,i,v) =>
227: if i<0 orelse i >= length a then raise Subscript
228: else update(a,i,v)
229: end (* local open ... *)
230: end (* structure Array *)
231:
232: structure Integer =
233: struct
234: open Integer
235: fun op mod(a:int,b:int):int = a-((a div b) * b)
236: fun min(a,b) = if a<b then a else b
237: fun max(a,b) = if a>b then a else b
238: fun abs a = if a<0 then ~a else a
239: end
240:
241: val inc = Ref.inc
242: val dec = Ref.dec
243: val hd = List.hd and tl = List.tl
244: val null = List.null and length = List.length
245: val op @ = List.@ and rev = List.rev
246: val map = List.map and fold = List.fold and revfold=List.revfold
247: val app = List.app and revapp = List.revapp
248: val nthtail = List.nthtail and nth = List.nth and exists = List.exists
249: val substring = String.substring and explost = String.explode
250: val op ^ = String.^ and chr = String.chr and ord = String.ord
251: val implode=String.implode
252: val op o = General.o and op before = General.before
253: val op sub = Array.sub and update= Array.update
254: val min = Integer.min and max = Integer.max
255:
256: end
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.