source: app/src/dgp/symbol.hh @ 9b57c4d

help
Last change on this file since 9b57c4d was 0214596, checked in by pawelk <pawelk@…>, 17 years ago

Dodalismy do pakietu utt komponent dgp (brak configow i innych bajerow).

git-svn-id: svn://atos.wmid.amu.edu.pl/utt@24 e293616e-ec6a-49c2-aa92-f4a8b91c5d16

  • 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 <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 indexed 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 to char* conversion. If symbol is invalid, NULL is returned.
107  const char* str() const { return (val>=0 && val<count())?defs[val]:NULL; };
108
109  /// Symbol to int& conversion.
110  /** Provides a way to iterate through symbols, eg:
111   *  for(Symbol<0> s=1; s; s++ ) ...
112      s=0; while(++s) ...
113   */
114  (operator int)() const { return val; };
115
116  Symbol operator++() {val++; return *this;}
117
118  //  bool operator<(Symbol& s) { return val < s.val; }
119
120
121 private:
122  static Symbols defs;
123  int val;
124};
125
126template <int space>
127void Symbol<space>::print()
128{
129  for(Symbol i=0; i<count(); ++i)
130    cout << (int)i << ": " << (const char*)i << endl;
131}
132
133template<int space>
134Symbols Symbol<space>::defs;
135
136template<int space>
137bool operator<(const Symbol<space>& s, const Symbol<space>& t)
138{
139  return (int)s < (int)t;
140}
141
142#endif
Note: See TracBrowser for help on using the repository browser.