Initial commit from cwc-1.0.1
[cwc.git] / src / symbol.cc
blob6b79ee1f8f16e636a388dc1cb83022ce3252cd24
1 /**
2 * cwc - a crossword compiler. Copyright 1999 Lars Christensen
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17 * 02111-1307, USA.
18 **/
20 #include <iostream.h>
22 #include "symbol.hh"
24 //////////////////////////////////////////////////////////////////////
25 // class symbol
27 char symbol::alphabet[32];
28 symbol symbol::alphindex[256];
30 int symbol::symballoc = 0;
31 symbol symbol::outside;
32 symbol symbol::none;
33 symbol symbol::empty;
35 symbol symbol::symbolbit(symbolset ss) {
36 symbol s;
37 for (symbolset i=1,n=0; i; i<<=1,n++) {
38 if (ss&i)
39 s.symb = n;
41 return s;
44 symbol symbol::alloc(char ch) {
45 symbol s;
46 if (symballoc >= 32) {
47 for (int i=0; i<32;i++) cout << alphabet[i]; cout << endl;
48 cout << ch << ' ' << int(ch) << endl;
49 throw error("Too many symbols");
52 s.symb = symballoc;
53 symballoc++;
54 alphabet[s.symb] = ch;
55 alphindex[(unsigned char)ch].symb = s.symb;
56 return s;
59 void symbol::buildindex() {
60 for (int i=0;i<32;i++)
61 alphabet[i] = UNDEF;
62 for (int i=0;i<256;i++)
63 alphindex[i].symb = UNDEF;
64 symballoc = 0;
65 none = symbol::alloc('/');
66 empty = symbol::alloc('+');
67 outside = symbol::alloc(' ');
70 symbol::symbol(char ch) {
71 symb = alphindex[(unsigned char)ch].symb;
72 if (symb == UNDEF)
73 *this = symbol::alloc(ch);
76 symbolset pickbit(symbolset &ss) {
77 int a[32], n = 0;
78 for (int i=1; i; i<<=1) {
79 if (ss & i)
80 a[n++] = i;
82 if (n==0) return 0;
83 symbolset bit = a[rand()%n];
84 ss &= ~bit;
85 return bit;
88 int wordlen(symbol *st) {
89 int n = 0;
90 while (st[n] != symbol::outside) n++;
91 return n;
94 ostream &operator <<(ostream &os, symbol *s) {
95 while (*s != symbol::outside) {
96 os << *s;
97 s++;
99 return os;
102 int numones(symbolset ss) {
103 int n = 0;
104 for (int i=1; i; i <<= 1) {
105 if (ss&i)
106 n++;
108 return n;
111 int symbol::numalpha() {
112 int n = 0;
113 for (int i=0; i<32; i++)
114 if (isalpha(alphabet[i]))
115 n++;
116 return n;