passing strings by reference

This commit is contained in:
Yaser Afshar
2021-09-03 13:59:23 -05:00
parent aeec0f0e86
commit 257a7fe9ca
2 changed files with 15 additions and 13 deletions

View File

@ -172,7 +172,7 @@ void LabelMap::create_lmap2lmap(LabelMap *lmap2, int mode)
return numeric type return numeric type
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
int LabelMap::find_or_create(std::string mylabel, int LabelMap::find_or_create(const std::string &mylabel,
std::vector<std::string> &labels, int ntypes) std::vector<std::string> &labels, int ntypes)
{ {
for (int i = 0; i < ntypes; i++) for (int i = 0; i < ntypes; i++)
@ -190,8 +190,10 @@ int LabelMap::find_or_create(std::string mylabel,
} }
// if label cannot be found or created, need more space reserved // if label cannot be found or created, need more space reserved
error->all(FLERR,"Topology type exceeds system topology type"); error->all(FLERR,"Topology type exceeds system topology type");
// never reaches here, just to prevent compiler warning
return -1;
} }
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
@ -199,7 +201,7 @@ int LabelMap::find_or_create(std::string mylabel,
return -1 if type not yet defined return -1 if type not yet defined
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
int LabelMap::find(std::string mylabel, int mode) int LabelMap::find(const std::string &mylabel, int mode)
{ {
if (mode == Atom::ATOM) if (mode == Atom::ATOM)
return search(mylabel,typelabel,natomtypes); return search(mylabel,typelabel,natomtypes);
@ -224,8 +226,8 @@ int LabelMap::find(std::string mylabel, int mode)
return -1 if type not yet defined return -1 if type not yet defined
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
int LabelMap::search(std::string mylabel, int LabelMap::search(const std::string &mylabel,
std::vector<std::string> labels, int ntypes) const std::vector<std::string> &labels, int ntypes)
{ {
for (int i = 0; i < ntypes; i++) for (int i = 0; i < ntypes; i++)
if (labels[i] == mylabel) return i+1; if (labels[i] == mylabel) return i+1;
@ -381,7 +383,7 @@ char *LabelMap::read_string(FILE *fp)
byte) into the restart file byte) into the restart file
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
void LabelMap::write_string(std::string str, FILE *fp) void LabelMap::write_string(const std::string &str, FILE *fp)
{ {
const char *cstr = str.c_str(); const char *cstr = str.c_str();
int n = strlen(cstr) + 1; int n = strlen(cstr) + 1;

View File

@ -43,12 +43,12 @@ class LabelMap : protected Pointers {
void allocate_type_labels(); void allocate_type_labels();
void modify_lmap(int, char **); void modify_lmap(int, char **);
void merge_lmap(class LabelMap *, int); // copy another lmap into this one void merge_lmap(class LabelMap *, int); // copy another lmap into this one
void create_lmap2lmap(class LabelMap *, int); // index mapping between two lmaps void create_lmap2lmap(class LabelMap *, int); // index mapping between two lmaps
int find_or_create(std::string, std::vector<std::string> &, int); // look up type or create new type int find_or_create(const std::string &, std::vector<std::string> &, int); // look up type or create new type
int find(std::string, int); // find numeric type of type label int find(const std::string &, int); // find numeric type of type label
int search(std::string, std::vector<std::string>, int); // look up type index int search(const std::string &, const std::vector<std::string> &, int); // look up type index
int is_complete(int); // check if all types are assigned int is_complete(int); // check if all types are assigned
// input/output for atom class label map // input/output for atom class label map
@ -58,7 +58,7 @@ class LabelMap : protected Pointers {
private: private:
char *read_string(FILE *); char *read_string(FILE *);
void write_string(std::string, FILE *); void write_string(const std::string &, FILE *);
int read_int(FILE *); int read_int(FILE *);
}; };