vrq
cuint.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright (C) 1997-2007, Mark Hummel
3  * This file is part of Vrq.
4  *
5  * Vrq is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * Vrq is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301 USA
19  *****************************************************************************
20  */
21 /*******************************************************************
22  *
23  * cuint.hpp
24  * - header file for cuint.hpp
25  *
26  *
27  *******************************************************************
28  */
29 
30 #ifndef CUINT_HPP
31 #define CUINT_HPP
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include "glue.h"
37 #include "cobstack.h"
38 
39 
40 #ifndef MAX
41 #define MAX(a,b) ((a)<(b)?(b):(a))
42 #endif // MAX
43 
44 #define CUINT_DEFAULT_SIZE (4) // size in 32 bit words
45 
50 class CUInt {
51 private:
52  unsigned int defaultData[CUINT_DEFAULT_SIZE];
53  unsigned int *data;
54  int allocatedSize;
55  int size;
56  CObstack* heap;
57 public:
58 
62  CUInt( void )
63  {
64  heap = NULL;
65  allocatedSize = CUINT_DEFAULT_SIZE;
66  data = defaultData;
67  size = 1;
68  data[0] = 0;
69  };
70 
75  CUInt( unsigned int aValue )
76  {
77  heap = NULL;
78  allocatedSize = CUINT_DEFAULT_SIZE;
79  data = defaultData;
80  size = 1;
81  data[0] = aValue;
82  };
83 
84 
89  CUInt( const CUInt &aValue )
90  {
91  INT32 i;
92 
93  heap = NULL;
94  allocatedSize = CUINT_DEFAULT_SIZE;
95  data = defaultData;
96  size = 1;
97  grow( aValue.size );
98  for( i = 0; i < size; i++ ) {
99  data[i] = aValue.data[i];
100  }
101  };
102 
106  virtual ~CUInt()
107  {
108  if( heap == NULL && data != defaultData ) {
109  shell_xfree( data, size );
110  data = NULL;
111  }
112  } ;
113 
114 
119  void SetHeap( CObstack* aHeap )
120  {
121  heap = aHeap;
122  }
123 
126  const CUInt &operator=( const CUInt &aValue )
127  {
128  INT32 i;
129 
130  grow( aValue.size );
131  for( i = 0; i < size; i++ ) {
132  data[i] = aValue.data[i];
133  }
134  return( aValue );
135  };
136 
137  const UINT64 operator=( const UINT64 aValue )
138  {
139  if( aValue>>32 != 0 ) {
140  grow( 2 );
141  data[0] = aValue;
142  data[1] = aValue>>32;
143  } else {
144  grow( 1 );
145  data[0] = aValue;
146  }
147  return( aValue );
148  };
150 
157  void Truncate( int newSize )
158  {
159  grow( newSize );
160  };
161 
164  CUInt operator~( void ) const;
166 
169  CUInt operator+( const CUInt & ) const;
170  CUInt operator+( const unsigned int ) const;
171  CUInt operator-( const CUInt & ) const;
172  CUInt operator-( unsigned int ) const;
173  CUInt operator*( const CUInt & ) const;
174  CUInt operator*( unsigned int ) const;
175  CUInt operator/( const CUInt & ) const;
176  CUInt operator/( unsigned int ) const;
177  CUInt operator%( const CUInt & ) const;
178  CUInt operator%( unsigned int ) const;
179  CUInt operator>>( const CUInt & ) const;
180  CUInt operator>>( unsigned int ) const;
181  CUInt operator<<( const CUInt & ) const;
182  CUInt operator<<( unsigned int ) const;
183  CUInt operator&( const CUInt & ) const;
184  CUInt operator&( unsigned int ) const;
185  CUInt operator|( const CUInt & ) const;
186  CUInt operator|( unsigned int ) const;
187  CUInt operator^( const CUInt & ) const;
188  CUInt operator^( unsigned int ) const;
190 
193  int operator<( const CUInt & ) const;
194  int operator<( unsigned int ) const;
195  int operator<=( const CUInt & ) const;
196  int operator<=( unsigned int ) const;
197  int operator>( const CUInt & ) const;
198  int operator>( unsigned int ) const;
199  int operator>=( const CUInt & ) const;
200  int operator>=( unsigned int ) const;
201  int operator==( const CUInt & ) const;
202  int operator==( unsigned int ) const;
203  int operator!=( const CUInt & ) const;
204  int operator!=( unsigned int ) const;
206 
209  UINT32 GetUINT32() const;
210  UINT64 GetUINT64() const;
212 
217  UINT32 Size();
218 
222 private:
223  void calibrate( void )
224  {
225  int i;
226  for( i=size-1; i>0; i-- ) {
227  if( data[i] != 0 ) {
228  break;
229  }
230  }
231  size = i+1;
232  return;
233  };
234  void grow( int aSize )
235  {
236  int i;
237 
238  if( aSize <= size ) {
239  size = aSize;
240  return;
241  }
242  if( allocatedSize < aSize ) {
243  if( heap == NULL ) {
244  if( data != defaultData ) {
245  unsigned int* newData;
246  newData = (unsigned int*)shell_xmalloc( aSize*4);
247  memcpy( newData, data, size*4 );
248  shell_xfree( data, allocatedSize*4 );
249  data = newData;
250  } else {
251  data = (unsigned int*)shell_xmalloc( aSize*4 );
252  memcpy( data, defaultData, size*4 );
253  }
254  } else {
255  unsigned int* newData;
256  newData = (unsigned int*)heap->Alloc( aSize*4 );
257  memcpy( newData, data, size*4 );
258  data = newData;
259  }
260  allocatedSize = aSize;
261  }
262  for( i=size; i < aSize; i++ ) {
263  data[i] = 0;
264  }
265  size = aSize;
266  };
270 };
271 
272 
273 
274 
275 #endif // CUINT_HPP
void * shell_xmalloc(int s)
Definition: main.cc:363
unsigned long long UINT64
Short cut for unsigned 64 bit integer.
Definition: glue.h:55
CUInt operator+(const CUInt &) const
CUInt(const CUInt &aValue)
Create a copy of unsigned integer object.
Definition: cuint.h:89
int operator<(const CUInt &) const
Infinite precision unsigned arithmetic class Storage within object will be used whenever possible...
Definition: cuint.h:50
CUInt operator%(const CUInt &) const
int operator!=(const CUInt &) const
void Truncate(int newSize)
Change size of unsigned integer.
Definition: cuint.h:157
virtual ~CUInt()
Destory object and free any storage.
Definition: cuint.h:106
CUInt operator^(const CUInt &) const
CUInt operator*(const CUInt &) const
void shell_xfree(void *p, int s)
Definition: main.cc:359
CUInt operator|(const CUInt &) const
long INT32
Short cut for signed 32 bit integer.
Definition: glue.h:38
CUInt operator~(void) const
void * Alloc(INT32 size)
Allocate block of storage with given size.
Bulk object allocation object.
Definition: cobstack.h:46
const UINT64 operator=(const UINT64 aValue)
Definition: cuint.h:137
CUInt(void)
Create a unsigned integer object.
Definition: cuint.h:62
CUInt operator/(const CUInt &) const
int operator<=(const CUInt &) const
void SetHeap(CObstack *aHeap)
Set heap to be used for storage allocation.
Definition: cuint.h:119
int operator==(const CUInt &) const
UINT32 GetUINT32() const
CUInt operator<<(const CUInt &) const
const CUInt & operator=(const CUInt &aValue)
Definition: cuint.h:126
int operator>(const CUInt &) const
CUInt operator>>(const CUInt &) const
CUInt operator&(const CUInt &) const
unsigned long UINT32
Short cut for unsigned 32 bit integer.
Definition: glue.h:47
UINT32 Size()
Calculate the number of non-zero significant bits.
CUInt operator-(const CUInt &) const
UINT64 GetUINT64() const
int operator>=(const CUInt &) const
CUInt(unsigned int aValue)
Create a unsigned integer object initialize with value.
Definition: cuint.h:75