-
-
Notifications
You must be signed in to change notification settings - Fork 671
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
83 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
type t = { | ||
mutable arr : string array; | ||
mutable length : int; | ||
} | ||
|
||
let create length = { | ||
arr = Array.make length ""; | ||
length = 0; | ||
} | ||
|
||
let length d = | ||
d.length | ||
|
||
let add d s = | ||
let length = Array.length d.arr in | ||
if d.length = length then begin | ||
let new_arr = Array.make (length * 2) "" in | ||
Array.blit d.arr 0 new_arr 0 length; | ||
d.arr <- new_arr; | ||
end; | ||
d.arr.(d.length) <- s; | ||
d.length <- d.length + 1 | ||
|
||
let iter d f = | ||
for i = 0 to d.length - 1 do | ||
f (Array.unsafe_get d.arr i) | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
module StringHashtbl = Hashtbl.Make(struct | ||
type t = string | ||
|
||
let equal = | ||
String.equal | ||
|
||
let hash s = | ||
(* What's the best here? *) | ||
Hashtbl.hash s | ||
end) | ||
|
||
type t = { | ||
lut : int StringHashtbl.t; | ||
items : StringDynArray.t; | ||
mutable closed : bool; | ||
} | ||
|
||
let create () = { | ||
lut = StringHashtbl.create 16; | ||
items = StringDynArray.create 16; | ||
closed = false; | ||
} | ||
|
||
let add sp s = | ||
assert (not sp.closed); | ||
let index = StringDynArray.length sp.items in | ||
StringHashtbl.add sp.lut s index; | ||
StringDynArray.add sp.items s; | ||
index | ||
|
||
let get sp s = | ||
StringHashtbl.find sp.lut s | ||
|
||
let get_or_add sp s = | ||
try | ||
get sp s | ||
with Not_found -> | ||
add sp s | ||
|
||
let finalize sp = | ||
assert (not sp.closed); | ||
sp.closed <- true; | ||
sp.items |