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