Skip to content

Commit

Permalink
[enhance] db3: add ralist module, with map interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Raja committed Jul 6, 2011
1 parent 7d965b9 commit b82f33d
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 1 deletion.
2 changes: 2 additions & 0 deletions database.mllib
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,6 @@ database/Dbgraph
database/Xml_dump
#database/Xml_import
#Internal structs
database/db3/structs/SigListMap
database/db3/structs/RaListMap
database/db3/structs/TabMap
2 changes: 1 addition & 1 deletion database/db3/structs/_tags
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# -*- conf -*- (for emacs)

<tabMap.ml>: with_mlstate_debug
<{tabMap,simpleListMap}.ml>: with_mlstate_debug
84 changes: 84 additions & 0 deletions database/db3/structs/raListMap.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
(*
Copyright © 2011 MLstate
This file is part of OPA.
OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.
OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.
You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)


module RA = RAList
module RAL = RAList.AsList
module RAA = RAList.AsArray
exception Found

type key = Revision.t
type 'a t = (Revision.t * 'a) RA.ra_list

let empty = RAL.empty

let is_empty = RAL.is_empty

let add key value lst =
try
let kk,_ = RAL.head lst in
(match Revision.compare key kk with
| 0 -> RAA.update lst 0 (key,value)
| 1 -> RAL.cons (key,value) lst
| -1 -> (
(* TODO imlement *)
assert false)
| _ -> assert false)
with RA.Empty ->
RAL.cons (key,value) lst
(*
if not (RAL.is_empty lst) then
assert (Revision.compare (fst (RAL.head lst)) key = -1
&& (error "Try to insert an older revision : %s, head is at %s"
(Revision.to_string key) (Revision.to_string (fst (RAL.head lst))); false));
RAL.cons (key,value) lst
*)
let fold f lst acc =
let f = fun (k,v) a -> f k v a in
RAL.fold f lst acc

let iter f lst =
let f = fun k v () -> f k v in
fold f lst ()

let rev_iter f lst =
let f = fun (k,v) () -> f k v in
RAL.rev_fold f lst ()

let find key lst =
let res = ref None in
let f = fun k v -> if Revision.equal key k then (res := Some v; raise Found) in
try iter f lst; raise Not_found
with Found -> Option.get !res

let find_inf key lst =
let res = ref None in
let f = fun k v -> if Revision.compare key k =1 then (res := Some (k,v); raise Found) in
try iter f lst; raise Not_found
with Found -> Option.get !res

let size = RAA.size

let max = RAL.head

let keys lst =
let f = fun k _ a -> k :: a in
fold f lst []

let remove_last = RAL.tail
19 changes: 19 additions & 0 deletions database/db3/structs/raListMap.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(*
Copyright © 2011 MLstate
This file is part of OPA.
OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.
OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.
You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)

include SigListMap.S
37 changes: 37 additions & 0 deletions database/db3/structs/sigListMap.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
(*
Copyright © 2011 MLstate
This file is part of OPA.
OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.
OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.
You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)

module type S =
sig
type key = Revision.t
type 'a t

val empty: 'a t
val is_empty : 'a t -> bool
val add: key -> 'a -> 'a t -> 'a t
val fold: (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b
val iter : (key -> 'a -> unit) -> 'a t -> unit
val rev_iter : (key -> 'a -> unit) -> 'a t -> unit
val find : key -> 'a t -> 'a
val find_inf : key -> 'a t -> key * 'a
val size : 'a t -> int
val max : 'a t -> key * 'a
val remove_last : 'a t -> 'a t
val keys : 'a t -> key list

end

0 comments on commit b82f33d

Please sign in to comment.