-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbasic_symbol.h
85 lines (68 loc) · 2.45 KB
/
basic_symbol.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#ifndef _BASIC_SYMBOL_H
#define _BASIC_SYMBOL_H
#include <set>
#include <string>
#include <iostream>
using namespace std;
namespace stx {
template<class Key, class Comp = std::less<Key> >
class basic_symbol {
public:
typedef unsigned long hash_type;
typedef Key key_type;
basic_symbol()
{ k = &(*key_pool().insert(key_type()).first); }
basic_symbol(const key_type& key)
{
k = &(*key_pool().insert(key).first);
}
basic_symbol(const basic_symbol& sym)
: k(sym.k) {}
basic_symbol& operator=(const basic_symbol& sym)
{ k = sym.k; return *this; }
basic_symbol& operator=(const key_type& key)
{
//cout << "\ninserting key: " << key;
k = &(*key_pool().insert(key).first);
//cout << "\nk: " << k;
return *this; }
const key_type& key() const { return *k; }
hash_type hash() const { return hash_type(k); }
private:
typedef std::set<key_type, Comp> pool_allocator;
static pool_allocator& key_pool();
const key_type* k;
};
template<class Key, class Comp>
typename basic_symbol<Key,Comp>::pool_allocator&
basic_symbol<Key,Comp>::key_pool() {
static pool_allocator pool;
return pool;
}
template<class K, class C>
inline bool operator==(const basic_symbol<K,C>& x, const basic_symbol<K,C>& y)
{ return x.hash() == y.hash(); }
template<class K, class C>
inline bool operator!=(const basic_symbol<K,C>& x, const basic_symbol<K,C>& y)
{ return x.hash() != y.hash(); }
template<class K, class C>
inline bool operator<(const basic_symbol<K,C>& x, const basic_symbol<K,C>& y)
{ return x.hash() < y.hash(); }
template<class K, class C>
inline bool operator<=(const basic_symbol<K,C>& x, const basic_symbol<K,C>& y)
{ return x.hash() <= y.hash(); }
template<class K, class C>
inline bool operator>(const basic_symbol<K,C>& x, const basic_symbol<K,C>& y)
{ return x.hash() > y.hash(); }
template<class K, class C>
inline bool operator>=(const basic_symbol<K,C>& x, const basic_symbol<K,C>& y)
{ return x.hash() >= y.hash(); }
template<class K, class C>
inline std::ostream& operator<<(std::ostream &x, const basic_symbol<K,C>& y)
{ return x << y.key(); }
template<class K, class C>
inline std::istream& operator<<(std::istream &x, const basic_symbol<K,C>& y)
{ return y.key() >> x; }
typedef basic_symbol<std::string> string_symbol;
}
#endif