rpc.cmsd: use TIRPC on Linux
[oweals/cde.git] / cde / programs / nsgmls / SubstTable.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: SubstTable.C /main/1 1996/07/29 17:05:48 cde-hp $ */
24 // Copyright (c) 1994 James Clark
25 // See the file COPYING for copying permission.
26
27 #ifndef SubstTable_DEF_INCLUDED
28 #define SubstTable_DEF_INCLUDED 1
29
30 #ifdef SP_NAMESPACE
31 namespace SP_NAMESPACE {
32 #endif
33
34 template<class T>
35 SubstTable<T>::SubstTable()
36 : pairsValid_(1)
37 {
38 }
39
40 template<class T>
41 void SubstTable<T>::addSubst(T from, T to)
42 {
43   if (table_.size() == 0) {
44     table_.resize(T(-1) + 1);
45     for (int i = 0; i < T(-1) + 1; i++)
46       table_[i] = i;
47   }
48   if (table_[from] != to)
49     pairsValid_ = 0;
50   table_[from] = to;
51 }
52
53 template<class T>
54 String<T> SubstTable<T>::inverse(T ch) const
55 {
56   if (!pairsValid_) {
57     const T *p = table_.data();
58     size_t length = table_.size();
59     for (size_t i = 0; i < length; i++)
60       if (p[i] != i) {
61         // FIXME use mutable if available
62         ((SubstTable<T> *)this)->pairs_ += T(i);
63         ((SubstTable<T> *)this)->pairs_ += p[i];
64       }
65     ((SubstTable<T> *)this)->pairsValid_ = 1;
66   }
67   const T *p = pairs_.data();
68   if (!p)
69     return String<T>(&ch, 1);
70   String<T> result;
71   if (table_[ch] == ch)
72     result += ch;
73   for (size_t n = pairs_.size(); n > 0; n -= 2, p += 2)
74     if (p[1] == ch)
75       result += p[0];
76   return result;
77 }
78
79 template<class T>
80 void SubstTable<T>::inverseTable(SubstTable<T> &inv) const
81 {
82   if (table_.size() == 0) {
83     inv.table_.resize(0);
84     inv.pairs_.resize(0);
85     inv.pairsValid_ = 1;
86   }
87   else {
88     if (inv.table_.size() == 0)
89       inv.table_.resize(T(-1) + 1);
90     int i;
91     for (i = 0; i < T(-1) + 1; i++)
92       inv.table_[i] = i;
93     inv.pairs_.resize(0);
94     inv.pairsValid_ = 0;
95     for (i = 0; i < T(-1) + 1; i++)
96       if (table_[i] != i)
97         inv.table_[table_[i]] = i;
98   }
99 }
100
101 template<class T>
102 void SubstTable<T>::subst(String<T> &str) const
103 {
104   for (size_t i = 0; i < str.size(); i++)
105     subst(str[i]);
106 }
107
108 #ifdef SP_NAMESPACE
109 }
110 #endif
111
112 #endif /* not SubstTable_DEF_INCLUDED */