File: bitVector.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 239,888 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; makefile: 95
file content (612 lines) | stat: -rw-r--r-- 11,340 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//

#include <BALL/DATATYPE/bitVector.h>
#include <BALL/MATHS/common.h>

#include <algorithm>

namespace BALL 
{

	#	ifdef BALL_NO_INLINE_FUNCTIONS
	#		include <BALL/DATATYPE/bitVector.iC>
	#	endif

	const Size BitVector::BlockSize = BALL_BLOCK_BITS;
	const Size BitVector::CharBits  = BALL_CHAR_SIZE * 8;

	Bit::IllegalOperation::IllegalOperation(const char* file, int line)
		:	Exception::GeneralException(file, line)
	{
		message_ = "Trying to modify a const bitvector by a bit";
		Exception::globalHandler.setMessage(message_);
	}


	Bit::~Bit()
	{
	}

	Bit::Bit()
		: bitvector_(0),
			index_(0),
			bitvector_muteable_(false)
	{
	}

	Bit::Bit(const Bit& bit) 
		: bitvector_(bit.bitvector_),
			index_(bit.index_),
			bitvector_muteable_(bit.bitvector_muteable_)
	{
	}

	BitVector::BitVector()
		:	size_(0),
			bitset_(1)
	{
		bitset_[0] = (BlockType)0;
	}

	BitVector::BitVector(Size size)
		:	size_(size)
	{				
		bitset_.resize(BALL_BLOCK_SIZE(size));
		for (Position i = 0; i < bitset_.size(); i++)
		{
			bitset_[i] = (BlockType)0;
		}
	}

	BitVector::BitVector(const BitVector& bit_vector)
		:	size_(bit_vector.size_),
			bitset_(bit_vector.bitset_)
	{
	}

	BitVector::BitVector(const char* bit_string)
		: size_(0),
			bitset_()
	{
		set(bit_string);
	}

	BitVector::~BitVector()
	{
	}

	void BitVector::set(const BitVector& bit_vector)
	{
		bitset_= bit_vector.bitset_;
		size_	= bit_vector.size_;
	}

	void BitVector::validateRange_(Index& first, Index& last) const
	{
		// indices may be given as negative arguments: start from the end
		// -1 therefore means the last bit.
		if (last < 0)
		{
			last += size_;	
		}

		if (first < 0)
		{
			first += size_;
		}
		
		// if the values are out of bounds - throw an exception
		// and leave it...
		if (last < 0)
		{
			throw Exception::IndexUnderflow(__FILE__, __LINE__, last, size_);
		}
		if (first < 0)
		{
			throw Exception::IndexUnderflow(__FILE__, __LINE__, first, size_);
		}
		if ((Size)last >= size_)
		{
			throw Exception::IndexOverflow(__FILE__, __LINE__, last, size_);
		}
		if ((Size)first >= size_)
		{
			throw Exception::IndexOverflow(__FILE__, __LINE__, first, size_);
		}
		
		// now swap, if last < first
		if (last >= first) 
		{
			return;
		}
		Index tmp = last;
		last = first;
		first = tmp;
	}

	void BitVector::validateIndex_(Index& index)
	{
		// indices may be given as negative arguments: start from the end
		// -1 therefore means the last bit.
		if (index < 0)
		{
			index += size_;
		}

		// if the values are out of bounds - throw an exception
		// leave it...
		if (index < 0)
		{
			throw Exception::IndexUnderflow(__FILE__, __LINE__);
		}

		if ((Size)index >= size_)
		{
			setSize(index + 1);
		}
	}

	void BitVector::validateIndex_(Index& index) const
	{
		// indices may be given as negative arguments: start from the end
		// -1 therefore means the last bit.
		if (index < 0)
		{
			index += size_;
		}

		// if the values are out of bounds - throw an exception
		// leave it...
		if (index < 0)
		{
			throw Exception::IndexUnderflow(__FILE__, __LINE__);
		}

		if ((Size)index >= size_)
		{
			throw Exception::IndexOverflow(__FILE__, __LINE__);
		}
	}

	BitVector BitVector::operator () (Index first, Index last) const
	{
		validateRange_(first, last);

		BitVector temp(last - first + 1);
		Index source = first;
		Index target = 0;
		Position end = std::min((Position)last, size_ - 1);
		
		for (; (Position)source <= end; source++, target++)
		{
			temp.setBit(target, getBit(source));
		}

		return temp;
	}

	BALL::Size BitVector::countValue(bool value) const
	{
		Size count = 0;

		for (Position index = 0; index < size_; index++)
		{
			if (getBit((Index)index) == value)
			{
				count++;
			}
		}

		return count;
	}

