Source

ocaml-lib / index.ml

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
let marshal x = Marshal.to_string x []
let unmarshal x = Marshal.from_string x 0

class type ['a,'b] index =
  object
    method to_string : string (* marshalling *)
    method from_string : string -> unit (* unmarshalling *)
    method get : 'a -> 'b
    method set : 'a -> 'b -> unit
    method update : 'a -> ('b -> 'b) -> unit
    method reset : 'a -> unit
    method reset_all : unit
    method iter : ('a -> 'b -> unit) -> unit
    method fold : 'c . ('a -> 'b -> 'c -> 'c) -> 'c -> 'c
  end

class ['a,'b] hashtbl (size : int) (init : 'a -> 'b) : ['a,'b] index =
  object
    val mutable ht : ('a,'b) Hashtbl.t = Hashtbl.create size

    method to_string = marshal ht

    method from_string s = ht <- unmarshal s

    method get k =
      try Hashtbl.find ht k
      with Not_found ->
	let v = init k in
	Hashtbl.add ht k v;
	v

    method set k v =
      Hashtbl.replace ht k v

    method update k f =
      let old = (* inlining of get *)
	try Hashtbl.find ht k
	with Not_found -> init k in
      Hashtbl.replace ht k (f old)

    method reset k =
      Hashtbl.remove ht k

    method reset_all = Hashtbl.clear ht

    method iter f = Hashtbl.iter f ht

    method fold : 'c . ('a -> 'b -> 'c -> 'c) -> 'c -> 'c =
      fun f e -> Hashtbl.fold f ht e
  end

class ['a] vector (size : int) (null : 'a) (init : int -> 'a) : [int,'a] index =
  object
    val mutable ar : 'a array = Array.make size null

    method to_string = marshal ar

    method from_string s = ar <- unmarshal s

    method get i =
      if ar.(i) = null then ar.(i) <- init i;
      ar.(i)

    method set i v =
      ar.(i) <- v

    method update i f =
      if ar.(i) = null then ar.(i) <- init i;
      ar.(i) <- f ar.(i)

    method reset i =
      ar.(i) <- null

    method reset_all =
      for i = 0 to size - 1 do
	ar.(i) <- null
      done

    method iter f =
      for i = 0 to size - 1 do
	if ar.(i) <> null
	then f i ar.(i)
      done

    method fold : 'c . (int -> 'a -> 'c -> 'c) -> 'c -> 'c =
      fun f e ->
	let res = ref e in
	for i = 0 to size - 1 do
	  if ar.(i) <> null
	  then res := f i ar.(i) !res
	done;
	!res
  end

class ['a] vector_opt (size : int) (init : int -> 'a) : [int,'a] index =
  object
    val mutable ar : 'a option array = Array.make size None

    method to_string = marshal ar

    method from_string s = ar <- unmarshal s

    method get i =
      match ar.(i) with
      | None ->
	  let v = init i in
	  ar.(i) <- Some v;
	  v
      | Some v ->
	  v

    method set i v =
      ar.(i) <- Some v

    method update i f =
      match ar.(i) with
      | None ->
	  ar.(i) <- Some (f (init i))
      | Some v ->
	  ar.(i) <- Some (f v)

    method reset i =
      ar.(i) <- None

    method reset_all =
      for i = 0 to size - 1 do
	ar.(i) <- None
      done

    method iter f =
      for i = 0 to size - 1 do
	match ar.(i) with
	| None -> ()
	| Some v -> f i v
      done

    method fold : 'c . (int -> 'a -> 'c -> 'c) -> 'c -> 'c =
      fun f e ->
	let res = ref e in
	for i = 0 to size - 1 do
	  match ar.(i) with
	  | None -> ()
	  | Some v -> res := f i v !res
	done;
	!res
  end

class ['a] var (null : 'a) (init : unit -> 'a) : [unit,'a] index =
  object
    val mutable x : 'a = null

    method to_string = marshal x

    method from_string s = x <- unmarshal s

    method get () =
      if x = null then x <- init ();
      x

    method set () v = x <- v

    method update () f = x <- f x

    method reset () = x <- null

    method reset_all = x <- null

    method iter f = if x <> null then f () x

    method fold : 'c . (unit -> 'a -> 'c -> 'c) -> 'c -> 'c =
      fun f e -> if x <> null then f () x e else e
  end

class ['a] var_opt (init : unit -> 'a) : [unit,'a] index =
  object
    val mutable x : 'a option = None

    method to_string = marshal x

    method from_string s = x <- unmarshal s

    method get () =
      match x with
      | None ->
	  let v = init () in
	  x <- Some v;
	  v
      | Some v -> v

    method set () v = x <- Some v

    method update () f =
      match x with
      | None ->
	  x <- Some (f (init ()))
      | Some v ->
	  x <- Some (f v)

    method reset () = x <- None

    method reset_all = x <- None

    method iter f =
      match x with
      | None -> ()
      | Some v -> f () v

    method fold : 'c . (unit -> 'a -> 'c -> 'c) -> 'c -> 'c =
      fun f e ->
	match x with
	| None -> e
	| Some v -> f () v e
  end

type 'a va = VarCons of 'a va * int * 'a array | VarNil

