// base version
T* base{};
// size of base version
int size{};
// container for roots of versions
std::vector<Node<T>*> versions{};
std::vector<std::pair<int, int>> parent_children_relation{};
// recursive function to build tree with leafs as elemnts of _array in [_l..._r] and _node as root of version
void buildTree(Node<T>* _node, T* _array, int _left_border, int _right_border);
// recursive function to add new version by changing one value in one of previous version
void add(Node<T>* _to, Node<T>* _from, int _left_border, int _right_border, int _position, T _new_value);
// recursive function to get element of specific node
T get(Node<T>* _node, int _left_border, int _right_border, int _position);
// recursive function to count leaves in a tree
int countLeaves(Node<T>* _node);
// print from [_left] to [_right]
void printSlice(T* _arr, int _left, int _right);
public:
// constructor with empty array of _size * elements
PersistentArray(int _size);
// constructor with _array of _size * elements
PersistentArray(T* _arr, int _size);
// destructor
~PersistentArray();
// set element to base version
void setElementToBase(int _index, T _value);
// change element by index in a specific version with new value
void changeVersion(int _number_of_version, int _change_index, T _new_value);
// add new version by changing one value in one of previous version
void addVersion(int _root_position, int _change_inde, T _new_value);
// print all versions as a table
void printAllVersions();
// get size of a specific version
int versionSize(int _number_of_version);
// get a specific version as array
T* getArray(int _number_of_version);
// delete element in a specific version
void deleteElement(int _root_position, int _delete_index);
// insert a new value in a specific version
void insertElement(int _root_position, int _insert_index, T _new_value);
// get a root of specific version
Node<T>* getVersion(int _number_of_version);
// get number of versions
int sizeVersions();
};
template
class PersistentDoublyLinkedList
{
private:
Assistant
Чтобы понимать приведённый код на C++, вам нужно изучить и освоить несколько ключевых тем. Вот список тем, которые будут полезны:
Основы C++:
Синтаксис и структура программы.
Типы данных: базовые (int, float, char и т.д.) и составные (массивы, структуры, классы).
Операторы (арифметические, логические и т.д.).
// base version
DL_node<T>* base;
// size of base version
int size{};
// container for roots of versions
std::vector<Node<T>*> versions{};
std::vector<std::pair<int, int>> parent_children_relation{};
// recursive function to build tree with leafs as elemnts of _array in [_l..._r] and _node as root of version
void buildTree(Node<T>* _node, DL_node<T>* _list_head, int _left_border, int _right_border);
// recursive function to add new version by changing one value in one of previous version
void add(Node<T>* _to, Node<T>* _from, int _left_border, int _right_border, int _position, T _new_value);
// recursive function to get element of specific node
T get(Node<T>* _node, int _left_border, int _right_border, int _position);
// making a single node with data and assign it to base
void initDL(T _data);
// recursive function to count leaves in a tree
int countLeaves(Node<T>* _node);
public:
// constructor that creats one node with _data in it
PersistentDoublyLinkedList(T _data);
// constructor that creates double linked list from array
PersistentDoublyLinkedList(T* _arr, int _size);
// constructor that takes _head as base
PersistentDoublyLinkedList(DL_node<T>* _head);
// destructor
~PersistentDoublyLinkedList();
// adding data to end
void append(T _data);
// print from [_left] to [_right]
void printSlice(DL_node<T>* _head, int _left, int _right);
// get size of a specific DL_list
int getSize(DL_node<T>* _head);
// get element by index in DL list
T getElement(DL_node<T>* _head, int _index);
// change element by index in a specific version with new value
void changeVersion(int _number_of_version, int _new_position, T _new_value);
// add new version by changing one value in one of previous version
void addVersion(int _root_position, int _new_position, T _new_value);
// print all versions as a table
void printAllVersions();
// get size of a specific version
int versionSize(int _number_of_version);
// get a specific version as array
DL_node<T>* getList(int _number_of_version);
// delete element in a specific version
void deleteElement(int _root_position, int _delete_index);
// insert a new value in a specific version
void insertElement(int _root_position, int _insert_index, T _new_value);
// get a root of specific version
Node<T>* getVersion(int _number_of_version);
// get number of versions
int sizeVersions();
void setElementToBase(KeyType, ValueType);
ValueType getElement(AA_node<KeyType, ValueType>*, KeyType);
ValueType getElementByIndex(AA_node<KeyType, ValueType>*, int, std::vector<KeyType>);
void addVersion(int, KeyType, ValueType);
void printSlice(AA_node<KeyType, ValueType>*, int, int);
void printAllVersions();
void changeVersion(int, KeyType, ValueType);
int versionSize(int);
int getSize(AA_node<KeyType, ValueType>*);
AA_node<KeyType, ValueType>* getAssociativeArray(int);
void deleteElement(int, KeyType);
void insertElement(int, int, KeyType, ValueType);
Node<ValueType>* getVersion(int _number_of_version);
// get number of versions
int sizeVersions();
};
template
class Convert
{
private:
// recursive function to insert by key with value
//static AA_node<std::string, ValueType>* insertRecursive(AA_node<std::string, ValueType>*, std::string, ValueType, std::vector<KeyType>);
// recursive function to get element of specific node
static ValueType get(Node<ValueType>* _node, int _left_border, int _right_border, int _position);
static int nodeSize(Node<ValueType>* _node);
public:
// TODO : _version_size in deep
// fuction that creates an object of PersistentArray class from tree
static PersistentArray<ValueType> toArray(Node<ValueType>* _version);
// fuction that creates an object of PersistentDoublyLinkedList class from tree
static PersistentDoublyLinkedList<ValueType> toDLList(Node<ValueType>* _version);
// fuction that creates an object of PersistentAssociativeArray class from tree
template<typename KeyType>
static PersistentAssociativeArray<KeyType, ValueType> toAssociativeArray(Node<ValueType>* _version, KeyType* _keys);
if (_left == _right)
{
_node->value = _keys_variant.at(_left - 1);
//std::cout << "in if\t" << _node->value << "\n";
}
else
{
int middle = (_left + _right) / 2; //std::cout << "m = " << middle << "\n";
_node->left_child = new Node<KeyType>(); //std::cout << "\nmaking left child";
buildTreeKey(_node->left_child, _root, _left, middle, _keys_variant);
_node->right_child = new Node<KeyType>(); //std::cout << "\nmaking right child";
buildTreeKey(_node->right_child, _root, middle + 1, _right, _keys_variant);
}
}
template<typename KeyType, typename ValueType>
inline void PersistentAssociativeArray<KeyType, ValueType>::addValue(Node* _to, Node* _from, int _left_border, int _right_border, int _position, ValueType _new_value)
{
return add(_to, _from, _left_border, _right_border, _position, _new_value);
}
template<typename KeyType, typename ValueType>
inline void PersistentAssociativeArray<KeyType, ValueType>::addKey(Node* _to, Node* _from, int _left_border, int _right_border, int _position, KeyType _new_key)
{
return add(_to, _from, _left_border, _right_border, _position, _new_key);
}
template<typename KeyType, typename ValueType>
inline ValueType PersistentAssociativeArray<KeyType, ValueType>::getValue(Node* _node, int _left_border, int _right_border, int _position)
{
return get(_node, _left_border, _right_border, _position);
}
template<typename KeyType, typename ValueType>
inline KeyType PersistentAssociativeArray<KeyType, ValueType>::getKey(Node* _node, int _left_border, int _right_border, int _position)
{
return get(_node, _left_border, _right_border, _position);
}
template<typename KeyType, typename ValueType>
inline int PersistentAssociativeArray<KeyType, ValueType>::countLeaves(Node* _node)
{
if (_node == nullptr)
{
return 0;
}
// the node is a leaf
if (_node->left_child == nullptr && _node->right_child == nullptr)
{
return 1;
}
int left_count = countLeaves(_node->left_child);
int right_count = countLeaves(_node->right_child);
return left_count + right_count;
template<typename KeyType, typename ValueType>
inline void PersistentAssociativeArray<KeyType, ValueType>::printSliceRecursive(AA_node<KeyType, ValueType>* _root, int _left, int _right)
{
// Empty case
if (_root == nullptr)
{
return;
}
int key_index{};
for (int i = 0; i < keys.size(); i++)
{
if (_root->key == keys.at(i))
{
key_index = i;
break;
}
}
key_index += 1;
// Key is in the range of left and right
if (key_index >= _left && key_index <= _right)
{
std::cout << _root->key << " : " << _root->value << std::endl;
}
if (key_index > _right) // Left subtree case
{
printSliceRecursive(_root->left, _left, _right);
}
else if (key_index < _left) // Right subtree case
{
printSliceRecursive(_root->right, _left, _right);
}
else // Key is in the range of left and right
{
printSliceRecursive(_root->left, _left, _right);
printSliceRecursive(_root->right, _left, _right);
}
template<typename KeyType, typename ValueType>
inline void PersistentAssociativeArray<KeyType, ValueType>::addVersion(int _root_position, KeyType _key, ValueType _new_value)
{
int position{};
for (size_t i = 0; i < keys.size(); i++)
{
if (_key == keys.at(i))
{
position = i;
}
}
_root_position += 1;
position += 1;
if (_root_position > versions.size())
{
return;
}
int l = 1;
int r = versionSize(_root_position);
if (position > r)
{
return;
}
versions.push_back(new Node<ValueType>());
addValue(versions.back(), versions.at(_root_position - 1), l, r, position, _new_value);
auto N = new Node<KeyType>();
N = key_versions.at(_root_position - 1);
key_versions.push_back(N);
std::pair<int, int> temp = { _root_position - 1, versions.size() - 1 };
parent_children_relation.push_back(temp);
}
template<typename KeyType, typename ValueType>
inline void PersistentAssociativeArray<KeyType, ValueType>::printSlice(AA_node<KeyType, ValueType>* _current, int _left, int _right)
{
// Empty case
if (_current == nullptr)
{
return;
}
int key_index{};
for (int i = 0; i < keys.size(); i++)
{
if (_current->key == keys.at(i))
{
key_index = i;
}
}
key_index += 1;
// Key is in the range of left and right
if (key_index >= _left && key_index <= _right)
{
std::cout << _current->key << " : " << _current->value << std::endl;
}
if (key_index > _right) // Left subtree case
{
printSliceRecursive(_current->left, _left, _right);
}
else if (key_index < _left) // Right subtree case
{
printSliceRecursive(_current->right, _left, _right);
}
else // Key is in the range of left and right
{
printSliceRecursive(_current->left, _left, _right);
printSliceRecursive(_current->right, _left, _right);
}
template
inline int PersistentArray::countLeaves(Node* _node)
{
if (_node == nullptr)
{
return 0;
}
// the node is a leaf
if (_node->left_child == nullptr && _node->right_child == nullptr)
{
return 1;
}
int left_count = countLeaves(_node->left_child);
int right_count = countLeaves(_node->right_child);
return left_count + right_count;
}
template
inline void PersistentArray::printSlice(T* _arr, int _left, int _right)
{
//std::cout << "in printArr with: size = " << _right - _left + 1 << "\n";
if (_arr == nullptr)
{
std::cout << "nullptr\n";
return;
}
std::cout << "arr:\t";
for (int i = _left - 1; i < _right; i++)
{
std::cout << _arr[i] << "\t";
}
std::cout << "\n";
}
template
inline PersistentArray::PersistentArray(int _size)
{
// Create the base version
this->base = new T[_size];
// Set size of base version
this->size = _size;
// Add new root to versions
versions.push_back(new Node<T>());
// From fisrt element to the last (=_size)
int left_border = 1;
int right_border = _size;
T* arr = base;
// Build tree with arr
buildTree(versions.at(0), arr, left_border, right_border);
std::pair<int, int> temp = { -1, versions.size() - 1 };
parent_children_relation.push_back(temp);
}
template
inline PersistentArray::PersistentArray(T* _arr, int _size)
{
if (_arr == nullptr)
{
throw std::invalid_argument("Empty array");
}
// Create the base version
this->base = new T[_size];
// Set size of base version
this->size = _size;
// Fill base with elements of _arr
for (int i = 0; i < _size; i++)
{
this->base[i] = _arr[i];
}
// Add new root to versions
versions.push_back(new Node<T>());
// From fisrt element to the last (=_size)
int left_border = 1;
int right_border = _size;
T* arr = base;
// Build tree with arr
buildTree(versions.at(0), arr, left_border, right_border);
std::pair<int, int> temp = { -1, versions.size() - 1 };
parent_children_relation.push_back(temp);
template
inline void PersistentArray::setElementToBase(int _index, T _value)
{
if (_index >= 0 && _index < size)
{
base[_index] = _value;
changeVersion(0, _index, _value);
}
else
{
throw std::out_of_range("Index out of borders");
}
}
template
inline void PersistentArray::changeVersion(int _number_of_version, int _change_index, T _new_value)
{
bool child_exists = false;
for (const auto& pair : parent_children_relation)
{
if (pair.first == _number_of_version)
{
child_exists = true;
std::cout << "Impossible to change a version which has children. ";
std::cout << "Version [" << pair.first << "] has at least one child [" << pair.second << "]\n";
return;
}
}
_number_of_version += 1;
_change_index += 1;
if (_number_of_version > versions.size())
{
return;
}
int left_border = 1;
int right_border = versionSize(_number_of_version);
if (_change_index > right_border)
{
throw std::out_of_range("Index out of borders");
}
add(versions[_number_of_version - 1], versions.at(_number_of_version - 1), left_border, right_border, _change_index, _new_value);
}
template
inline void PersistentArray::addVersion(int _root_position, int _change_index, T _new_value)
{
_root_position += 1;
_change_index += 1;
if (_root_position > versions.size())
{
return;
}
int left_border = 1;
int right_border = versionSize(_root_position);
if (_change_index > right_border)
{
throw std::out_of_range("Index out of borders");
}
versions.push_back(new Node<T>());
add(versions.back(), versions.at(_root_position - 1), left_border, right_border, _change_index, _new_value);
std::pair<int, int> temp = { _root_position - 1, versions.size() - 1 };
parent_children_relation.push_back(temp);
template
inline void PersistentDoublyLinkedList::initDL(T _data)
{
// Create an empty node
base = new DL_node();
// Assign data to the head node
base->value = _data;
// Assign prev pointer to the head node
base->prev = nullptr;
// Assign next pointer to the head node
base->next = nullptr;
}
template
inline int PersistentDoublyLinkedList::countLeaves(Node* _node)
{
if (_node == nullptr)
{
return 0;
}
// the node is a leaf
if (_node->left_child == nullptr && _node->right_child == nullptr)
{
return 1;
}
int left_count = countLeaves(_node->left_child);
int right_count = countLeaves(_node->right_child);
return left_count + right_count;
}
template
inline PersistentDoublyLinkedList::PersistentDoublyLinkedList(T _data)
{
// Making a single node with data
/DL_node head = new DL_node();
head->data = _data;
head->prev = nullptr;
head->next = nullptr;
// Assign it to base
this->base = head;*/
initDL(_data);
this->size = 1;
// New root of version
versions.push_back(new Node<T>());
// From fisrt element to the last (= 1)
int left_border = 1;
int right_border = 1;
buildTree(versions.at(0), this->base, left_border, right_border);
std::pair<int, int> temp = { -1, versions.size() - 1 };
parent_children_relation.push_back(temp);
}
template
inline PersistentDoublyLinkedList::PersistentDoublyLinkedList(T* _arr, int _size)
{
// Assign empty node to base
this->base = nullptr;
this->size = _size;
// New root of version
versions.push_back(new Node<T>());
// Fill base list
if (_size > 0)
{
initDL(_arr[0]);
for (int i = 1; i < _size; i++)
{
append(_arr[i]);
}
}
// From fisrt element to the last (=_size)
int left_border = 1;
int right_border = _size;
buildTree(versions.at(0), this->base, left_border, right_border);
std::pair<int, int> temp = { -1, versions.size() - 1 };
parent_children_relation.push_back(temp);
template
inline void PersistentDoublyLinkedList::append(T _data)
{
// Create the new node of the doubly linked list
DL_node* new_node = new DL_node();
// Assign data to the new node
new_node->value = _data;
// Assign next pointer to nullptr
new_node->next = nullptr;
// If linked list is empty
if (base == nullptr)
{
// This is the only node in the list
new_node->prev = nullptr;
}
else
{
// Iterate to find the last node in the list
DL_node<T>* end_node = base;
while (end_node->next != nullptr)
{
end_node = end_node->next;
}
// Assign the new node as the next node of the last node
end_node->next = new_node;
// Assign the previous pointer of the new node to the last node
new_node->prev = end_node;
}
this->size += 1;
// calculate the left and right borders
int left_border = 1;
int right_border = this->size;
buildTree(versions.at(0), this->base, left_border, right_border);
}
template
inline void PersistentDoublyLinkedList::printSlice(DL_node* _head, int _left, int _right)
{
// Create a temporary node
DL_node* temp_node = _head;
int counter = 1;
// Iterate through the list until the temporary node != null and the count is < than the left boundary
while (temp_node != nullptr && counter < _left)
{
// Move to the next node
temp_node = temp_node->next;
// Increase counter
counter++;
}
//std::cout << "\nin printSlice(DL_node* _head, int _left, int _right): counter = " << counter << "\t";
// Iterate through the list until the temporary node != null and the count is <= than the right boundary
while (temp_node != nullptr && counter <= _right)
{
// Print data
std::cout << temp_node->value << "\t";
// Move to the next node
temp_node = temp_node->next;
// Increase counter
counter++;
}
//std::cout << counter << std::endl;
std::cout << std::endl;
}
template
inline int PersistentDoublyLinkedList::getSize(DL_node* _head)
{
int count = 0;
DL_node* temp_node = _head;
template
inline T PersistentDoublyLinkedList::getElement(DL_node* _head, int _index)
{
DL_node* temp_node = _head;
int count = 0;
while (temp_node != nullptr && count < _index)
{
temp_node = temp_node->next;
count++;
}
if (temp_node != nullptr)
{
return temp_node->value;
}
else
{
//std::cout << "Index out of bounds" << std::endl;
return -1;
}
}
template
inline void PersistentDoublyLinkedList::changeVersion(int _number_of_version, int _new_position, T _new_value)
{
bool child_exists = false;
for (const auto& pair : parent_children_relation)
{
if (pair.first == _number_of_version)
{
child_exists = true;
std::cout << "Impossible to change a version which has children. ";
std::cout << "Version [" << pair.first << "] has at least one child [" << pair.second << "]\n";
return;
}
}
Node<T>* root = versions[_number_of_version - 1];
auto version_size = versionSize(_number_of_version);
DL_node<T>* head = nullptr;
DL_node<T>* tail = nullptr;
for (int i = 1; i <= version_size; i++)
{
int value = get(root, 1, version_size, i);
// Create a new Node
DL_node<T>* new_node = new DL_node<T>();
new_node->value = value;
new_node->next = nullptr;
// If there is only one Node
if (head == nullptr)
{
head = new_node;
tail = new_node;
new_node->prev = nullptr;
}
else // Add Node to the end
{
new_node->prev = tail;
tail->next = new_node;
tail = new_node;
}
}
return head;
template
inline int Convert::nodeSize(Node* _node)
{
if (_node == nullptr)
{
return 0;
}
// the node is a leaf
if (_node->left_child == nullptr && _node->right_child == nullptr)
{
return 1;
}
int left_count = nodeSize(_node->left_child);
int right_count = nodeSize(_node->right_child);
return left_count + right_count;
}
template
inline PersistentArray Convert::toArray(Node* _version)
{
auto version_size = nodeSize(_version);
ValueType* array = new ValueType[version_size];
for (int i = 1; i <= version_size; i++)
{
array[i - 1] = get(_version, 1, version_size, i);
}
PersistentArray<ValueType> result(array, version_size);
delete[] array;
return result;
}
template
inline PersistentDoublyLinkedList Convert::toDLList(Node* _version)
{
auto version_size = nodeSize(_version);
ValueType* array = new ValueType[version_size];
for (int i = 1; i <= version_size; i++)
{
array[i - 1] = get(_version, 1, version_size, i);
}
PersistentDoublyLinkedList<ValueType> result(array, version_size);
delete[] array;
return result;