Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/periodentafel.cpp

    ra67d19 r1ac51b  
    1010#include <fstream>
    1111#include <cstring>
     12#include <cassert>
    1213
    1314#include "element.hpp"
     
    1819#include "verbose.hpp"
    1920
     21using namespace std;
     22
    2023/************************************* Functions for class periodentafel ***************************/
    2124
     
    2326 * Initialises start and end of list and resets periodentafel::checkliste to false.
    2427 */
    25 periodentafel::periodentafel() : start(new element), end(new element)
    26 {
    27   start->previous = NULL;
    28   start->next = end;
    29   end->previous = start;
    30   end->next = NULL;
    31 };
     28periodentafel::periodentafel()
     29{};
    3230
    3331/** destructor for class periodentafel
     
    3735{
    3836  CleanupPeriodtable();
    39   delete(end);
    40   delete(start);
    4137};
    4238
     
    4541 * \return true - succeeded, false - does not occur
    4642 */
    47 bool periodentafel::AddElement(element * const pointer)
    48 {
     43periodentafel::iterator periodentafel::AddElement(element * const pointer)
     44{
     45  atomicNumber_t Z = pointer->getNumber();
     46  assert(!elements.count(Z));
    4947  pointer->sort = &pointer->Z;
    50   if (pointer->Z < 1 && pointer->Z >= MAX_ELEMENTS)
    51     DoLog(0) && (Log() << Verbose(0) << "Invalid Z number!\n");
    52   return add(pointer, end);
     48  if (pointer->getNumber() < 1 && pointer->getNumber() >= MAX_ELEMENTS)
     49    DoeLog(0) && (eLog() << Verbose(0) << "Invalid Z number!\n");
     50  pair<iterator,bool> res = elements.insert(pair<atomicNumber_t,element*>(Z,pointer));
     51  return res.first;
    5352};
    5453
     
    5756 * \return true - succeeded, false - element not found
    5857 */
    59 bool periodentafel::RemoveElement(element * const pointer)
    60 {
    61   return remove(pointer, start, end);
     58void periodentafel::RemoveElement(element * const pointer)
     59{
     60  atomicNumber_t Z = pointer->getNumber();
     61  elements.erase(Z);
    6262};
    6363
     
    6565 * \return true - succeeded, false - does not occur
    6666 */
    67 bool periodentafel::CleanupPeriodtable()
    68 {
    69   return cleanup(start,end);
     67void periodentafel::CleanupPeriodtable()
     68{
     69  for(iterator iter=elements.begin();iter!=elements.end();++iter){
     70    delete(*iter).second;
     71  }
     72  elements.clear();
    7073};
    7174
     
    7578 * \return pointer to element or NULL if not found
    7679 */
    77 element * const periodentafel::FindElement(const int Z) const
    78 {
    79   element *walker = find(&Z, start,end);
    80   return(walker);
     80const element * periodentafel::FindElement(atomicNumber_t Z) const
     81{
     82  const_iterator res = elements.find(Z);
     83  return res!=elements.end()?((*res).second):0;
    8184};
    8285
     
    8689 * \return pointer to element
    8790 */
    88 element * const periodentafel::FindElement(const char * const shorthand) const
    89 {
    90   element *walker =  periodentafel::start;
    91   while (walker->next != periodentafel::end) {
    92     walker = walker->next;
    93     if (strncmp(walker->symbol, shorthand, 3) == 0)
    94       return(walker);
    95   }
    96   return (NULL);
     91const element * periodentafel::FindElement(const char * const shorthand) const
     92{
     93  element *res = 0;
     94  for(const_iterator iter=elements.begin();iter!=elements.end();++iter) {
     95    if((*iter).second->getSymbol() == shorthand){
     96      res = (*iter).second;
     97      break;
     98    }
     99  }
     100  return res;
    97101};
    98102
    99103/** Asks for element number and returns pointer to element
    100104 */
    101 element * const periodentafel::AskElement() const
    102 {
    103   element *walker = NULL;
     105const element * periodentafel::AskElement() const
     106{
     107  const element *walker = NULL;
    104108  int Z;
    105109  do {
     
    114118 * \return pointer to either present or newly created element
    115119 */
    116 element * const periodentafel::EnterElement()
    117 {
    118   element *walker = NULL;
    119   int Z = -1;
     120const element * periodentafel::EnterElement()
     121{
     122  const element *res = NULL;
     123  atomicNumber_t Z = 0;
    120124  DoLog(0) && (Log() << Verbose(0) << "Atomic number: " << Z << endl);
    121125  cin >> Z;
    122   walker = FindElement(Z);
    123   if (walker == NULL) {
     126  res = FindElement(Z);
     127  if (!res) {
     128    // TODO: make this using the constructor
     129    element *tmp;
    124130    DoLog(0) && (Log() << Verbose(0) << "Element not found in database, please enter." << endl);
    125     walker = new element;
    126     walker->Z = Z;
     131    tmp = new element;
     132    tmp->Z = Z;
    127133    DoLog(0) && (Log() << Verbose(0) << "Mass: " << endl);
    128     cin >> walker->mass;
     134    cin >> tmp->mass;
    129135    DoLog(0) && (Log() << Verbose(0) << "Name [max 64 chars]: " << endl);
    130     cin >> walker->name;
     136    cin >> tmp->name;
    131137    DoLog(0) && (Log() << Verbose(0) << "Short form [max 3 chars]: " << endl);
    132     cin >> walker->symbol;
    133     periodentafel::AddElement(walker);
    134   }
    135   return(walker);
    136 };
     138    cin >> tmp->symbol;
     139    AddElement(tmp);
     140    res = tmp;
     141  }
     142  return res;
     143};
     144
     145
     146/******************** Access to iterators ****************************/
     147periodentafel::const_iterator periodentafel::begin(){
     148  return elements.begin();
     149}
     150
     151periodentafel::const_iterator periodentafel::end(){
     152  return elements.end();
     153}
     154
     155periodentafel::reverse_iterator periodentafel::rbegin(){
     156  return reverse_iterator(elements.end());
     157}
     158
     159periodentafel::reverse_iterator periodentafel::rend(){
     160  return reverse_iterator(elements.begin());
     161}
    137162
    138163/** Prints period table to given stream.
    139164 * \param output stream
    140165 */
    141 bool periodentafel::Output(ofstream * const output) const
     166bool periodentafel::Output(ostream * const output) const
    142167{
    143168  bool result = true;
    144   element *walker = start;
    145169  if (output != NULL) {
    146     while (walker->next != end) {
    147       walker = walker->next;
    148       result = result && walker->Output(output);
     170    for(const_iterator iter=elements.begin(); iter !=elements.end();++iter){
     171      result = result && (*iter).second->Output(output);
    149172    }
    150173    return result;
     
    157180 * \param *checkliste elements table for this molecule
    158181 */
    159 bool periodentafel::Checkout(ofstream * const output, const int * const checkliste) const
    160 {
    161   element *walker = start;
     182bool periodentafel::Checkout(ostream * const output, const int * const checkliste) const
     183{
    162184  bool result = true;
    163185  int No = 1;
     
    166188    *output << "# Ion type data (PP = PseudoPotential, Z = atomic number)" << endl;
    167189    *output << "#Ion_TypeNr.\tAmount\tZ\tRGauss\tL_Max(PP)L_Loc(PP)IonMass\t# chemical name, symbol" << endl;
    168     while (walker->next != end) {
    169       walker = walker->next;
    170       if ((walker != NULL) && (walker->Z > 0) && (walker->Z < MAX_ELEMENTS) && (checkliste[walker->Z])) {
    171         walker->No = No;
    172         result = result && walker->Checkout(output, No++, checkliste[walker->Z]);
     190    for(const_iterator iter=elements.begin(); iter!=elements.end();++iter){
     191      if (((*iter).first < MAX_ELEMENTS) && (checkliste[(*iter).first])) {
     192        (*iter).second->No = No;
     193        result = result && (*iter).second->Checkout(output, No++, checkliste[(*iter).first]);
    173194      }
    174195    }
     
    184205{
    185206  ifstream infile;
    186   double tmp;
    187207  element *ptr;
     208  map<atomicNumber_t,element*> parsedElems;
    188209  bool status = true;
    189210  bool otherstatus = true;
     
    191212
    192213  // fill elements DB
    193   strncpy(filename, path, MAXSTRINGSIZE);
    194   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
    195   strncat(filename, STANDARDELEMENTSDB, MAXSTRINGSIZE-strlen(filename));
     214  snprintf(filename,MAXSTRINGSIZE,"%s/%s",path,STANDARDELEMENTSDB);
    196215  infile.open(filename);
    197216  if (infile != NULL) {
     
    223242      //neues->Output((ofstream *)&cout);
    224243      if ((neues->Z > 0) && (neues->Z < MAX_ELEMENTS))
    225         periodentafel::AddElement(neues);
     244        parsedElems[neues->getNumber()] = neues;
    226245      else {
    227246        DoLog(0) && (Log() << Verbose(0) << "Could not parse element: ");
     
    237256
    238257  // fill valence DB per element
    239   strncpy(filename, path, MAXSTRINGSIZE);
    240   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
    241   strncat(filename, STANDARDVALENCEDB, MAXSTRINGSIZE-strlen(filename));
    242   infile.open(filename);
    243   if (infile != NULL) {
    244     while (!infile.eof()) {
    245       infile >> tmp;
    246       infile >> ws;
    247       infile >> FindElement((int)tmp)->Valence;
     258  snprintf(filename,MAXSTRINGSIZE,"%s/%s",path,STANDARDVALENCEDB);
     259  infile.open(filename);
     260  if (infile != NULL) {
     261    while (!infile.eof()) {
     262      atomicNumber_t Z;
     263      infile >> Z;
     264      infile >> ws;
     265      infile >> parsedElems[Z]->Valence;
    248266      infile >> ws;
    249267      //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->Valence << " valence electrons." << endl;
     
    255273
    256274  // fill valence DB per element
    257   strncpy(filename, path, MAXSTRINGSIZE);
    258   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
    259   strncat(filename, STANDARDORBITALDB, MAXSTRINGSIZE-strlen(filename));
    260   infile.open(filename);
    261   if (infile != NULL) {
    262     while (!infile.eof()) {
    263       infile >> tmp;
    264       infile >> ws;
    265       infile >> FindElement((int)tmp)->NoValenceOrbitals;
     275  snprintf(filename,MAXSTRINGSIZE,"%s/%s",path,STANDARDORBITALDB);
     276  infile.open(filename);
     277  if (infile != NULL) {
     278    while (!infile.eof()) {
     279      atomicNumber_t Z;
     280      infile >> Z;
     281      infile >> ws;
     282      infile >> parsedElems[Z]->NoValenceOrbitals;
    266283      infile >> ws;
    267284      //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->NoValenceOrbitals << " number of singly occupied valence orbitals." << endl;
     
    273290
    274291  // fill H-BondDistance DB per element
    275   strncpy(filename, path, MAXSTRINGSIZE);
    276   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
    277   strncat(filename, STANDARDHBONDDISTANCEDB, MAXSTRINGSIZE-strlen(filename));
    278   infile.open(filename);
    279   if (infile != NULL) {
    280     while (!infile.eof()) {
    281       infile >> tmp;
    282       ptr = FindElement((int)tmp);
     292  snprintf(filename,MAXSTRINGSIZE,"%s/%s",path,STANDARDHBONDDISTANCEDB);
     293  infile.open(filename);
     294  if (infile != NULL) {
     295    while (!infile.eof()) {
     296      atomicNumber_t Z;
     297      infile >> Z;
     298      ptr = parsedElems[Z];
    283299      infile >> ws;
    284300      infile >> ptr->HBondDistance[0];
     
    294310
    295311  // fill H-BondAngle DB per element
    296   strncpy(filename, path, MAXSTRINGSIZE);
    297   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
    298   strncat(filename, STANDARDHBONDANGLEDB, MAXSTRINGSIZE-strlen(filename));
    299   infile.open(filename);
    300   if (infile != NULL) {
    301     while (!infile.eof()) {
    302       infile >> tmp;
    303       ptr = FindElement((int)tmp);
     312  snprintf(filename,MAXSTRINGSIZE,"%s/%s",path,STANDARDHBONDANGLEDB);
     313  infile.open(filename);
     314  if (infile != NULL) {
     315    while (!infile.eof()) {
     316      atomicNumber_t Z;
     317      infile >> Z;
     318      ptr = parsedElems[Z];
    304319      infile >> ws;
    305320      infile >> ptr->HBondAngle[0];
     
    313328    otherstatus = false;
    314329
    315   if (!otherstatus)
     330  if (otherstatus){
     331    map<atomicNumber_t,element*>::iterator iter;
     332    for(iter=parsedElems.begin();iter!=parsedElems.end();++iter){
     333      AddElement((*iter).second);
     334    }
     335  }
     336  else{
    316337    DoeLog(2) && (eLog()<< Verbose(2) << "Something went wrong while parsing the other databases!" << endl);
     338    map<atomicNumber_t,element*>::iterator iter;
     339    for(iter=parsedElems.begin();iter!=parsedElems.end();++iter){
     340      AddElement((*iter).second);
     341    }
     342  }
    317343
    318344  return status;
     
    327353  char filename[MAXSTRINGSIZE];
    328354
    329   strncpy(filename, path, MAXSTRINGSIZE);
    330   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
    331   strncat(filename, STANDARDELEMENTSDB, MAXSTRINGSIZE-strlen(filename));
     355  snprintf(filename,MAXSTRINGSIZE,"%s/%s",path,STANDARDELEMENTSDB);
    332356  f.open(filename);
    333357  if (f != NULL) {
    334358    f << header1 << endl;
    335359    f << header2 << endl;
    336     element *walker = periodentafel::start;
    337     while (walker->next != periodentafel::end) {
    338       walker = walker->next;
    339       result = result && walker->Output(&f);
     360    for(const_iterator iter=elements.begin();iter!=elements.end();++iter){
     361         result = result && (*iter).second->Output(&f);
    340362    }
    341363    f.close();
Note: See TracChangeset for help on using the changeset viewer.