source: src/dgp/symbol.hh

Last change on this file was e7de6cc, checked in by Tomasz Obrebski <to@…>, 13 years ago

new version of dgp
added dgc, tre and compdic components
compiledic renamed to compdic_utf8
./configure updated

  • Property mode set to 100644
File size: 3.0 KB
Line 
1#ifndef _SYMBOL_HH
2#define _SYMBOL_HH
3
4#include <ext/hash_map>
5#include <string>
6#include <cstring>
7#include <fstream>
8#include <vector>
9#include <iostream>
10
11using namespace std;
12
13using __gnu_cxx::hash_map;
14using __gnu_cxx::hash;
15
16
17// Key comparison for the cstr_hash hash table
18struct eqstr
19{ 
20  bool operator()(const char * s, const char* t) const 
21  { return strcmp(s,t)==0; }
22};
23
24
25// Hash table for storing symbols
26
27typedef hash_map<const char*,int,hash<const char*>,eqstr> cstr_hash;
28
29// Symbol table. Provides access to symbols through their index or name.
30
31class Symbols
32{
33 public:
34
35  Symbols() { add("NULL"); };
36  ~Symbols();
37 
38  void load(const char* filename);
39
40  int operator[](const char* s) { return hash[s]; };
41 
42  const char* operator[](int i) { return table[i]; };
43
44  void add(const char* c);
45
46  int count() { return table.size(); };
47
48 private:
49   
50  std::vector<const char*> table;
51  cstr_hash hash;
52
53};
54
55//////////////////////////////////////////////////////////////////////
56
57/// Symbol class template.
58/** The template argument determines the symbol space.
59    Each space is created with symbol "NULL" with index 0 already in.
60*/
61
62template <int space>
63class Symbol
64{
65 public:
66
67  /// Load the contents of the symbol table from file.
68  static void define(const char *filename) 
69  { defs.load(filename); }
70 
71  /// Add symbol s.
72  /** The string is duplicated.
73   */
74  static Symbol<space> add(const char* s) { defs.add(s); }
75     
76  /// Number of symbols.
77  static int count() { return defs.count(); };
78
79  /// First symbol.
80  static int first() { return 1; }
81
82  /// Last symbol.
83  static int last() { return defs.count()+1; }
84
85  /// Last symbol.
86  static int index(const char* s) { return defs[s]; }
87
88  /// Just for tests.
89  static void print();
90
91  /// 0-argument constructor, default value is 0 ("NULL").
92  Symbol() : val(0) {};
93
94  /// Constructing a symbol from its index.
95  /** No check is performed.
96  */
97
98  Symbol(int v) : val(v) {};
99 
100  /// Constructing a symbol from its name (string to Symbol conversion).
101  /** If s is not a symbol name, the value of 0 ("NULL") is assigned.
102  */
103
104  Symbol(const char * s) : val(defs[s]) {};
105
106  Symbol(string s) : val(defs[(char*)s]) {};
107
108
109  bool empty() const { return val==0; }
110
111  /// Symbol to char* conversion. If symbol is invalid, NULL is returned.
112  const char* str() const { return (val>=0 && val<count())?defs[val]:NULL; };
113
114  /// Symbol to int& conversion.
115  /** Provides a way to iterate through symbols, eg:
116   *  for(Symbol<0> s=1; s; s++ ) ...
117      s=0; while(++s) ...
118   */
119
120  (operator int)() const { return val; };
121
122  Symbol operator++() {val++; return *this;}
123
124  //  bool operator<(Symbol& s) { return val < s.val; }
125
126
127 private:
128  static Symbols defs;
129  int val;
130};
131
132template <int space>
133void Symbol<space>::print()
134{
135  for(Symbol i=0; i<count(); ++i)
136    cout << (int)i << ": " << (const char*)i << endl;
137}
138
139template<int space>
140Symbols Symbol<space>::defs;
141
142template<int space>
143bool operator<(const Symbol<space>& s, const Symbol<space>& t)
144{
145  return (int)s < (int)t;
146}
147
148#endif
Note: See TracBrowser for help on using the repository browser.