class ['a] varray (size : int) (null : 'a) (init : int -> 'a) : [int,'a] index =
  object (self)
    val mutable var : 'a va = VarCons (VarNil, 0, Array.make size null)

    method to_string = marshal var

    method from_string s = var <- unmarshal s

    method get i =
      let rec aux = function
	| VarCons (l,s,r) as var1 ->
	    if i >= s
	    then
	      let lr = Array.length r in
	      let i' = i - s in
	      if i' < lr
	      then begin
		if r.(i') = null then r.(i') <- init i;
		r.(i') end
	      else begin
		assert (var1 == var);  (* we know that the current VarCons is var because i is out of bounds *)
		let v = init i in
		let s' = s + lr in
		let lr' = max s' (i - s' + 1) in
		let r' = Array.make lr' null in
		r'.(i - s') <- v;
		var <- VarCons (var, s', r');
		v end
	    else aux l
	| VarNil ->
	    raise (Invalid_argument "negative index") in
      aux var

    method set i v =
      let rec aux = function
	| VarCons (l,s,r) ->
	    if i >= s
	    then
	      let lr = Array.length r in
	      let i' = i - s in
	      if i' < lr
	      then
		r.(i') <- v
	      else begin
		let s' = s + lr in
		let lr' = max s' (i - s' + 1) in
		let r' = Array.make lr' null in
		r'.(i - s') <- v;
		var <- VarCons (var, s', r')
	      end
	    else aux l
	| VarNil ->
	    raise (Invalid_argument "negative index") in
      aux var

    method update i f =
      self # set i (f (self # get i))

    method reset i =
      let rec aux = function
	| VarCons (l,s,r) ->
	    if i >= s
	    then
	      let i' = i - s in
	      if i' < Array.length r
	      then r.(i') <- null
	      else ()
	    else aux l
	| VarNil ->
	    raise (Invalid_argument "negative index") in
      aux var

    method reset_all =
      let rec aux = function
	| VarCons (l,s,r) ->
	    for i' = 0 to Array.length r - 1 do
	      r.(i') <- null
	    done;
	    aux l
	| VarNil -> () in
      aux var

    method iter f =
      let rec aux = function
	| VarCons (l,s,r) ->
	    for i' = Array.length r - 1 downto 0 do
	      if r.(i') <> null
	      then f (s + i') r.(i')
	    done;
	    aux l
	| VarNil -> () in
      aux var

    method fold : 'c . (int -> 'a -> 'c -> 'c) -> 'c -> 'c =
      fun f e ->
	let rec aux acc = function
	  | VarCons (l,s,r) ->
	      let res = ref acc in
	      for i' = Array.length r - 1 downto 0 do
		if r.(i') <> null
		then res := f (s + i') r.(i') !res
	      done;
	      aux !res l
	  | VarNil -> acc in
	aux e var
  end

class ['a] varray_opt (size : int) (init : int -> 'a) : [int,'a] index =
  object (self)
    val mutable var : 'a option va = VarCons (VarNil, 0, Array.make size None)

    method to_string = marshal var

    method from_string s = var <- unmarshal s

    method get i =
      let rec aux = function
	| VarCons (l,s,r) as var1 ->
	    if i >= s
	    then
	      let lr = Array.length r in
	      let i' = i - s in
	      if i' < lr
	      then
		match r.(i') with
		| None ->
		    let v = init i in
		    r.(i') <- Some v;
		    v
		| Some v -> v
	      else begin
		assert (var1 == var);  (* we know that the current VarCons is var because i is out of bounds *)
		let v = init i in
		let s' = s + lr in
		let lr' = max s' (i - s' + 1) in
		let r' = Array.make lr' None in
		r'.(i - s') <- Some v;
		var <- VarCons (var, s', r');
		v end
	    else aux l
	| VarNil ->
	    raise (Invalid_argument "negative index") in
      aux var

    method set i v =
      let rec aux = function
	| VarCons (l,s,r) ->
	    if i >= s
	    then
	      let lr = Array.length r in
	      let i' = i - s in
	      if i' < lr
	      then
		r.(i') <- Some v
	      else begin
		let s' = s + lr in
		let lr' = max s' (i - s' + 1) in
		let r' = Array.make lr' None in
		r'.(i - s') <- Some v;
		var <- VarCons (var, s', r')
	      end
	    else aux l
	| VarNil ->
	    raise (Invalid_argument "negative index") in
      aux var

    method update i f =
      self # set i (f (self # get i))

    method reset i =
      let rec aux = function
	| VarCons (l,s,r) ->
	    if i >= s
	    then
	      let i' = i - s in
	      if i' < Array.length r
	      then r.(i') <- None
	      else ()
	    else aux l
	| VarNil ->
	    raise (Invalid_argument "negative index") in
      aux var

    method reset_all =
      let rec aux = function
	| VarCons (l,s,r) ->
	    for i' = 0 to Array.length r - 1 do
	      r.(i') <- None
	    done;
	    aux l
	| VarNil -> () in
      aux var

    method iter f =
      let rec aux = function
	| VarCons (l,s,r) ->
	    for i' = Array.length r - 1 downto 0 do
	      match r.(i') with
	      | None -> ()
	      | Some v -> f (s + i') v
	    done;
	    aux l
	| VarNil -> () in
      aux var

    method fold : 'c . (int -> 'a -> 'c -> 'c) -> 'c -> 'c =
      fun f e ->
	let rec aux acc = function
	  | VarCons (l,s,r) ->
	      let res = ref acc in
	      for i' = Array.length r - 1 downto 0 do
		match r.(i') with
		| None -> ()
		| Some v -> res := f (s + i') v !res
	      done;
	      aux !res l
	  | VarNil -> acc in
	aux e var
  end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.