	void BitVector::set(const char* bit_string)
	{
		if (bit_string == 0)
		{
			return;
		}

		size_ = (Size)strlen(bit_string);
		bitset_.resize((Size)((size_ + BALL_BLOCK_MASK) >> BALL_BLOCK_SHIFT));
		for (Position i = 0; i < bitset_.size(); i++)
		{
			bitset_[i] = (BlockType)0;
		}
				
		const char* tmp = bit_string;
		for (Index i = size_ - 1; i >= 0; i--)
		{
			if (*tmp != '0')
			{
				bitset_[block_(i)] |= mask_(i);
			}
			tmp++;
		}
	}

	void BitVector::fill(bool bit, Index first, Index last)
	{
		if (size_ == 0)
		{
			return;
		}
		validateRange_(first, last);
		for (Index i = first; i <= last; i++) 
		{
			setBit(i, bit);
		}
	}

	void BitVector::toggle(Index first, Index last)
	{
		validateRange_(first, last);
		for (Index i = first; i <= last; i++)
		{
			toggleBit(i);
		}
	}

	void BitVector::setUnsignedChar(unsigned char bit_pattern)
	{
		setSize(CharBits, false);
		unsigned char c = bit_pattern;

		// We do this in a loop instead of using a direct cast to avoid
		// problems with differing byte orders (big endian/little endian)
		for (Position i = 0; i < CharBits; i++)
		{
			setBit((Index)i, (((int)c & (int)0x1) == 1));
			c = c >> 1;
		}
	}

	unsigned char BitVector::getUnsignedChar() const
	{
		unsigned char c = 0;

		// We do this in a loop instead of using a direct cast to avoid
		// problems with differing byte orders (big endian/little endian)

		for (Index i = (Index)std::min(CharBits, getSize()) - 1; i >= 0; i--)
		{
			c = c << 1;
			if (getBit((Index)i))
			{
				c |= 1;
			}
		}

		return c;
	}

	void BitVector::setUnsignedShort(unsigned short bit_pattern)
	{
		setSize(sizeof(unsigned short) * CharBits, false);
		unsigned short c = bit_pattern;

		for (Position i = 0; i < sizeof(unsigned short) * CharBits; i++)
		{
			setBit((Index)i, (((int)c & (int)0x1) == 1));
			c = c >> 1;
		}
	}

	unsigned short BitVector::getUnsignedShort() const
	{
		unsigned short c = 0;
		Index i = (Index)std::min((Size)(sizeof(unsigned short) * CharBits), getSize()) - 1;
		for (; i >= 0; i--)
		{
			c = c << 1;
			if (getBit((Index)i))
			{
				c |= 1;
			}
		}

		return c;
	}

	void BitVector::setUnsignedInt(unsigned int bit_pattern)
	{
		setSize(sizeof(unsigned int) * CharBits, false);
		unsigned int c = bit_pattern;

		for (Position i = 0; i < sizeof(unsigned int) * CharBits; i++)
		{
			setBit((Index)i, (((int)c & (int)0x1) == 1));
			c = c >> 1;
		}
	}

	unsigned int BitVector::getUnsignedInt() const
	{
		unsigned int c = 0;

		Index i = (Index)std::min((Size)(sizeof(unsigned int) * CharBits), getSize()) - 1;
		for (; i >= 0; i--)
		{
			c = c << 1;
			if (getBit((Index)i))
			{
				c |= 1;
			}
		}

		return c;
	}

	void BitVector::setUnsignedLong(unsigned long bit_pattern)
	{
		setSize(sizeof(unsigned long) * CharBits, false);
		unsigned long c = bit_pattern;

		for (Position i = 0; i < sizeof(unsigned long) * CharBits; i++)
		{
			setBit((Index)i, (((int)c & (int)0x1) == 1));
			c = c >> 1;
		}
	}

	unsigned long BitVector::getUnsignedLong() const
	{
		unsigned long c = 0;

		Index i = (Index)std::min((Size)(sizeof(unsigned long) * CharBits), getSize()) - 1;
		for (; i >= 0; i--)
		{
			c = c << 1;
			if (getBit((Index)i))
			{
				c |= 1;
			}
		}

		return c;
	}

	void BitVector::bitwiseXor(const BitVector& bit_vector)
	{
		// adjust the bitvector size to that of the longest vector!
		if (size_ < bit_vector.size_)
		{
			setSize(bit_vector.size_, true);
		} 

		for (Position i = 0; i < std::min(bitset_.size(), bit_vector.bitset_.size()); i++)
		{
			bitset_[i] ^= bit_vector.bitset_[i];
		}
	}

	void BitVector::bitwiseOr(const BitVector& bit_vector)
	{
		// adjust the bitvector size to that of the longest vector!
		if (size_ < bit_vector.size_)
		{
			setSize(bit_vector.size_, true);
		}

		for (Position i = 0; i < std::min(bitset_.size(), bit_vector.bitset_.size()); i++)
		{
			bitset_[i] |= bit_vector.bitset_[i];
		}
	}

