From ce599e4f9f94b4eb00c1b5edb85bce5431ab3df2 Mon Sep 17 00:00:00 2001 From: toma Date: Wed, 25 Nov 2009 17:56:58 +0000 Subject: Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features. BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdeedu@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- kalzium/src/solver/chemset.ml | 82 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 kalzium/src/solver/chemset.ml (limited to 'kalzium/src/solver/chemset.ml') diff --git a/kalzium/src/solver/chemset.ml b/kalzium/src/solver/chemset.ml new file mode 100644 index 00000000..d8136846 --- /dev/null +++ b/kalzium/src/solver/chemset.ml @@ -0,0 +1,82 @@ +(*************************************************************************** + * Copyright (C) 2004 by Thomas Nagy * + * tnagy2^8@yahoo.fr * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************) + +open Printf;; +open Hashtbl;; +open List;; +open String;; + +type chemtbl = (string, int) Hashtbl.t;; +type chemrecord = {mutable hashtbl:chemtbl; mutable formula:string};; +type item = {ikey:string; itbl:chemrecord; mutable sign:int};; +type listitems = (item) list;; + + +(* add a symbol to a molecule *) +let chem_addsym (tbl:chemtbl) (sym:string) (qte:int) = + let prev_qte = ref 0 in + if Hashtbl.mem tbl sym then prev_qte := Hashtbl.find tbl sym; + Hashtbl.replace tbl sym (!prev_qte+qte) +;; + +(* add merge two sub_molecules *) +let chem_add (tbl1:chemrecord) (tbl2:chemrecord) = + Hashtbl.iter (fun sym qte -> chem_addsym tbl1.hashtbl sym qte) tbl2.hashtbl; + tbl1.formula <- tbl1.formula^tbl2.formula; + tbl1 +;; + +(* multiply a sub-molecule (amount of atoms) by an integer value *) +let chem_mult (tbl:chemrecord) (qte:int) = + Hashtbl.iter (fun sym old_qte-> Hashtbl.replace tbl.hashtbl sym (old_qte*qte) ) tbl.hashtbl; + tbl.formula <- "("^tbl.formula^")"^string_of_int(qte); + tbl +;; + +(* creates a small molecule *) +let createchem (sym:string) (qte:int) = + + let prettyformula () = + if String.contains sym '+' || String.contains sym '-' then begin + if qte == 1 then ""^sym^"" + else ""^string_of_int(qte)^sym^"" end + else begin + if qte == 1 then sym + else sym^""^string_of_int(qte)^"" + end + in + + let table = Hashtbl.create 10 in + Hashtbl.add table sym qte; + { hashtbl=table ; formula=prettyformula() } + (*if (qte!=1) then { hashtbl=table ; formula=prettyformula() } + else { hashtbl=table ; formula=sym }*) +;; + +let chem_negate (l:listitems) = + List.iter (fun i -> i.sign <- -1) l +;; + +(* outputs a molecule *) +let chem_printitem (i:item) = + Printf.printf "item : %s %s %d \n" i.ikey (i.itbl).formula i.sign; + Hashtbl.iter (fun sym qte -> Printf.printf " * %s %d\n" sym qte) i.itbl.hashtbl +;; + -- cgit v1.2.3