HPCToolkit
StringTable.hpp
Go to the documentation of this file.
1 // -*-Mode: C++;-*-
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 // This file defines a simple string table to convert between C++
48 // string and long. This saves space when there are many copies of
49 // the same string by storing an index for the string instead of the
50 // actual string itself.
51 //
52 // Notes:
53 // 1. You can test for string equality by testing their indices.
54 //
55 // 2. str2index() inserts the string if not already in the table.
56 //
57 // 3. index2str() returns "invalid-string" if the index is out of
58 // range. We could possibly throw an exception instead.
59 //
60 // 4. This version manages the strings via new and delete. We could
61 // add a custom allocator to store the strings in a common area for
62 // faster bulk deletion.
63 
64 //***************************************************************************
65 
66 #ifndef Support_String_Table_hpp
67 #define Support_String_Table_hpp
68 
69 #include <map>
70 #include <string>
71 #include <vector>
72 
73 namespace HPC {
74 
75 // compare the strings, not the pointers
77 public:
78  bool operator() (const std::string *s1, const std::string *s2)
79  {
80  return *s1 < *s2;
81  }
82 };
83 
84 class StringTable {
85  typedef std::map <const std::string *, long, StringCompare> StringMap;
86  typedef std::vector <const std::string *> StringVec;
87 
88 private:
89  StringMap m_map;
90  StringVec m_vec;
91  std::string m_invalid;
92 
93 public:
95  {
96  m_map.clear();
97  m_vec.clear();
98  m_invalid = "invalid-string";
99  }
100 
101  // delete each string individually
103  {
104  for (long i = 0; i < (long) m_vec.size(); i++) {
105  delete m_vec[i];
106  }
107  }
108 
109  // lookup the string in the map and insert if not there
110  long str2index(const std::string & str)
111  {
112  StringMap::iterator it = m_map.find(&str);
113 
114  if (it != m_map.end()) {
115  return it->second;
116  }
117 
118  // add string to table
119  const std::string *copy = new std::string(str);
120  m_vec.push_back(copy);
121  long index = m_vec.size() - 1;
122  m_map[copy] = index;
123 
124  return index;
125  }
126 
127  const std::string & index2str(long index)
128  {
129  if (index < 0 || index >= (long) m_vec.size()) {
130  return m_invalid;
131  }
132  return *(m_vec[index]);
133  }
134 
135  long size()
136  {
137  return m_vec.size();
138  }
139 
140 }; // class StringTable
141 
142 } // namespace HPC
143 
144 #endif
long str2index(const std::string &str)
std::vector< const std::string * > StringVec
Definition: StringTable.hpp:86
void copy(const char *dst,...)
Definition: FileUtil.cpp:233
std::map< const std::string *, long, StringCompare > StringMap
Definition: StringTable.hpp:85
const std::string & index2str(long index)
bool operator()(const std::string *s1, const std::string *s2)
Definition: StringTable.hpp:78
std::string m_invalid
Definition: StringTable.hpp:91