SHOGUN  v3.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DynInt.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2009 Soeren Sonnenburg
8  * Copyright (C) 2009 Fraunhofer Institute FIRST and Max Planck Society
9  */
10 
11 #ifndef __DYNINT_H__
12 #define __DYNINT_H__
13 
14 #include <shogun/lib/common.h>
15 #include <shogun/io/SGIO.h>
17 
18 namespace shogun
19 {
36 template <class T, int sz> class CDynInt
37 {
38 public:
44  {
45  for (int i=0; i<sz; i++)
46  integer[i]=0;
47  }
48 
55  CDynInt(uint8_t x)
56  {
57  for (int i=0; i<sz-1; i++)
58  integer[i]=0;
59  integer[sz-1]= (T) x;
60  }
61 
68  CDynInt(uint16_t x)
69  {
70  for (int i=0; i<sz-1; i++)
71  integer[i]=0;
72  integer[sz-1]= (T) x;
73  }
74 
81  CDynInt(uint32_t x)
82  {
83  for (int i=0; i<sz-1; i++)
84  integer[i]=0;
85  integer[sz-1]= (T) x;
86  }
87 
94  CDynInt(int32_t x)
95  {
96  for (int i=0; i<sz-1; i++)
97  integer[i]=0;
98  integer[sz-1]= (T) x;
99  }
100 
107  CDynInt(int64_t x)
108  {
109  for (int i=0; i<sz-1; i++)
110  integer[i]=0;
111  integer[sz-1]=(T) x;
112  }
113 
120  CDynInt(uint64_t x)
121  {
122  for (int i=0; i<sz-1; i++)
123  integer[i]=0;
124  integer[sz-1]=(T) x;
125  }
126 
133  CDynInt(const T x[sz])
134  {
135  for (int i=0; i<sz; i++)
136  integer[i]=x[i];
137  }
138 
141  {
142  for (int i=0; i<sz; i++)
143  integer[i]=x.integer[i];
144  }
145 
148  {
149  }
150 
155  {
156  for (int i=0; i<sz; i++)
157  integer[i]=x.integer[i];
158  return *this;
159  }
160 
165  const CDynInt<T,sz> operator|(const CDynInt<T,sz>& x) const
166  {
167  CDynInt<T,sz> r;
168 
169  for (int i=sz-1; i>=0; i--)
170  r.integer[i]=integer[i] | x.integer[i];
171 
172  return r;
173  }
174 
179  const CDynInt<T,sz> operator&(const CDynInt<T,sz>& x) const
180  {
181  CDynInt<T,sz> r;
182 
183  for (int i=sz-1; i>=0; i--)
184  r.integer[i]=integer[i] & x.integer[i];
185 
186  return r;
187  }
188 
196  {
197  CDynInt<T,sz> r=*this;
198 
199  while (shift>0)
200  {
201  int s=CMath::min(shift, 8*((int) sizeof(T))-1);
202 
203  for (int i=0; i<sz; i++)
204  {
205  T overflow=0;
206  if (i<sz-1)
207  overflow = r.integer[i+1] >> (sizeof(T)*8 - s);
208  r.integer[i]= (r.integer[i] << s) | overflow;
209  }
210 
211  shift-=s;
212  }
213 
214  return r;
215  }
216 
224  {
225  CDynInt<T,sz> r=*this;
226 
227  while (shift>0)
228  {
229  int s=CMath::min(shift, 8*((int) sizeof(T))-1);
230 
231  for (int i=sz-1; i>=0; i--)
232  {
233  T overflow=0;
234  if (i>0)
235  overflow = (r.integer[i-1] << (sizeof(T)*8 - s));
236  r.integer[i]= (r.integer[i] >> s) | overflow;
237  }
238 
239  shift-=s;
240  }
241 
242  return r;
243  }
244 
249  const CDynInt<T,sz> operator^(const CDynInt<T,sz>& x) const
250  {
251  CDynInt<T,sz> r;
252 
253  for (int i=sz-1; i>=0; i--)
254  r.integer[i]=integer[i] ^ x.integer[i];
255 
256  return r;
257  }
258 
263  const CDynInt<T,sz> operator+(const CDynInt<T,sz> &x) const
264  {
265  CDynInt<T,sz> r;
266 
267  T overflow=0;
268  for (int i=sz-1; i>=0; i--)
269  {
270  r.integer[i]=integer[i]+x.integer[i]+overflow;
271  if (r.integer[i] < CMath::max(integer[i], x.integer[i]))
272  overflow=1;
273  else
274  overflow=0;
275  }
276 
277  return x;
278  }
279 
284  const CDynInt<T,sz> operator-(const CDynInt<T,sz> &x) const
285  {
286  return NULL;
287  }
288 
293  const CDynInt<T,sz> operator/(const CDynInt<T,sz> &x) const
294  {
295  return NULL;
296  }
297 
302  const CDynInt<T,sz> operator*(const CDynInt<T,sz> &x) const
303  {
304  return NULL;
305  }
306 
312  {
313  return NULL;
314  }
315 
321  {
322  return NULL;
323  }
324 
330  {
331  return NULL;
332  }
333 
339  {
340  return NULL;
341  }
342 
347  bool operator==(const CDynInt<T,sz> &x) const
348  {
349  for (int i=sz-1; i>=0; i--)
350  {
351  if (integer[i]!=x.integer[i])
352  return false;
353  }
354 
355  return true;
356  }
357 
362  bool operator>=(const CDynInt<T,sz> &x) const
363  {
364  for (int i=0; i<sz; i++)
365  {
366  if (integer[i]>x.integer[i])
367  return true;
368  if (integer[i]<x.integer[i])
369  return false;
370  }
371  return true;
372  }
373 
378  bool operator<=(const CDynInt<T,sz> &x) const
379  {
380  for (int i=0; i<sz; i++)
381  {
382  if (integer[i]<x.integer[i])
383  return true;
384  if (integer[i]>x.integer[i])
385  return false;
386  }
387  return true;
388  }
389 
394  bool operator>(const CDynInt<T,sz> &x) const
395  {
396  for (int i=0; i<sz; i++)
397  {
398  if (integer[i]>x.integer[i])
399  return true;
400  if (integer[i]<x.integer[i])
401  return false;
402  }
403  return false;
404  }
405 
410  bool operator<(const CDynInt<T,sz> &x) const
411  {
412  for (int i=0; i<sz; i++)
413  {
414  if (integer[i]<x.integer[i])
415  return true;
416  if (integer[i]>x.integer[i])
417  return false;
418  }
419  return false;
420  }
421 
426  bool operator!=(const CDynInt<T,sz> &x) const
427  {
428  for (int i=sz-1; i>=0; i--)
429  {
430  if (integer[i]!=x.integer[i])
431  return true;
432  }
433  return false;
434  }
435 
443  {
444  for (int i=sz-1; i>=0; i--)
445  integer[i]|=x.integer[i];
446 
447  return *this;
448  }
449 
457  {
458  for (int i=sz-1; i>=0; i--)
459  integer[i]&=x.integer[i];
460 
461  return *this;
462  }
463 
471  {
472  for (int i=sz-1; i>=0; i--)
473  integer[i]^=x.integer[i];
474 
475  return *this;
476  }
477 
485  {
486  *this=*this<<shift;
487  return *this;
488  }
489 
497  {
498  *this=*this>>shift;
499  return *this;
500  }
501 
504  {
505  for (int i=sz-1; i>=0; i--)
506  integer[i]= ~integer[i];
507  return *this;
508  }
509 
511  operator T() { return integer[sz-1]; }
512 
515  {
516  T overflow=0;
517  for (int i=sz-1; i>=0; i--)
518  {
519  T x = integer[i]-1-overflow;
520  overflow=0;
521  if (integer[i]>x)
522  overflow=1;
523  integer[i]=x;
524  }
525  return *this;
526  }
527 
530  {
531  T overflow=0;
532  for (int i=sz-1; i>=0; i--)
533  {
534  T x = integer[i]+1+overflow;
535  overflow=0;
536  if (integer[i]>x)
537  overflow=1;
538  integer[i]=x;
539  }
540  return *this;
541  }
542 
544  void print_hex() const
545  {
546  for (int i=0; i<sz; i++)
547  SG_SPRINT("%.16llx", (uint64_t) integer[i])
548  }
549 
551  void print_bits() const
552  {
553  CMath::display_bits(integer, sz);
554  }
555 
556 private:
558  T integer[sz];
559 };
560 
563 
566 
569 
572 
576 }
577 #endif // __DYNINT_H__

SHOGUN Machine Learning Toolbox - Documentation