#include <iostream>
template<class T>
class Array
{
public:
Array();
~Array();
private:
};
template<>
class Array<bool>
{
public:
explicit Array(size_t size);
~Array();
bool operator[](size_t i) const
{
return data[i / INTBITS] & (1 << (i%INTBITS));
}
void Set(size_t i, bool v);
private:
int *data;
size_t size;
size_t realSize;
static const int INTBITS = sizeof(int) * 8;
};
Array<bool>::Array(size_t size) : size(size)
{
realSize = size / INTBITS + 1;
data = new int[size / INTBITS + 1];
for (size_t i = 0; i != realSize; ++i)
{
data[i] = 0;
}
}
Array<bool>::~Array()
{
delete[] data;
data = nullptr;
}
void Array<bool>::Set(size_t i, bool v)
{
if (v == true)
{
data[i / INTBITS] |= (1 << i%INTBITS);
}
else
{
int t = 0xffffffff ^ (1 << i%INTBITS);
data[i / INTBITS] &= t;
}
}
int main()
{
Array<bool> arr(10);
std::cout << "First loop:" << std::endl;
for (size_t i = 0; i != 10; ++i)
{
arr.Set(i, true);
std::cout << arr[i] << std::endl;
}
std::cout << "Second loop:" << std::endl;
arr.Set(0, false);
for (size_t i = 0; i != 10; ++i)
{
if (i & 1)
arr.Set(i, false);
std::cout << arr[i] << std::endl;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgoKdGVtcGxhdGU8Y2xhc3MgVD4KY2xhc3MgQXJyYXkKewpwdWJsaWM6CglBcnJheSgpOwoJfkFycmF5KCk7Cgpwcml2YXRlOgoKfTsKCgp0ZW1wbGF0ZTw+CmNsYXNzIEFycmF5PGJvb2w+CnsKcHVibGljOgoJZXhwbGljaXQgQXJyYXkoc2l6ZV90IHNpemUpOwoJfkFycmF5KCk7CgoJYm9vbCBvcGVyYXRvcltdKHNpemVfdCBpKSBjb25zdAoJewoJCXJldHVybiBkYXRhW2kgLyBJTlRCSVRTXSAmICgxIDw8IChpJUlOVEJJVFMpKTsKCX0KCgl2b2lkIFNldChzaXplX3QgaSwgYm9vbCB2KTsKCnByaXZhdGU6CglpbnQgKmRhdGE7CglzaXplX3Qgc2l6ZTsKCXNpemVfdCByZ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