1 (***************************************************************************
2 * Copyright (C) 2004 by Thomas Nagy *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program 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 *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************)
26 type chemtbl
= (string, int) Hashtbl.t
;;
27 type chemrecord
= {mutable hashtbl
:chemtbl
; mutable formula
:string};;
28 type item
= {ikey
:string; itbl
:chemrecord
; mutable sign
:int};;
29 type listitems
= (item
) list
;;
32 (* add a symbol to a molecule *)
33 let chem_addsym (tbl
:chemtbl
) (sym
:string) (qte
:int) =
34 let prev_qte = ref 0 in
35 if Hashtbl.mem tbl sym
then prev_qte := Hashtbl.find tbl sym
;
36 Hashtbl.replace tbl sym
(!prev_qte+qte
)
39 (* add merge two sub_molecules *)
40 let chem_add (tbl1
:chemrecord
) (tbl2
:chemrecord
) =
41 Hashtbl.iter
(fun sym qte
-> chem_addsym tbl1
.hashtbl sym qte
) tbl2
.hashtbl
;
42 tbl1
.formula
<- tbl1
.formula^tbl2
.formula
;
46 (* multiply a sub-molecule (amount of atoms) by an integer value *)
47 let chem_mult (tbl
:chemrecord
) (qte
:int) =
48 Hashtbl.iter
(fun sym old_qte
-> Hashtbl.replace tbl
.hashtbl sym
(old_qte
*qte
) ) tbl
.hashtbl
;
49 tbl
.formula
<- "("^tbl
.formula^
")"^string_of_int
(qte
);
53 (* creates a small molecule *)
54 let createchem (sym
:string) (qte
:int) =
56 let prettyformula () =
57 if String.contains sym '
+'
|| String.contains sym '
-'
then begin
58 if qte
== 1 then "<b><sup>"^sym^
"</sup></b>"
59 else "<b><sup>"^string_of_int
(qte
)^sym^
"</sup></b>" end
62 else sym^
"<b><sub>"^string_of_int
(qte
)^
"</sub></b>"
66 let table = Hashtbl.create
10 in
67 Hashtbl.add
table sym qte
;
68 { hashtbl
=table ; formula
=prettyformula() }
69 (*if (qte!=1) then { hashtbl=table ; formula=prettyformula() }
70 else { hashtbl=table ; formula=sym }*)
73 let chem_negate (l
:listitems
) =
74 List.iter
(fun i
-> i
.sign
<- -1) l
77 (* outputs a molecule *)
78 let chem_printitem (i
:item
) =
79 Printf.printf
"item : %s %s %d \n" i
.ikey
(i
.itbl
).formula i
.sign
;
80 Hashtbl.iter
(fun sym qte
-> Printf.printf
" * %s %d\n" sym qte
) i
.itbl
.hashtbl