1 //////////////////////////////////////////////////////////////////////////////
4 // ADLib, Prop and their related set of tools and documentation are in the
5 // public domain. The author(s) of this software reserve no copyrights on
6 // the source code and any code generated using the tools. You are encouraged
7 // to use ADLib and Prop to develop software, in both academic and commercial
8 // settings, and are free to incorporate any part of ADLib and Prop into
11 // Although you are under no obligation to do so, we strongly recommend that
12 // you give away all software developed using our tools.
14 // We also ask that credit be given to us when ADLib and/or Prop are used in
15 // your programs, and that this notice be preserved intact in all the source
18 // This software is still under development and we welcome any suggestions
19 // and help from the users.
23 //////////////////////////////////////////////////////////////////////////////
29 #include <AD/strings/quark.h> // Atomic strings
30 #include <AD/strings/charesc.h> // Escape characters
31 #include <AD/hash/lhash.h> // Hash table
32 #include <AD/memory/strpool.h> // String pools
36 QuarkMap(const QuarkMap
&); // no copy constructor
37 void operator = (const QuarkMap
&); // no assignment
39 // A table of all strings encountered
40 LHashTable
<char *,char> universe
;
41 // A string pool to store the strings
44 inline QuarkMap() : universe(397), pool(8196) {}
47 static QuarkMap
* map
= 0;
49 int Quark::entries() { return map
? map
->universe
.size() : 0; }
51 void Quark::intern(const char * string
)
53 if (map
== 0) map
= new QuarkMap
;
54 Ix key
= map
->universe
.lookup((char * const)string
);
57 // If the string is already in the universe, use the old copy.
58 // Otherwise, allocate a new string and put it into the map.
61 name
= map
->universe
.key(key
);
63 char * newName
= map
->pool
[string
]; // allocate the string from the pool
64 map
->universe
.insert(newName
,' '); // add the new key into the map
69 //////////////////////////////////////////////////////////////////////////////
71 //////////////////////////////////////////////////////////////////////////////
72 Quark::Quark(const char string
[])
74 Quark::Quark(const unsigned char string
[])
75 { intern((const char *)string
); }
78 std::ostrstream
S(buf
,sizeof(buf
));
83 Quark::Quark(unsigned short n
)
85 std::ostrstream
S(buf
,sizeof(buf
));
92 std::ostrstream
S(buf
,sizeof(buf
));
97 Quark::Quark(const char string
[], int n
)
100 S2
<< string
<< n
<< std::ends
;
102 S
.rdbuf()->freeze(0);
104 Quark::Quark(unsigned int n
)
106 std::ostrstream
S(buf
,sizeof(buf
));
107 std::ostream
& S2
= S
;
108 S2
<< n
<< std::ends
;
113 std::ostrstream
S(buf
,sizeof(buf
));
114 std::ostream
& S2
= S
;
115 S2
<< n
<< std::ends
;
118 Quark::Quark(unsigned long n
)
120 std::ostrstream
S(buf
,sizeof(buf
));
121 std::ostream
& S2
= S
;
122 S2
<< n
<< std::ends
;
125 Quark::Quark(double n
)
127 std::ostrstream
S(buf
,sizeof(buf
));
128 std::ostream
& S2
= S
;
129 S2
<< n
<< std::ends
;
135 char * p
= print_char(buf
+1,c
); *p
++ = '\''; *p
= '\0';
138 Quark::Quark (const char s1
[], const char s2
[])
140 std::ostream
& S2
= S
;
141 S2
<< s1
<< s2
<< std::ends
;
143 S
.rdbuf()->freeze(0);
145 Quark::Quark (const char s1
[], const char s2
[], const char s3
[])
147 std::ostream
& S2
= S
;
148 S2
<< s1
<< s2
<< s3
<< std::ends
;
150 S
.rdbuf()->freeze(0);
152 Quark::Quark (const char s1
[], const char s2
[], const char s3
[], const char s4
[])
154 std::ostream
& S2
= S
;
155 S2
<< s1
<< s2
<< s3
<< s4
<< std::ends
;
157 S
.rdbuf()->freeze(0);
160 std::ostream
& operator << (std::ostream
& f
, const Quark
& q
)
161 { return f
<< q
.name
; }
163 std::istream
& operator >> (std::istream
& f
, Quark
& q
)
170 unsigned int hash_nocase(const Quark
& q
)
171 { unsigned int h
= 0;
172 for (const char * p
= q
.name
; *p
; p
++)
173 { if (isupper(*p
)) h
+= h
+ (*p
- 'A') + 'a';
179 int compare_nocase(const Quark
& a
, const Quark
& b
)
180 { return stricmp(a
.string(),b
.string()); }