The HexaTriDecimal Numbering System
From WikiOfCode
Code
1 public class HexaTriDecimalType {
2 #region << Constants >>
3 const int DEF_WIDTH = 10;
4 #endregion
5
6 #region << Members >>
7 int _width = DEF_WIDTH;
8 char[] _places;
9 #endregion
10
11 #region << Properties >>
12 public int Width {
13 get {
14 return _width;
15 }
16 }
17 #endregion
18
19 #region << Constructors >>
20 public HexaTriDecimalType () {
21 _places = new char[_width];
22 for (int i = 0; i < _width; ++i) {
23 _places[i] = ‘0′;
24 }
25 }
26
27 public HexaTriDecimalType (int width) {
28 _width = width;
29 _places = new char[_width];
30 for (int i = 0; i < _width; ++i) {
31 _places[i] = ‘0′;
32 }
33 }
34
35 public HexaTriDecimalType (HexaTriDecimalType a) {
36 _width = a._width;
37 _places = new char[_width];
38 for (int i = 0; i < _width; ++i) {
39 _places[i] = a._places[i];
40 }
41 }
42
43 public HexaTriDecimalType (string numberAsString, int width) {
44 _width = width;
45 _places = new char[_width];
46 for (int i = 0; i < _width; ++i) {
47 _places[i] = ‘0′;
48 }
49
50 Parse (numberAsString);
51 }
52
53 public HexaTriDecimalType (string numberAsString) {
54 _places = new char[_width];
55 for (int i = 0; i < _width; ++i) {
56 _places[i] = ‘0′;
57 }
58
59 Parse (numberAsString);
60 }
61 #endregion
62
63 #region << External Utils >>
64 public static string GetNextNumber (string number) {
65 foreach (char ch in number) {
66 if (!IsLegalChar (ch)) {
67 throw new Exception ("Illegal characters in string.");
68 }
69 }
70
71 string numberAsString = ConvertToUpperCase (number);
72 HexaTriDecimalType htd = null;
73 try {
74 htd = new HexaTriDecimalType (numberAsString, numberAsString.Length);
75 htd++;
76 } catch (Exception) {
77 htd = new HexaTriDecimalType (numberAsString, numberAsString.Length + 1);
78 htd++;
79 }
80
81 return htd.ToString ();
82 }
83 #endregion
84
85 #region << Internal Utils >>
86 private void Parse (string numberAsString) {
87 if (numberAsString.Length < _width) {
88 throw new Exception ("Width exceeding size.");
89 }
90
91 foreach (char ch in numberAsString) {
92 if (!IsLegalChar (ch)) {
93 throw new Exception ("Illegal characters in string.");
94 }
95 }
96
97 string num = ConvertToUpperCase (numberAsString);
98
99 for (int i = _places.Length-1; i >= _places.Length-num.Length; –i) {
100 _places[i] = num[i-(_places.Length-num.Length)];
101 }
102 }
103
104 private static string StripOffInsignificantChars (string number) {
105 int sigIndex = 0;
106 for (int i = 0; i < number.Length; ++i) {
107 char ch = number[i];
108 if (ch == ‘0′) {
109 ++sigIndex;
110 } else {
111 break;
112 }
113 }
114
115 return number.Substring (sigIndex, number.Length - sigIndex);
116 }
117
118 private static char[] StripOffInsignificantChars (char[] array) {
119 int sigIndex = 0;
120 for (int i = 0; i < array.Length; ++i) {
121 if (array[i] == ‘0′) {
122 ++sigIndex;
123 } else {
124 break;
125 }
126 }
127
128 char[] opArray = new char[array.Length - sigIndex];
129 int index = 0;
130 for (int i = sigIndex; i < array.Length; ++i) {
131 opArray[index++] = array[i];
132 }
133 return opArray;
134 }
135
136 private static bool IsLegalChar (char ch) {
137 if ((ch >= ‘0′ && ch <= ‘9′) || (ch >= ‘A’ && ch <= ‘Z’) || (ch >= ‘a’ && ch <= ‘z’)) {
138 return true;
139 }
140
141 return false;
142 }
143
144 private static string ConvertToUpperCase (string numberAsString) {
145 string opStr = "";
146
147 for (int i = 0; i < numberAsString.Length; ++i) {
148 char ch = numberAsString[i];
149 if (ch >= ‘a’ && ch <= ‘z’) {
150 ch -= (char) 32;
151 }
152 opStr += ch;
153 }
154
155 return opStr;
156 }
157 #endregion
158
159 #region << Increment Operation >>
160 public static HexaTriDecimalType operator ++ (HexaTriDecimalType a) {
161 a.IncrementByOne ();
162 return a;
163 }
164
165 private void IncrementByOne () {
166 IncrementByOne (_width - 1);
167 }
168
169 private void IncrementByOne (int place) {
170 if (place < 0) {
171 throw new Exception ("Maximum value reached.");
172 }
173 char ch = _places[place];
174 if ((ch >= ‘0′ && ch <= ‘8′) || (ch >= ‘A’ && ch <= ‘Y’)) {
175 _places[place] = ++ch;
176 } else if (ch == ‘9′) {
177 _places[place] = ‘A’;
178 } else if (ch == ‘Z’) {
179 _places[place] = ‘0′;
180 IncrementByOne (place - 1);
181 }
182 }
183 #endregion
184
185 #region << Decrement Operation >>
186 public static HexaTriDecimalType operator – (HexaTriDecimalType a) {
187 a.DecrementByOne ();
188 return a;
189 }
190
191 private void DecrementByOne () {
192 DecrementByOne (_width - 1);
193 }
194
195 private void DecrementByOne (int place) {
196 if (place < 0) {
197 throw new Exception ("Minimum value reached.");
198 }
199 char ch = _places[place];
200 if ((ch >= ‘1′ && ch <= ‘9′) || (ch >= ‘B’ && ch <= ‘Z’)) {
201 _places[place] = –ch;
202 } else if (ch == ‘A’) {
203 _places[place] = ‘9′;
204 } else if (ch == ‘0′) {
205 _places[place] = ‘Z’;
206 DecrementByOne (place - 1);
207 }
208 }
209 #endregion
210
211 #region << Comparison Operations >>
212 public static bool operator == (HexaTriDecimalType lhs, HexaTriDecimalType rhs) {
213 if (lhs.Width != rhs.Width) {
214 return false;
215 }
216
217 for (int i = 0; i < lhs.Width; ++i) {
218 if (lhs._places[i] != rhs._places[i]) {
219 return false;
220 }
221 }
222
223 return true;
224 }
225
226 public static bool operator == (HexaTriDecimalType lhs, string rhs) {
227 string sigRhs = ConvertToUpperCase (StripOffInsignificantChars (rhs));
228
229 char[] rhsPlaces = sigRhs.ToCharArray ();
230 char[] lhsPlaces = StripOffInsignificantChars (lhs._places);
231
232 if (lhsPlaces.Length != rhsPlaces.Length) {
233 return false;
234 }
235
236 if (lhsPlaces.Length == rhsPlaces.Length) {
237 for (int i = 0; i < lhsPlaces.Length; ++i) {
238 if (lhsPlaces[i] != rhsPlaces[i]) {
239 return false;
240 }
241 }
242 }
243
244 return true;
245 }
246
247 public static bool operator != (HexaTriDecimalType lhs, HexaTriDecimalType rhs) {
248 return !(lhs == rhs);
249 }
250
251 public static bool operator != (HexaTriDecimalType lhs, string rhs) {
252 return !(lhs == rhs);
253 }
254 #endregion
255
256 #region << Overrides >>
257 public override string ToString () {
258 string opStr = "";
259
260 for (int i = 0; i < _width; ++i) {
261 opStr += _places[i];
262 }
263
264 return opStr;
265 }
266
267 public override int GetHashCode () {
268 return base.GetHashCode ();
269 }
270
271 public override bool Equals (object obj) {
272 return base.Equals (obj);
273 }
274 #endregion
275 }