	void BitVector::bitwiseAnd(const BitVector& bit_vector)
	{
		for (Position i = 0; i < bitset_.size(); i++)
		{
			bitset_[i] &= ((i < bit_vector.bitset_.size()) ? bit_vector.bitset_[i] : (BlockType)0);
		}
	}

	bool BitVector::operator == (const BitVector& bit_vector) const
	{
		if (size_ != bit_vector.size_)
		{
			return false;
		}

		for (Index i = 0; i < (Index)size_; i++) 
		{
			const Index sh_i = i >> BALL_BLOCK_SHIFT;
			if ((bitset_[sh_i] & mask_(i)) != (bit_vector.bitset_[sh_i] & bit_vector.mask_(i)))
			{
				return false;
			}
		}
	 
		return true;
	}

	bool BitVector::isAnyBit(bool bit, Index first, Index last) const
	{
		validateRange_(first, last);

		Index	i;
		for (i = first; i <= last; i++)
		{
			if ((*this)[i] == bit)
			{
				return true;
			}
		}
	 
		return false;
	}

	bool BitVector::isEveryBit(bool bit, Index first, Index last) const
	{
		validateRange_(first, last);

		Index	i;
		for (i = first; i <= last ; i++)
		{
			if ((*this)[i] != bit)
			{
				return false;
			}
		}

		return true;
	}

	std::istream& operator >> (std::istream& s, BitVector& bit_vector)
	{
		bit_vector.read(s);
		return s;
	}

	std::ostream& operator << (std::ostream& s, const BitVector& bit_vector)
	{
		bit_vector.write(s);
		return s;
	}

	void BitVector::read(std::istream& s)
	{
		Size size = 0;
		s >> size;
		setSize(size, false);
		size--;

		char c;
		s.get(c); // read leading blank
		for (Index i = (Index)size; i >= 0; i--) 
		{
			s.get(c);

			if (c != '0') 
			{
				setBit(i);
			}
		}
	}

	void BitVector::write(std::ostream& s) const
	{
		s << getSize() << ' ';

		for (Index i = (Index)size_ - 1; i >= 0; i--) 
		{
			s << (getBit(i) ? '1' : '0');
		}

		s << ' ';
	}

	bool BitVector::read(PersistenceManager& pm)
	{
		Size size = 0;
		
		if (!pm.readPrimitive(size, "size"))
		{
			return false;
		}
			
		setSize(size, false);
		size--;
		bool bit;
		for (Index i = (Index)size; i >= 0 && pm.readPrimitive(bit, ""); i--) 
		{
			setBit(i, bit);
		}

		return true;
	}

	void BitVector::write(PersistenceManager& pm) const
	{
		Size size = getSize();
		pm.writePrimitive(size, "size");

		for (Index i = (Index)size_ - 1; i >= 0; i--) 
		{
			bool bit = getBit(i);
			pm.writePrimitive(bit, "");
		}
	}

	Index BitVector::block_(Index index)
	{
		if (index < 0)
		{
			index = (Index)size_ - index + 1;
		}

		if (index < 0)
		{
			throw Exception::IndexUnderflow(__FILE__, __LINE__);
		}
		
		if ((Size)index >= size_)
		{
			Size block_size = (Size)((index + BALL_BLOCK_BITS) >> BALL_BLOCK_SHIFT);

			if (block_size > bitset_.size())
			{
				setSize(block_size);
			}
		
			size_ = index + 1; 
		}

		return (index >> BALL_BLOCK_SHIFT);
	}

	Index BitVector::block_(Index index) const
	{
		if (index < 0)
		{
			index = (Index)size_ - index + 1;
		}

		if (index < 0)
		{
			throw Exception::IndexUnderflow(__FILE__, __LINE__);
		}
		
		if ((Size)index >= size_)
		{
			throw Exception::IndexOverflow(__FILE__, __LINE__);
		}

		return (index >> BALL_BLOCK_SHIFT);
	}

	void BitVector::setSize(Size size, bool keep) 
	{
		// calculate new block size
		Size block_size = BALL_BLOCK_SIZE(size);

		if (keep) 
		{
			if (block_size != bitset_.size()) 
			{
				// resize the block array and reset its
				bitset_.resize(block_size);
			}

			// reset the new contents to zero
			for (Position i = size_; i < size; i++)
			{		
				bitset_[block_(i)] &= ~mask_(i);
			}
		} 
		else 
		{
			// resize the array and clear its contents
			bitset_.resize(block_size);
			for (Position i = 0; i < bitset_.size(); i++)
			{
				bitset_[i] = (BlockType)0;
			}
		}

		size_ = size;
	}
} // namespace BALL