SubstTable.C 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #ifndef SubstTable_DEF_INCLUDED
  27. #define SubstTable_DEF_INCLUDED 1
  28. #ifdef SP_NAMESPACE
  29. namespace SP_NAMESPACE {
  30. #endif
  31. template<class T>
  32. SubstTable<T>::SubstTable()
  33. : pairsValid_(1)
  34. {
  35. }
  36. template<class T>
  37. void SubstTable<T>::addSubst(T from, T to)
  38. {
  39. if (table_.size() == 0) {
  40. table_.resize(T(-1) + 1);
  41. for (int i = 0; i < T(-1) + 1; i++)
  42. table_[i] = i;
  43. }
  44. if (table_[from] != to)
  45. pairsValid_ = 0;
  46. table_[from] = to;
  47. }
  48. template<class T>
  49. String<T> SubstTable<T>::inverse(T ch) const
  50. {
  51. if (!pairsValid_) {
  52. const T *p = table_.data();
  53. size_t length = table_.size();
  54. for (size_t i = 0; i < length; i++)
  55. if (p[i] != i) {
  56. // FIXME use mutable if available
  57. ((SubstTable<T> *)this)->pairs_ += T(i);
  58. ((SubstTable<T> *)this)->pairs_ += p[i];
  59. }
  60. ((SubstTable<T> *)this)->pairsValid_ = 1;
  61. }
  62. const T *p = pairs_.data();
  63. if (!p)
  64. return String<T>(&ch, 1);
  65. String<T> result;
  66. if (table_[ch] == ch)
  67. result += ch;
  68. for (size_t n = pairs_.size(); n > 0; n -= 2, p += 2)
  69. if (p[1] == ch)
  70. result += p[0];
  71. return result;
  72. }
  73. template<class T>
  74. void SubstTable<T>::inverseTable(SubstTable<T> &inv) const
  75. {
  76. if (table_.size() == 0) {
  77. inv.table_.resize(0);
  78. inv.pairs_.resize(0);
  79. inv.pairsValid_ = 1;
  80. }
  81. else {
  82. if (inv.table_.size() == 0)
  83. inv.table_.resize(T(-1) + 1);
  84. int i;
  85. for (i = 0; i < T(-1) + 1; i++)
  86. inv.table_[i] = i;
  87. inv.pairs_.resize(0);
  88. inv.pairsValid_ = 0;
  89. for (i = 0; i < T(-1) + 1; i++)
  90. if (table_[i] != i)
  91. inv.table_[table_[i]] = i;
  92. }
  93. }
  94. template<class T>
  95. void SubstTable<T>::subst(String<T> &str) const
  96. {
  97. for (size_t i = 0; i < str.size(); i++)
  98. subst(str[i]);
  99. }
  100. #ifdef SP_NAMESPACE
  101. }
  102. #endif
  103. #endif /* not SubstTable_DEF_INCLUDED */