⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bits.cpp

📁 ROSETTA C++库是一个C++类库和例程集合
💻 CPP
字号:
//-------------------------------------------------------------------
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Revisions.....:
//===================================================================

#include <stdafx.h> // Precompiled headers.
#include <copyright.h>

#include <kernel/basic/bits.h>
#include <kernel/basic/undefined.h>

#include <kernel/system/stdlib.h>
#include <kernel/system/memory.h>
#include <kernel/system/iostream.h>

//-------------------------------------------------------------------
// Static helpers (file scope).
//===================================================================

// Used to efficiently determine the number of bits set for a specific
// byte value. This is useful for quickly counting the number of elements
// in a bit set object.

static int static_counts_[256] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
                                  1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
		                              1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
		                              2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
		                              1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
		                              2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
		                              2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
		                              3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
		                              1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
		                              2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
		                              2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
		                              3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
		                              2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
		                              3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
		                              3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
		                              4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8};

//-------------------------------------------------------------------
// Methods for class Bits.
//===================================================================

//-------------------------------------------------------------------
// Method........: Constructor
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================

Bits::Bits() {

  blocks_    = NULL;
	no_blocks_ = 0;
	no_bits_   = 0;

}

//-------------------------------------------------------------------
// Method........: Constructor
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================

Bits::Bits(int no_bits, bool state) {

	blocks_    = NULL;
	no_blocks_ = 0;
	no_bits_   = 0;

	// Allocate buffer. Bits are assumed initialized as 0.
	Resize(no_bits, false);

	// Initialize contents, if needed.
	if (state)
		Invert();

}

//-------------------------------------------------------------------
// Method........: Constructor
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================

Bits::Bits(const String &text, char on, const String &delimiters) {

	blocks_    = NULL;
	no_blocks_ = 0;
	no_bits_   = 0;

	Create(text, on, delimiters);

}

//-------------------------------------------------------------------
// Method........: Constructor
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================

Bits::Bits(const Bits &bits) {

	no_blocks_ = bits.no_blocks_;
	no_bits_   = bits.no_bits_;

	if (bits.blocks_ != NULL) {
		blocks_ = new Block[no_blocks_];
		memcpy(blocks_, bits.blocks_, no_blocks_ * BYTES_PER_BLOCK);
	}
	else {
		blocks_ = NULL;
	}

}

//-------------------------------------------------------------------
// Method........: Constructor
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================

Bits::Bits(const Vector(bool) &v) {

	blocks_    = NULL;
	no_blocks_ = 0;
	no_bits_   = 0;

	Create(v);

}

//-------------------------------------------------------------------
// Method........: Constructor
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================

Bits::Bits(const Vector(int) &v, int size) {

	blocks_    = NULL;
	no_blocks_ = 0;
	no_bits_   = 0;

	// Allocate space.
	Resize(size, false);

	// Initialize buffer.
	memset(blocks_, 0, no_blocks_ * BYTES_PER_BLOCK);

	int i;

	// Set bits.
	for (i = v.size() - 1; i >= 0; i--) {
		if (v[i] >= 0 && v[i] < no_bits_)
			SetState(v[i], true);
	}

}

//-------------------------------------------------------------------
// Method........: Destructor
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================

Bits::~Bits() {

	if (blocks_ != NULL)
		delete [] blocks_;

}

//-------------------------------------------------------------------
// Operators.
//===================================================================

//-------------------------------------------------------------------
// Method........: operator =
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......: Consider more optimal reallocation.
// Revisions.....: A

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -