Warning, /education/kalzium/src/solver/chemset.ml is written in an unsupported language. File is not indexed.
0001 (*
0002 SPDX-FileCopyrightText: 2004 Thomas Nagy <tnagy2^8@yahoo.fr>
0003
0004 SPDX-License-Identifier: GPL-2.0-or-later
0005 *)
0006
0007 open Printf;;
0008 open Hashtbl;;
0009 open List;;
0010 open String;;
0011
0012 type chemtbl = (string, int) Hashtbl.t;;
0013 type chemrecord = {mutable hashtbl:chemtbl; mutable formula:string};;
0014 type item = {ikey:string; itbl:chemrecord; mutable sign:int};;
0015 type listitems = (item) list;;
0016
0017
0018 (* add a symbol to a molecule *)
0019 let chem_addsym (tbl:chemtbl) (sym:string) (qte:int) =
0020 let prev_qte = ref 0 in
0021 if Hashtbl.mem tbl sym then prev_qte := Hashtbl.find tbl sym;
0022 Hashtbl.replace tbl sym (!prev_qte+qte)
0023 ;;
0024
0025 (* add merge two sub_molecules *)
0026 let chem_add (tbl1:chemrecord) (tbl2:chemrecord) =
0027 Hashtbl.iter (fun sym qte -> chem_addsym tbl1.hashtbl sym qte) tbl2.hashtbl;
0028 tbl1.formula <- tbl1.formula^tbl2.formula;
0029 tbl1
0030 ;;
0031
0032 (* multiply a sub-molecule (amount of atoms) by an integer value *)
0033 let chem_mult (tbl:chemrecord) (qte:int) =
0034 Hashtbl.iter (fun sym old_qte-> Hashtbl.replace tbl.hashtbl sym (old_qte*qte) ) tbl.hashtbl;
0035 tbl.formula <- "("^tbl.formula^")"^string_of_int(qte);
0036 tbl
0037 ;;
0038
0039 (* creates a small molecule *)
0040 let createchem (sym:string) (qte:int) =
0041
0042 let prettyformula () =
0043 if String.contains sym '+' || String.contains sym '-' then begin
0044 if qte == 1 then "<b><sup>"^sym^"</sup></b>"
0045 else "<b><sup>"^string_of_int(qte)^sym^"</sup></b>" end
0046 else begin
0047 if qte == 1 then sym
0048 else sym^"<b><sub>"^string_of_int(qte)^"</sub></b>"
0049 end
0050 in
0051
0052 let table = Hashtbl.create 10 in
0053 Hashtbl.add table sym qte;
0054 { hashtbl=table ; formula=prettyformula() }
0055 (*if (qte!=1) then { hashtbl=table ; formula=prettyformula() }
0056 else { hashtbl=table ; formula=sym }*)
0057 ;;
0058
0059 let chem_negate (l:listitems) =
0060 List.iter (fun i -> i.sign <- -1) l
0061 ;;
0062
0063 (* outputs a molecule *)
0064 let chem_printitem (i:item) =
0065 Printf.printf "item : %s %s %d \n" i.ikey (i.itbl).formula i.sign;
0066 Hashtbl.iter (fun sym qte -> Printf.printf " * %s %d\n" sym qte) i.itbl.hashtbl
0067 ;;