Added Dictionary to svn

Use pointer to vector instead of copying arrays.



git-svn-id: svn://136.177.114.72/svn_GW/phreeqcpp/trunk@901 1feff8c3-07ed-0310-ac33-dd36852eb9cd
This commit is contained in:
David L Parkhurst 2006-05-04 23:09:32 +00:00
parent 11431ae291
commit 764d31c7ae
3 changed files with 159 additions and 5 deletions

105
Dictionary.cxx Normal file
View File

@ -0,0 +1,105 @@
// StorageBin.cxx: implementation of the cxxStorageBin class.
//
//////////////////////////////////////////////////////////////////////
#ifdef _DEBUG
#pragma warning(disable : 4786) // disable truncation warning (Only used by debugger)
#endif
#include "Dictionary.h" // define first
#define EXTERNAL extern
#include "global.h"
#include "phqalloc.h"
#include "phrqproto.h"
#include "output.h"
#include <cassert> // assert
#include <algorithm> // std::sort
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
cxxDictionary::cxxDictionary()
{
// default constructor for cxxStorageBin
}
cxxDictionary::~cxxDictionary()
{
}
void cxxDictionary::add_phreeqc() {
HashTable *Table = strings_hash_table;
int i,j;
Segment *seg;
Element *p,*q;
if (Table != NULL) {
for (i = 0; i < Table->SegmentCount; i++) {
/* test probably unnecessary */
if ((seg = Table->Directory[i]) != NULL) {
for (j = 0; j < SegmentSize; j++) {
p = seg[j];
while (p != NULL) {
q = p->Next;
//p->Data = (char*) free_check_null((void*)p->Data);
this->putString((char *) (p->Data));
p = q;
}
}
}
}
}
}
int cxxDictionary::putString(std::string str) {
int n;
std::map<std::string, int>::iterator it = stringkey.find(str);
if (it != stringkey.end()) {
n = it->second;
} else {
n = stringkey.size();
stringkey[str] = n;
intkey[n] = str;
}
return(n);
}
int cxxDictionary::putString(char * str) {
std::string str1(str);
return (putString(str1));
}
int cxxDictionary::string2int(std::string str) {
int n;
std::map<std::string, int>::iterator it = stringkey.find(str);
if (it != stringkey.end()) {
n = it->second;
} else {
n = -1;
}
return(n);
}
int cxxDictionary::string2int(char * str) {
if (str == NULL) return (-1);
std::string str1(str);
return(string2int(str1));
}
std::string *cxxDictionary::int2string(int i) {
std::map<int, std::string>::iterator it = intkey.find(i);
if (it != intkey.end()) {
return(&it->second);
} else {
return(NULL);
}
}
char *cxxDictionary::int2char(int i) {
std::map<int, std::string>::iterator it = intkey.find(i);
if (it != intkey.end()) {
return(string_hsave(it->second.c_str()));
} else {
return(NULL);
}
}

46
Dictionary.h Normal file
View File

@ -0,0 +1,46 @@
#if !defined(DICTIONARY_H_INCLUDED)
#define DICTIONARY_H_INCLUDED
#include <cassert> // assert
#include <map> // std::map
#include <string> // std::string
#include <list> // std::list
#include <vector> // std::vector
#include "Solution.h"
#include "NameDouble.h"
class cxxDictionary
{
public:
cxxDictionary();
~cxxDictionary();
void add_phreeqc();
int size() {
return stringkey.size();
}
int putString(std::string str);
int putString(char * str);
int string2int(std::string str);
int string2int(char * str);
std::string *int2string(int i);
char *cxxDictionary::int2char(int i);
protected:
std::map<std::string, int> stringkey;
std::map<int, std::string> intkey;
public:
};
#endif // !defined(DICTIONARY_H_INCLUDED)

View File

@ -682,9 +682,10 @@ void cxxStorageBin::mpi_send(int n, int task_number)
// Convert to arrays
int i = ints.size();
int int_array[i];
//int int_array[i];
int d = doubles.size();
double double_array[d];
//double double_array[d];
/*
for (int j = 0; j < i; j++) {
int_array[j] = ints[j];
//std::cerr << "Sending ints " << j << " value " << ints[j] << std::endl;
@ -693,16 +694,18 @@ void cxxStorageBin::mpi_send(int n, int task_number)
double_array[j] = doubles[j];
//std::cerr << "Sending doubles " << j << " value " << doubles[j] << std::endl;
}
*/
/*
* Send message to processor
*/
int position = 0;
MPI_Send(&max_size, 1, MPI_INT, task_number, 0, MPI_COMM_WORLD);
MPI_Pack(&i, 1, MPI_INT, buffer, max_size, &position, MPI_COMM_WORLD);
MPI_Pack(&int_array, i, MPI_INT, buffer, max_size, &position, MPI_COMM_WORLD);
//MPI_Pack(&int_array, i, MPI_INT, buffer, max_size, &position, MPI_COMM_WORLD);
MPI_Pack(&(ints.front()), i, MPI_INT, buffer, max_size, &position, MPI_COMM_WORLD);
MPI_Pack(&d, 1, MPI_INT, buffer, max_size, &position, MPI_COMM_WORLD);
MPI_Pack(&double_array, d, MPI_DOUBLE, buffer, max_size, &position, MPI_COMM_WORLD);
//MPI_Pack(&double_array, d, MPI_DOUBLE, buffer, max_size, &position, MPI_COMM_WORLD);
MPI_Pack(&(doubles.front()), d, MPI_DOUBLE, buffer, max_size, &position, MPI_COMM_WORLD);
MPI_Send(buffer, position, MPI_PACKED, task_number, 0, MPI_COMM_WORLD);
buffer = (void *) free_check_null(buffer);