std::cout << "TESTING SIMPLIFIED PERSISTENT ARRAY\n";
PersistentArray<int> my_persistent_array(values, values_size); // Version[0] is made of values
my_persistent_array.addVersion(0, 2, -250); // Version[1]
my_persistent_array.addVersion(1, 3, -150); // Version[2]
my_persistent_array.printAllVersions();
my_persistent_array.undo(); std::cout << "\tundo\n"; // reject last version
my_persistent_array.printAllVersions(); std::cout << "\n";
my_persistent_array.redo(); std::cout << "\tredo\n"; // repeat last action
my_persistent_array.printAllVersions(); std::cout << "\n";
std::cout << "\n==============================================\n";
std::cout << "TESTING SIMPLIFIED PERSISTENT DOUBLY LINKED LIST\n";
PersistentDoublyLinkedList<int> my_persistent_list(values, values_size);
my_persistent_list.printAllVersions();
my_persistent_list.addVersion(0, 2, -250); my_persistent_list.printAllVersions();
my_persistent_list.addVersion(1, 3, -150);
my_persistent_list.printAllVersions();
my_persistent_list.undo(); std::cout << "\tundo\n"; // reject last version
my_persistent_list.printAllVersions(); std::cout << "\n";
my_persistent_list.redo(); std::cout << "\tredo\n"; // repeat last action
my_persistent_list.printAllVersions(); std::cout << "\n";
std::cout << "\n==============================================\n";
std::cout << "TESTING SIMPLIFIED PERSISTENT ASSOCIATIVE ARRAY\n";
//std::vector<std::string> keys = { "one", "two", "three" };
//PersistentAssociativeArray<std::string, int> myPersistentMap(keys, values, values_size);
//myPersistentMap.printAllVersions();
std::vector<std::string> keys = { "zero", "one", "two", "three", "four" };
//int values_array[] = { 10, 20, 30 };
//size_t values_array_size = sizeof(values_array) / sizeof(values_array[0]);
try {
PersistentAssociativeArray<std::string, int> assoc_array(keys, values, values_size);
assoc_array.printAllVersions(); // Вызов функции печати для проверки
// Добавляем новую версию, изменяя ключ 2 на значение 25
assoc_array.addVersion(0, "two", -250);
assoc_array.printAllVersions();
assoc_array.addVersion(1, "three", -150);
assoc_array.printAllVersions();
assoc_array.undo(); std::cout << "\tundo\n"; // reject last version
assoc_array.printAllVersions(); std::cout << "\n";
assoc_array.redo(); std::cout << "\tredo\n"; // repeat last action
assoc_array.printAllVersions(); std::cout << "\n";
}
catch (const std::invalid_argument& e) {
std::cerr << "Ошибка: " << e.what() << std::endl;
}
catch (const std::out_of_range& e) {
std::cerr << "Ошибка: " << e.what() << std::endl;
}
std::cout << "\n==============================================\n";
std::cout << "TESTING CONVERTATION\n";
//double values2[] = { 100.1, 200.2, 300.3, 400.4, 500.5 };
//int values2_size = sizeof(values2) / sizeof(double);
std::vector<double> values2 = { 100.1, 200.2, 300.3, 400.4, 500.5 };
PersistentArray<double> array_to_convert(values2, values2.size());
array_to_convert.printAllVersions();
PersistentDoublyLinkedList<double> dll_from_array = Convert<double>::convertArrayToList(array_to_convert, 0);
dll_from_array.printAllVersions();
std::vector<double> values3 = { -100.1, -200.2, -300.3, -400.4, -500.5 };
PersistentDoublyLinkedList<double> dll_to_convert(values3, values3.size());
dll_to_convert.printAllVersions();
PersistentArray<double> array_from_dll = Convert<double>::convertListToArray(dll_to_convert, 0);
array_from_dll.printAllVersions();
return 0;
The provided C++ code appears to be a set of tests for three different data structures: a persistent array, a persistent doubly linked list, and a persistent associative array. In addition, there are conversion operations between arrays and lists for persistent data. Below are some insights and a brief explanation of the code:
Key Components of the Code:
-
Persistent Data Structures:
- Persistent Array: This allows for creating versions of the array with modifications like additions. You can undo or redo changes made to the array.
- Persistent Doubly Linked List: Similar to the persistent array, but it's implemented as a doubly linked list. This allows for efficient insertions and deletions between nodes while keeping track of versions.
- Persistent Associative Array: This is a mapping between keys (strings in this case) and values (integers) where each version can be modified just like the previous structures.
-
Conversion Functions:
- The code includes functions to convert between persistent arrays and persistent linked lists, enabling flexible data manipulations.
-
Testing in main() Function:
- The
main() function initializes instances of these data structures with predefined values.
- It performs a series of operations such as adding new versions, undoing changes, redos, and printing the contents of each structure after various operations to observe changes.
- Error handling is included to catch invalid operations, such as adding a version for a key that does not exist.
Structure of the Code:
- Start with including the necessary header files.
- Define the
main() function, where:
- Initialize Data: An array of integers and corresponding operations on an array, a doubly linked list, and an associative array.
- Perform Operations: The code adds, undoes, and redoes operations on the persistent structures, printing the state after each operation for clarity.
Example of Adding Versions:
- For the
PersistentArray, calling addVersion(0, 2, -250) suggests modifying the value at index 2 of the original array (version 0) to -250. The result is seen in the next call to printAllVersions().
Example of Error Handling:
- The code includes a
try-catch block that captures any exceptions thrown during operations on the PersistentAssociativeArray, which helps in diagnosing issues such as out-of-range access or invalid arguments.
Conversion Example:
- The code performs conversion from an array of doubles to a doubly linked list and vice versa, showcasing versatility in how data can be structured and manipulated.
Conclusion:
This code effectively demonstrates the usage of persistent data structures in C++. If you implement the related classes (PersistentArray, PersistentDoublyLinkedList, PersistentAssociativeArray, and Convert), ensure that you handle versioning and state management correctly to reflect expected results during testing. If you'd like to know more specific details about any part of the implementation or seek enhancements, feel free to ask!