-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsli_name.h
167 lines (139 loc) · 4.08 KB
/
sli_name.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#ifndef SLI_NAME_H
#define SLI_NAME_H
/*
* name.h
*
* This file is part of NEST
*
* Copyright (C) 2004-2007 by
* The NEST Initiative
*
* See the file AUTHORS for details.
*
* Permission is granted to compile and modify
* this file for non-commercial use.
* See the file LICENSE for details.
*
*/
#include <cassert>
#include <map>
#include <string>
#include <deque>
#include <iostream>
/**
* Represent strings by ints to facilitate fast comparison.
*
* Each Name object represents a string by a unique integer number.
* Comparing Name objects instead of comparing strings directly,
* reduces the complexity of string comparison to that of int comparison.
*
* Each Name object contains a Handle to the string it represents. Strings are
* mapped to Handles via an associative array. Handles are stored in a table,
* and each Handle contains its own index into this table as unique ID, as
* well as the string represented. Fast comparison of Name objects is achieved
* by comparing the indices stored in the handles. Reference counting
* permits deletion of unused Handles.
*
* @note Any string read by the interpreter should be converted to a Name
* at once.
* @note class Name maintains two static lookup tables and is thus not
* thread-safe.
*
*/
namespace sli3
{
class Name
{
public:
typedef unsigned int handle_t;
/**
* Create Name without value.
*/
Name() : handle_(0) {}
Name(long h) : handle_(h)
{}
Name(const char s[]) : handle_(insert(std::string(s))) {}
Name(const std::string &s) : handle_(insert(s)) {}
Name(const Name &n) : handle_(n.handle_) {}
operator int() const
{
return handle_;
}
/**
* Return string represented by Name.
*/
const std::string& toString(void) const;
/**
* Return table index for Name object.
*/
handle_t toIndex(void) const
{
return handle_;
}
bool operator== (const Name &n) const
{
return handle_ == n.handle_;
}
bool operator!= (const Name &n) const
{
return !(handle_ == n.handle_);
}
/**
* Non-alphabetic ordering of names.
* Entering Name's into dictionaries requires ordering. Ordering based
* on string comparison would be very slow. We thus compare based on
* table indices.
*/
bool operator< (const Name &n) const
{
return handle_ < n.handle_;
}
static bool lookup(const std::string &s)
{
HandleMap_ &table=handleMapInstance_();
return (table.find(s) != table.end());
}
static
size_t capacity();
static
size_t num_handles();
void print_handle(std::ostream&) const;
static void list_handles(std::ostream &);
static void list(std::ostream &);
static void info(std::ostream &);
private:
handle_t insert(const std::string&);
/**
* Datatype for map from strings to handles.
*/
typedef std::map<std::string, handle_t> HandleMap_;
typedef std::deque<std::string> HandleTable_;
/**
* Function returning a reference to the single map instance.
* Implementation akin to Meyers Singleton, see Alexandrescu, ch 6.4.
*/
static HandleMap_& handleMapInstance_();
static HandleTable_& handleTableInstance_();
/**
* Handle for the name represented by the Name object.
*/
handle_t handle_;
};
std::ostream& operator<<(std::ostream&, const Name&);
inline
Name::HandleTable_& Name::handleTableInstance_()
{
// Meyers singleton, created first time function is invoked.
static HandleTable_ handleTable(1,"0");
return handleTable;
}
inline
Name::HandleMap_& Name::handleMapInstance_()
{
// Meyers singleton, created first time function is invoked.
static HandleMap_ handleMap;
handleTableInstance_();
return handleMap;
}
} // end of namespace
#endif