The Original CHomP Software
words.h
Go to the documentation of this file.
1
3
14
15// Copyright (C) 1997-2020 by Pawel Pilarczyk.
16//
17// This file is part of my research software package. This is free software:
18// you can redistribute it and/or modify it under the terms of the GNU
19// General Public License as published by the Free Software Foundation,
20// either version 3 of the License, or (at your option) any later version.
21//
22// This software is distributed in the hope that it will be useful,
23// but WITHOUT ANY WARRANTY; without even the implied warranty of
24// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25// GNU General Public License for more details.
26//
27// You should have received a copy of the GNU General Public License
28// along with this software; see the file "license.txt". If not,
29// please, see <https://www.gnu.org/licenses/>.
30
31// Started on May 5, 2002. Last revision: February 21, 2007.
32
33
34#ifndef _CHOMP_STRUCT_WORDS_H_
35#define _CHOMP_STRUCT_WORDS_H_
36
37#include "chomp/system/config.h"
41
42#include <cstdlib>
43#include <cstring>
44#include <iostream>
45#include <fstream>
46#include <sstream>
47
48namespace chomp {
49namespace homology {
50
51
52// the data types defined within this file
53class word;
54
57
58
59// --------------------------------------------------
60// ---------------------- word ----------------------
61// --------------------------------------------------
62
64class word
65{
66public:
68 word ();
69
71 word (const char *s);
72
74 word (const word &w);
75
77 ~word ();
78
80 word &operator = (const word &w);
81
83 int length () const;
84
86 const char *text () const;
87
89 operator const char * () const;
90
92 word &operator += (const word &w);
93
95 word operator + (const word &w) const;
96
99 operator int () const;
100
101private:
103 int len;
104
106 char *txt;
107
108}; /* class word */
109
110// --------------------------------------------------
111
112inline word::word ()
113{
114 len = 0;
115 txt = NULL;
116 return;
117} /* word::word */
118
119inline word::word (const char *s)
120{
121 len = s ? strlen (s) : 0;
122 if (!len)
123 txt = NULL;
124 else
125 {
126 txt = new char [len + 1];
127 if (!txt)
128 throw "Not enough memory to create a word.";
129 strcpy (txt, s);
130 }
131 return;
132} /* word::word */
133
134inline word::word (const word &w)
135{
136 len = w. len;
137 if (!len)
138 txt = NULL;
139 else
140 {
141 txt = new char [len + 1];
142 if (!txt)
143 throw "Not enough memory to copy a word.";
144 strcpy (txt, w. txt);
145 }
146 return;
147} /* word::word */
148
149inline word::~word ()
150{
151 if (txt)
152 delete [] txt;
153 return;
154} /* word::~word */
155
156inline word &word::operator = (const word &w)
157{
158 if (txt)
159 delete [] txt;
160 len = w. len;
161 if (w. txt)
162 {
163 txt = new char [len + 1];
164 if (!txt)
165 throw "Not enough memory to copy a word.";
166 strcpy (txt, w. txt);
167 }
168 else
169 txt = NULL;
170 return *this;
171} /* operator = */
172
173inline int word::length () const
174{
175 return len;
176} /* length */
177
178inline const char *word::text () const
179{
180 return txt;
181} /* text */
182
183inline word::operator const char * () const
184{
185 return txt;
186} /* operator int */
187
188inline word &word::operator += (const word &w)
189{
190 if (!len)
191 {
192 *this = w;
193 return *this;
194 }
195 if (!w. len)
196 return *this;
197 int newlen = len + w. len;
198 char *newtxt = new char [newlen + 1];
199 strcpy (newtxt, txt);
200 strcat (newtxt, w. txt);
201 delete [] txt;
202 txt = newtxt;
203 len = newlen;
204 return *this;
205} /* operator += */
206
207inline word word::operator + (const word &w) const
208{
209 word new_w (*this);
210 new_w += w;
211 return new_w;
212} /* operator + */
213
214inline word::operator int () const
215{
216 if (!len)
217 return 0;
218 char *s = txt;
219 if (*s == '+')
220 ++ s;
221 int num;
222 std::istringstream str (s);
223 str >> num;
224 if (!str)
225 return 0;
226 else
227 return num;
228} /* operator int */
229
230// --------------------------------------------------
231
232inline int_t hashkey1 (const word &w)
233{
234 int len = w. length ();
235 if (!len)
236 return 13;
237 const char *txt = w. text ();
238 int_t code = (static_cast<int_t> (txt [0]) << 7) ^
239 (static_cast<int_t> (txt [len - 1]));
240 if (len > 3)
241 code ^= static_cast<int_t> (txt [2]) << 15;
242 return code;
243} /* word::hashkey1 */
244
245inline int_t hashkey2 (const word &w)
246{
247 int len = w. length ();
248 if (!len)
249 return 7;
250 const char *txt = w. text ();
251 int_t code = (static_cast<int_t> (txt [0])) ^
252 (static_cast<int_t> (txt [len - 1] << 17));
253 if (len > 4)
254 code ^= static_cast<int_t> (txt [3]) << 8;
255 return code;
256} /* word::hashkey2 */
257
259inline int operator == (const word &w1, const word &w2)
260{
261 if (w1. length () != w2. length ())
262 return 0;
263 if (!w1. length ())
264 return 1;
265 return !strcmp ((const char *) w1, (const char *) w2);
266} /* operator == */
267
269inline int operator != (const word &w1, const word &w2)
270{
271 return !(w1 == w2);
272} /* operator != */
273
275inline int operator == (const word &w, const char *c)
276{
277 if (!w. length ())
278 return (!c || !*c);
279 return !strcmp ((const char *) w, c);
280} /* operator == */
281
283inline int operator != (const word &w, const char *c)
284{
285 return !(w == c);
286} /* operator != */
287
289inline int operator == (const char *c, const word &w)
290{
291 return (w == c);
292} /* operator == */
293
295inline int operator != (const char *c, const word &w)
296{
297 return (w != c);
298} /* operator != */
299
300// --------------------------------------------------
301
304inline int operator < (const word &w1, const word &w2)
305{
306 const char *c1 = (const char *) w1;
307 const char *c2 = (const char *) w2;
308
309 while (*c1 && *c2 && ((*c1) == (*c2)))
310 {
311 ++ c1;
312 ++ c2;
313 }
314 return ((*c1) < (*c2));
315} /* operator < */
316
319inline int operator > (const word &w1, const word &w2)
320{
321 return (w2 < w1);
322} /* operator > */
323
326inline int operator <= (const word &w1, const word &w2)
327{
328 return !(w1 > w2);
329} /* operator <= */
330
333inline int operator >= (const word &w1, const word &w2)
334{
335 return !(w1 < w2);
336} /* operator >= */
337
338
339// --------------------------------------------------
340
343template <class type>
344word &operator << (word &w, const type &elem)
345{
346 std::ostringstream s;
347 s << elem;
348 w += s. str (). c_str ();
349 return w;
350} /* operator << */
351
352
353// --------------------------------------------------
354
356inline std::ostream &operator << (std::ostream &out, const word &w)
357{
358 if (w. length ())
359 out << (const char *) w;
360 return out;
361} /* operator << */
362
364inline std::istream &operator >> (std::istream &in, word &w)
365{
366 char buf [256 + 1];
367 int pos = 0;
368 int ch = in. peek ();
369 while ((ch != ' ') && (ch != '\r') && (ch != '\n') && (ch != '\t') &&
370 (ch != EOF))
371 {
372 buf [pos ++] = in. get ();
373 ch = in. peek ();
374 if (pos >= 256)
375 break;
376 }
377 buf [pos] = '\0';
378 w = word (buf);
379 return in;
380} /* operator >> */
381
382
383} // namespace homology
384} // namespace chomp
385
386#endif // _CHOMP_STRUCT_WORDS_H_
387
389
This is a template for a set of objects of the given type.
Definition: hashsets.h:185
A word, that is, a string with very few properties.
Definition: words.h:65
const char * text() const
Returns a pointer to the contents of the word.
Definition: words.h:178
word()
Default constructor of an empty word.
Definition: words.h:112
char * txt
A memory buffer containing the word.
Definition: words.h:106
word & operator=(const word &w)
Assignment operator.
Definition: words.h:156
int len
The length of the word (without the terminating zero character).
Definition: words.h:103
word operator+(const word &w) const
Word concatenation operator.
Definition: words.h:207
int length() const
Returns the length of the word (without the ending zero char).
Definition: words.h:173
word & operator+=(const word &w)
Word concatenation operator.
Definition: words.h:188
~word()
Destructor.
Definition: words.h:149
This file contains some precompiler definitions which indicate the operating system and/or compiler u...
int int_t
Index type for indexing arrays, counting cubes, etc.
Definition: config.h:115
This file contains the definition of the container "hashedset" which can be used to represent a set o...
This file defines a class "integer" which represents the ring of integers or the field of integers mo...
hashedset< word > words
The default type of a set of words.
Definition: words.h:56
int operator<=(const word &w1, const word &w2)
Compares two words in an alphabetical way (by ASCII codes).
Definition: words.h:326
std::ostream & operator<<(std::ostream &out, const bincube< Dim, twoPower > &b)
Definition: bincube.h:907
int_t hashkey2(const hashNumber< Number > &n)
The second hashing key.
Definition: bincube.h:1036
int operator>=(const word &w1, const word &w2)
Compares two words in an alphabetical way (by ASCII codes).
Definition: words.h:333
bool operator<(const integer &x, const integer &y)
Definition: integer.h:411
bool operator!=(const typename bincube< Dim, twoPower >::neighborhood_iterator &x1, const typename bincube< Dim, twoPower >::neighborhood_iterator &x2)
Definition: bincube.h:794
std::istream & operator>>(std::istream &in, bincube< Dim, twoPower > &b)
Definition: bincube.h:914
int_t hashkey1(const hashNumber< Number > &n)
The first hashing key.
Definition: bincube.h:1029
bool operator==(const typename bincube< Dim, twoPower >::neighborhood_iterator &x1, const typename bincube< Dim, twoPower >::neighborhood_iterator &x2)
Definition: bincube.h:785
bool operator>(const integer &x, const integer &y)
Definition: integer.h:416
This namespace contains the entire CHomP library interface.
Definition: bitmaps.h:51
This file contains some useful functions related to the text input/output procedures.