Index: molecuilder/src/periodentafel.cpp
===================================================================
--- molecuilder/src/periodentafel.cpp	(revision e6fe8a5981d5b0684c89c93c89221f4be0ac59d5)
+++ molecuilder/src/periodentafel.cpp	(revision 5dba7ae88fa00048a0010e446e197388daab7b69)
@@ -10,4 +10,5 @@
 #include <fstream>
 #include <cstring>
+#include <cassert>
 
 #include "element.hpp"
@@ -18,4 +19,6 @@
 #include "verbose.hpp"
 
+using namespace std;
+
 /************************************* Functions for class periodentafel ***************************/
 
@@ -23,11 +26,6 @@
  * Initialises start and end of list and resets periodentafel::checkliste to false.
  */
-periodentafel::periodentafel() : start(new element), end(new element)
-{
-  start->previous = NULL;
-  start->next = end;
-  end->previous = start;
-  end->next = NULL;
-};
+periodentafel::periodentafel()
+{};
 
 /** destructor for class periodentafel
@@ -37,6 +35,4 @@
 {
   CleanupPeriodtable();
-  delete(end);
-  delete(start);
 };
 
@@ -45,10 +41,13 @@
  * \return true - succeeded, false - does not occur
  */
-bool periodentafel::AddElement(element * const pointer)
-{
+periodentafel::iterator periodentafel::AddElement(element * const pointer)
+{
+  atomicNumber_t Z = pointer->getNumber();
+  assert(!elements.count(Z));
   pointer->sort = &pointer->Z;
-  if (pointer->Z < 1 && pointer->Z >= MAX_ELEMENTS)
+  if (pointer->getNumber() < 1 && pointer->getNumber() >= MAX_ELEMENTS)
     Log() << Verbose(0) << "Invalid Z number!\n";
-  return add(pointer, end);
+  pair<iterator,bool> res = elements.insert(pair<atomicNumber_t,element*>(Z,pointer));
+  return res.first;
 };
 
@@ -57,7 +56,8 @@
  * \return true - succeeded, false - element not found
  */
-bool periodentafel::RemoveElement(element * const pointer)
-{
-  return remove(pointer, start, end);
+void periodentafel::RemoveElement(element * const pointer)
+{
+  atomicNumber_t Z = pointer->getNumber();
+  elements.erase(Z);
 };
 
@@ -65,7 +65,7 @@
  * \return true - succeeded, false - does not occur
  */
-bool periodentafel::CleanupPeriodtable()
-{
-  return cleanup(start,end);
+void periodentafel::CleanupPeriodtable()
+{
+  elements.clear();
 };
 
@@ -75,8 +75,8 @@
  * \return pointer to element or NULL if not found
  */
-element * const periodentafel::FindElement(const int Z) const
-{
-  element *walker = find(&Z, start,end);
-  return(walker);
+const element * periodentafel::FindElement(atomicNumber_t Z) const
+{
+  const_iterator res = elements.find(Z);
+  return res!=elements.end()?((*res).second):0;
 };
 
@@ -86,20 +86,21 @@
  * \return pointer to element
  */
-element * const periodentafel::FindElement(const char * const shorthand) const
-{
-  element *walker =  periodentafel::start;
-  while (walker->next != periodentafel::end) {
-    walker = walker->next;
-    if (strncmp(walker->symbol, shorthand, 3) == 0)
-      return(walker);
+const element * periodentafel::FindElement(const char * const shorthand) const
+{
+  element *res = 0;
+  for(const_iterator iter=elements.begin();iter!=elements.end();++iter) {
+    if((*iter).second->getSymbol() == shorthand){
+      res = (*iter).second;
+      break;
+    }
   }
-  return (NULL);
+  return res;
 };
 
 /** Asks for element number and returns pointer to element
  */
-element * const periodentafel::AskElement() const
-{
-  element *walker = NULL;
+const element * periodentafel::AskElement() const
+{
+  const element *walker = NULL;
   int Z;
   do {
@@ -114,37 +115,56 @@
  * \return pointer to either present or newly created element
  */
-element * const periodentafel::EnterElement()
-{
-  element *walker = NULL;
-  int Z = -1;
+const element * periodentafel::EnterElement()
+{
+  const element *res = NULL;
+  atomicNumber_t Z = 0;
   Log() << Verbose(0) << "Atomic number: " << Z << endl;
   cin >> Z;
-  walker = FindElement(Z);
-  if (walker == NULL) {
+  res = FindElement(Z);
+  if (!res) {
+    // TODO: make this using the constructor
+    element *tmp;
     Log() << Verbose(0) << "Element not found in database, please enter." << endl;
-    walker = new element;
-    walker->Z = Z;
+    tmp = new element;
+    tmp->Z = Z;
     Log() << Verbose(0) << "Mass: " << endl;
-    cin >> walker->mass;
+    cin >> tmp->mass;
     Log() << Verbose(0) << "Name [max 64 chars]: " << endl;
-    cin >> walker->name;
+    cin >> tmp->name;
     Log() << Verbose(0) << "Short form [max 3 chars]: " << endl;
-    cin >> walker->symbol;
-    periodentafel::AddElement(walker);
+    cin >> tmp->symbol;
+    AddElement(tmp);
+    res = tmp;
   }
-  return(walker);
-};
+  return res;
+};
+
+
+/******************** Access to iterators ****************************/
+periodentafel::const_iterator periodentafel::begin(){
+  return elements.begin();
+}
+
+periodentafel::const_iterator periodentafel::end(){
+  return elements.end();
+}
+
+periodentafel::reverse_iterator periodentafel::rbegin(){
+  return reverse_iterator(elements.end());
+}
+
+periodentafel::reverse_iterator periodentafel::rend(){
+  return reverse_iterator(elements.begin());
+}
 
 /** Prints period table to given stream.
  * \param output stream
  */
-bool periodentafel::Output(ofstream * const output) const
+bool periodentafel::Output(ostream * const output) const
 {
   bool result = true;
-  element *walker = start;
   if (output != NULL) {
-    while (walker->next != end) {
-      walker = walker->next;
-      result = result && walker->Output(output);
+    for(const_iterator iter=elements.begin(); iter !=elements.end();++iter){
+      result = result && (*iter).second->Output(output);
     }
     return result;
@@ -157,7 +177,6 @@
  * \param *checkliste elements table for this molecule
  */
-bool periodentafel::Checkout(ofstream * const output, const int * const checkliste) const
-{
-  element *walker = start;
+bool periodentafel::Checkout(ostream * const output, const int * const checkliste) const
+{
   bool result = true;
   int No = 1;
@@ -166,9 +185,8 @@
     *output << "# Ion type data (PP = PseudoPotential, Z = atomic number)" << endl;
     *output << "#Ion_TypeNr.\tAmount\tZ\tRGauss\tL_Max(PP)L_Loc(PP)IonMass\t# chemical name, symbol" << endl;
-    while (walker->next != end) {
-      walker = walker->next;
-      if ((walker != NULL) && (walker->Z > 0) && (walker->Z < MAX_ELEMENTS) && (checkliste[walker->Z])) {
-        walker->No = No;
-        result = result && walker->Checkout(output, No++, checkliste[walker->Z]);
+    for(const_iterator iter=elements.begin(); iter!=elements.end();++iter){
+      if (((*iter).first < MAX_ELEMENTS) && (checkliste[(*iter).first])) {
+        (*iter).second->No = No;
+        result = result && (*iter).second->Checkout(output, No++, checkliste[(*iter).first]);
       }
     }
@@ -184,6 +202,6 @@
 {
   ifstream infile;
-  double tmp;
   element *ptr;
+  map<atomicNumber_t,element*> parsedElems;
   bool status = true;
   bool otherstatus = true;
@@ -223,5 +241,5 @@
       //neues->Output((ofstream *)&cout);
       if ((neues->Z > 0) && (neues->Z < MAX_ELEMENTS))
-        periodentafel::AddElement(neues);
+        parsedElems[neues->getNumber()] = neues;
       else {
         Log() << Verbose(0) << "Could not parse element: ";
@@ -243,7 +261,8 @@
   if (infile != NULL) {
     while (!infile.eof()) {
-      infile >> tmp;
-      infile >> ws;
-      infile >> FindElement((int)tmp)->Valence;
+      atomicNumber_t Z;
+      infile >> Z;
+      infile >> ws;
+      infile >> parsedElems[Z]->Valence;
       infile >> ws;
       //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->Valence << " valence electrons." << endl;
@@ -261,7 +280,8 @@
   if (infile != NULL) {
     while (!infile.eof()) {
-      infile >> tmp;
-      infile >> ws;
-      infile >> FindElement((int)tmp)->NoValenceOrbitals;
+      atomicNumber_t Z;
+      infile >> Z;
+      infile >> ws;
+      infile >> parsedElems[Z]->NoValenceOrbitals;
       infile >> ws;
       //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->NoValenceOrbitals << " number of singly occupied valence orbitals." << endl;
@@ -279,6 +299,7 @@
   if (infile != NULL) {
     while (!infile.eof()) {
-      infile >> tmp;
-      ptr = FindElement((int)tmp);
+      atomicNumber_t Z;
+      infile >> Z;
+      ptr = parsedElems[Z];
       infile >> ws;
       infile >> ptr->HBondDistance[0];
@@ -300,6 +321,7 @@
   if (infile != NULL) {
     while (!infile.eof()) {
-      infile >> tmp;
-      ptr = FindElement((int)tmp);
+      atomicNumber_t Z;
+      infile >> Z;
+      ptr = parsedElems[Z];
       infile >> ws;
       infile >> ptr->HBondAngle[0];
@@ -313,6 +335,13 @@
     otherstatus = false;
 
-  if (!otherstatus)
+  if (otherstatus){
+    map<atomicNumber_t,element*>::iterator iter;
+    for(iter=parsedElems.begin();iter!=parsedElems.end();++iter){
+      AddElement((*iter).second);
+    }
+  }
+  else{
     eLog() << Verbose(2) << "Something went wrong while parsing the other databases!" << endl;
+  }
 
   return status;
@@ -334,8 +363,6 @@
     f << header1 << endl;
     f << header2 << endl;
-    element *walker = periodentafel::start;
-    while (walker->next != periodentafel::end) {
-      walker = walker->next;
-      result = result && walker->Output(&f);
+    for(const_iterator iter=elements.begin();iter!=elements.end();++iter){
+         result = result && (*iter).second->Output(&f);
     }
     f.close();
