SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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/config.h>
15 
16 #include <shogun/lib/common.h>
17 #include <shogun/io/SGIO.h>
19 
20 namespace shogun
21 {
38 template <class T, int sz> class CDynInt
39 {
40 public:
46  {
47  for (int i=0; i<sz; i++)
48  integer[i]=0;
49  }
50 
57  CDynInt(uint8_t x)
58  {
59  for (int i=0; i<sz-1; i++)
60  integer[i]=0;
61  integer[sz-1]= (T) x;
62  }
63 
70  CDynInt(uint16_t x)
71  {
72  for (int i=0; i<sz-1; i++)
73  integer[i]=0;
74  integer[sz-1]= (T) x;
75  }
76 
83  CDynInt(uint32_t x)
84  {
85  for (int i=0; i<sz-1; i++)
86  integer[i]=0;
87  integer[sz-1]= (T) x;
88  }
89 
96  CDynInt(int32_t x)
97  {
98  for (int i=0; i<sz-1; i++)
99  integer[i]=0;
100  integer[sz-1]= (T) x;
101  }
102 
109  CDynInt(int64_t x)
110  {
111  for (int i=0; i<sz-1; i++)
112  integer[i]=0;
113  integer[sz-1]=(T) x;
114  }
115 
122  CDynInt(uint64_t x)
123  {
124  for (int i=0; i<sz-1; i++)
125  integer[i]=0;
126  integer[sz-1]=(T) x;
127  }
128 
135  CDynInt(const T x[sz])
136  {
137  for (int i=0; i<sz; i++)
138  integer[i]=x[i];
139  }
140 
143  {
144  for (int i=0; i<sz; i++)
145  integer[i]=x.integer[i];
146  }
147 
150  {
151  }
152 
157  {
158  for (int i=0; i<sz; i++)
159  integer[i]=x.integer[i];
160  return *this;
161  }
162 
167  const CDynInt<T,sz> operator|(const CDynInt<T,sz>& x) const
168  {
169  CDynInt<T,sz> r;
170 
171  for (int i=sz-1; i>=0; i--)
172  r.integer[i]=integer[i] | x.integer[i];
173 
174  return r;
175  }
176 
181  const CDynInt<T,sz> operator&(const CDynInt<T,sz>& x) const
182  {
183  CDynInt<T,sz> r;
184 
185  for (int i=sz-1; i>=0; i--)
186  r.integer[i]=integer[i] & x.integer[i];
187 
188  return r;
189  }
190 
198  {
199  CDynInt<T,sz> r=*this;
200 
201  while (shift>0)
202  {
203  int s=CMath::min(shift, 8*((int) sizeof(T))-1);
204 
205  for (int i=0; i<sz; i++)
206  {
207  T overflow=0;
208  if (i<sz-1)
209  overflow = r.integer[i+1] >> (sizeof(T)*8 - s);
210  r.integer[i]= (r.integer[i] << s) | overflow;
211  }
212 
213  shift-=s;
214  }
215 
216  return r;
217  }
218 
226  {
227  CDynInt<T,sz> r=*this;
228 
229  while (shift>0)
230  {
231  int s=CMath::min(shift, 8*((int) sizeof(T))-1);
232 
233  for (int i=sz-1; i>=0; i--)
234  {
235  T overflow=0;
236  if (i>0)
237  overflow = (r.integer[i-1] << (sizeof(T)*8 - s));
238  r.integer[i]= (r.integer[i] >> s) | overflow;
239  }
240 
241  shift-=s;
242  }
243 
244  return r;
245  }
246 
251  const CDynInt<T,sz> operator^(const CDynInt<T,sz>& x) const
252  {
253  CDynInt<T,sz> r;
254 
255  for (int i=sz-1; i>=0; i--)
256  r.integer[i]=integer[i] ^ x.integer[i];
257 
258  return r;
259  }
260 
265  const CDynInt<T,sz> operator+(const CDynInt<T,sz> &x) const
266  {
267  CDynInt<T,sz> r;
268 
269  T overflow=0;
270  for (int i=sz-1; i>=0; i--)
271  {
272  r.integer[i]=integer[i]+x.integer[i]+overflow;
273  if (r.integer[i] < CMath::max(integer[i], x.integer[i]))
274  overflow=1;
275  else
276  overflow=0;
277  }
278 
279  return x;
280  }
281 
286  const CDynInt<T,sz> operator-(const CDynInt<T,sz> &x) const
287  {
288  return NULL;
289  }
290 
295  const CDynInt<T,sz> operator/(const CDynInt<T,sz> &x) const
296  {
297  return NULL;
298  }
299 
304  const CDynInt<T,sz> operator*(const CDynInt<T,sz> &x) const
305  {
306  return NULL;
307  }
308 
314  {
315  return NULL;
316  }
317 
323  {
324  return NULL;
325  }
326 
332  {
333  return NULL;
334  }
335 
341  {
342  return NULL;
343  }
344 
349  bool operator==(const CDynInt<T,sz> &x) const
350  {
351  for (int i=sz-1; i>=0; i--)
352  {
353  if (integer[i]!=x.integer[i])
354  return false;
355  }
356 
357  return true;
358  }
359 
364  bool operator>=(const CDynInt<T,sz> &x) const
365  {
366  for (int i=0; i<sz; i++)
367  {
368  if (integer[i]>x.integer[i])
369  return true;
370  if (integer[i]<x.integer[i])
371  return false;
372  }
373  return true;
374  }
375 
380  bool operator<=(const CDynInt<T,sz> &x) const
381  {
382  for (int i=0; i<sz; i++)
383  {
384  if (integer[i]<x.integer[i])
385  return true;
386  if (integer[i]>x.integer[i])
387  return false;
388  }
389  return true;
390  }
391 
396  bool operator>(const CDynInt<T,sz> &x) const
397  {
398  for (int i=0; i<sz; i++)
399  {
400  if (integer[i]>x.integer[i])
401  return true;
402  if (integer[i]<x.integer[i])
403  return false;
404  }
405  return false;
406  }
407 
412  bool operator<(const CDynInt<T,sz> &x) const
413  {
414  for (int i=0; i<sz; i++)
415  {
416  if (integer[i]<x.integer[i])
417  return true;
418  if (integer[i]>x.integer[i])
419  return false;
420  }
421  return false;
422  }
423 
428  bool operator!=(const CDynInt<T,sz> &x) const
429  {
430  for (int i=sz-1; i>=0; i--)
431  {
432  if (integer[i]!=x.integer[i])
433  return true;
434  }
435  return false;
436  }
437 
445  {
446  for (int i=sz-1; i>=0; i--)
447  integer[i]|=x.integer[i];
448 
449  return *this;
450  }
451 
459  {
460  for (int i=sz-1; i>=0; i--)
461  integer[i]&=x.integer[i];
462 
463  return *this;
464  }
465 
473  {
474  for (int i=sz-1; i>=0; i--)
475  integer[i]^=x.integer[i];
476 
477  return *this;
478  }
479 
487  {
488  *this=*this<<shift;
489  return *this;
490  }
491 
499  {
500  *this=*this>>shift;
501  return *this;
502  }
503 
506  {
507  for (int i=sz-1; i>=0; i--)
508  integer[i]= ~integer[i];
509  return *this;
510  }
511 
513  operator T() { return integer[sz-1]; }
514 
517  {
518  T overflow=0;
519  for (int i=sz-1; i>=0; i--)
520  {
521  T x = integer[i]-1-overflow;
522  overflow=0;
523  if (integer[i]>x)
524  overflow=1;
525  integer[i]=x;
526  }
527  return *this;
528  }
529 
532  {
533  T overflow=0;
534  for (int i=sz-1; i>=0; i--)
535  {
536  T x = integer[i]+1+overflow;
537  overflow=0;
538  if (integer[i]>x)
539  overflow=1;
540  integer[i]=x;
541  }
542  return *this;
543  }
544 
546  void print_hex() const
547  {
548  for (int i=0; i<sz; i++)
549  SG_SPRINT("%.16llx", (uint64_t) integer[i])
550  }
551 
553  void print_bits() const
554  {
555  CMath::display_bits(integer, sz);
556  }
557 
558 private:
560  T integer[sz];
561 };
562 
565 
568 
571 
574 
578 }
579 #endif // __DYNINT_H__
CDynInt< T, sz > & operator/=(const CDynInt< T, sz > &x)
Definition: DynInt.h:340
CDynInt< T, sz > & operator~()
Definition: DynInt.h:505
const CDynInt< T, sz > operator+(const CDynInt< T, sz > &x) const
Definition: DynInt.h:265
const CDynInt< T, sz > operator|(const CDynInt< T, sz > &x) const
Definition: DynInt.h:167
CDynInt< T, sz > & operator|=(const CDynInt< T, sz > &x)
Definition: DynInt.h:444
const CDynInt< T, sz > operator*(const CDynInt< T, sz > &x) const
Definition: DynInt.h:304
bool operator==(const CDynInt< T, sz > &x) const
Definition: DynInt.h:349
bool operator>(const CDynInt< T, sz > &x) const
Definition: DynInt.h:396
CDynInt< T, sz > & operator*=(const CDynInt< T, sz > &x)
Definition: DynInt.h:331
CDynInt< uint64_t, 4 > uint256_t
256 bit integer constructed out of 4 64bit uint64_t's
Definition: DynInt.h:570
CDynInt< T, sz > operator>>(int shift)
Definition: DynInt.h:225
bool operator!=(const CDynInt< T, sz > &x) const
Definition: DynInt.h:428
void print_bits() const
Definition: DynInt.h:553
CDynInt< T, sz > & operator--()
Definition: DynInt.h:516
CDynInt(int64_t x)
Definition: DynInt.h:109
CDynInt< uint64_t, 3 > uint192_t
192 bit integer constructed out of 3 64bit uint64_t's
Definition: DynInt.h:567
CDynInt< uint64_t, 8 > uint512_t
512 bit integer constructed out of 8 64bit uint64_t's
Definition: DynInt.h:573
CDynInt< T, sz > & operator^=(const CDynInt< T, sz > &x)
Definition: DynInt.h:472
#define SG_SPRINT(...)
Definition: SGIO.h:180
CDynInt(const T x[sz])
Definition: DynInt.h:135
CDynInt< T, sz > & operator-=(const CDynInt< T, sz > &x)
Definition: DynInt.h:322
CDynInt< T, sz > & operator+=(const CDynInt< T, sz > &x)
Definition: DynInt.h:313
const CDynInt< T, sz > operator-(const CDynInt< T, sz > &x) const
Definition: DynInt.h:286
CDynInt(uint32_t x)
Definition: DynInt.h:83
CDynInt< T, sz > operator<<(int shift)
Definition: DynInt.h:197
static T max(T a, T b)
Definition: Math.h:168
CDynInt(uint8_t x)
Definition: DynInt.h:57
const CDynInt< T, sz > operator^(const CDynInt< T, sz > &x) const
Definition: DynInt.h:251
static void display_bits(T word, int32_t width=8 *sizeof(T))
Definition: Math.h:1635
CDynInt< T, sz > & operator>>=(int shift)
Definition: DynInt.h:498
const CDynInt< T, sz > operator&(const CDynInt< T, sz > &x) const
Definition: DynInt.h:181
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
void print_hex() const
Definition: DynInt.h:546
CDynInt< uint64_t, 16 > uint1024_t
1024 bit integer constructed out of 16 64bit uint64_t's
Definition: DynInt.h:576
CDynInt< T, sz > & operator<<=(int shift)
Definition: DynInt.h:486
CDynInt(int32_t x)
Definition: DynInt.h:96
static T min(T a, T b)
Definition: Math.h:157
CDynInt< T, sz > & operator++()
Definition: DynInt.h:531
CDynInt< T, sz > & operator&=(const CDynInt< T, sz > &x)
Definition: DynInt.h:458
CDynInt< T, sz > & operator=(const CDynInt< T, sz > &x)
Definition: DynInt.h:156
integer type of dynamic size
Definition: DynInt.h:38
const CDynInt< T, sz > operator/(const CDynInt< T, sz > &x) const
Definition: DynInt.h:295
bool operator>=(const CDynInt< T, sz > &x) const
Definition: DynInt.h:364
CDynInt(const CDynInt< T, sz > &x)
Definition: DynInt.h:142
CDynInt(uint64_t x)
Definition: DynInt.h:122
CDynInt(uint16_t x)
Definition: DynInt.h:70

SHOGUN Machine Learning Toolbox - Documentation