#include <iostream> class UtoplyusVKostrule { public: UtoplyusVKostrule(int x, int y,…
- User
#include
class UtoplyusVKostrule { public: UtoplyusVKostrule(int x, int y, int z) : width_(x), height_(y), depth_(z) { arr_ = new int32_t[kNumLenght_ * x * y * z]; std::memset(arr_, 0, kNumLenght_ * x * y * z * sizeof(int32_t)); }
~UtoplyusVKostrule() { delete[] arr_; }
UtoplyusVKostrule(const UtoplyusVKostrule& other) : width_(other.width_), height_(other.height_), depth_(other.depth_) { arr_ = new int32_t[kNumLenght_ * width_ * height_ * depth_]; std::memcpy(arr_, other.arr_, kNumLenght_ * width_ * height_ * depth_ * sizeof(int32_t)); }
UtoplyusVKostrule& operator=(const UtoplyusVKostrule& other) { if (this != &other) { delete[] arr_; width_ = other.width_; height_ = other.height_; depth_ = other.depth_; arr_ = new int32_t[kNumLenght_ * width_ * height_ * depth_]; std::memcpy(arr_, other.arr_, kNumLenght_ * width_ * height_ * depth_ * sizeof(int32_t)); } return *this; }
static UtoplyusVKostrule make_array(int x, int y, int z) { return UtoplyusVKostrule(x, y, z); }
UtoplyusVKostrule& operator()(int x, int y, int z) { current_index_ = x + y * width_ + z * width_ * height_; return *this; }
UtoplyusVKostrule& operator=(int32_t value) { int first_bit_index = current_index_ * kNumLenght_;
char* c_value = new char[kNumLenght_]; int a = -1; while (value != 0) { c_value[++a] = (value % 2) + '0'; value /= 2; } for (int i = 0; i < kNumLenght_; ++i) { if (c_value[i] - '0') { SetBit(arr_, first_bit_index + i); } } delete[] c_value; return *this;}
private: const int8_t kNumLenght_ = 17; int32_t current_index_; int32_t width_; int32_t height_; int32_t depth_; int32_t* arr_;
bool TakeBit(const int32_t* value, uint32_t bit_position) { return (value[bit_position / 32] >> (bit_position % 32)) & 1; }
UtoplyusVKostrule& SetBit(int32_t* value, int32_t bit_position) { value[bit_position / 32] |= (1 << (bit_position % 32)); return *this; }
int32_t ToDec(int32_t* arr_, int32_t current_index_) const { int first_bit_index = current_index_ * kNumLenght_;
int32_t decimal_value = 0; int32_t exp = 1; for (int i = first_bit_index + kNumLenght_ - 1; i >= first_bit_index; --i) { decimal_value += (arr_[i / 32] >> (i % 32) & 1) * exp; exp *= 2; } return decimal_value;}
UtoplyusVKostrule& TwoImplement(int32_t* arr_) { int32_t current_value = ToDec(arr_, current_index_); int32_t inverted_value = ~current_value; int32_t new_value = inverted_value + 1;
for (int i = 0; i < kNumLenght_; ++i) { arr_[current_index_ / 32] = (arr_[current_index_ / 32] & ~(1 << (current_index_ % 32))) | ((new_value & 1) << (current_index_ % 32)); } return *this;}
friend UtoplyusVKostrule& operator+(const UtoplyusVKostrule& lhs, const UtoplyusVKostrule& rhs);
friend std::ostream& operator<<(std::ostream& stream, const UtoplyusVKostrule& array); };
- Assistant
- User
- Assistant
- User
- Assistant
- User
- Assistant
- User
- Assistant
- User
- Assistant
- User
- Assistant
There are still some issues in your current implementation. The main problem seems to be with the bit indexing and how the values are stored in and retrieved from the array. Let's go over your , , and the .
- User
- Assistant
- User
- Assistant
- User
- Assistant
The error you're encountering during the linking process indicates that the linker is unable to find the definition of
operator<<. The declaration is provided in your class as a friend function, but there's no corresponding definition found in your code. You need to define it outside of your class definition. - User
- Assistant
- User
- Assistant
- User
- Assistant
- User
- Assistant
- User
- Assistant
- User
[image: Open in Visual Studio Code] open
Лабораторная работа 5
Задача
Спроектировать и реализовать класс для хранения трехмерного массива 17-битных положительных целых чисел.
Требования
- Assistant
Considering the specialized nature of the library that handles a 3D array of 17-bit positive integers, the name should reflect its functionality and data structure design. Here are a few suggestions for naming such a library:
TriBitArray: A combination of "Tri" (indicating three-dimensional) and "BitArray," indicating that it is an array handling individual bits.BitCube17: Suggesting a 3-dimensional "cube" of bits, with "17" indicating the specific number of bits for each element.Int17Cube: Emphasizing that it is a 3-dimensional array ('cube') of 17-bit integers.BitMatrix3D17: Combining "Bit," "Matrix," and "3D" to denote a three-dimensional bit matrix, with "17" for the bit length.VoxelGrid17: Using "Voxel" to suggest a three-dimensional pixel, common in graphics terminology, with "Grid" indicating an arrangement in space, and "17" for the bit-length.
I would recommend
TriBitArrayorBitCube17as they are concise and clearly represent the structure and functionality of the library. - User
- Assistant