Hello, and I have this problem again. I need to make my container look like a map, but storing only the key in memory, and value writing to a file whose name is the key.
With mymap[999] = 100; 999 goes to the AVL tree, and then a file is created with the name 999, and the value 100 is stored in it.
the problem is what I need in order to be able to do this:
I cannot change anything in the modify method.
void modify(int& a) {// базовый метод a += 5; } //Желаемый функционал myMap[2] = 20; int c = 30; myMap[3] = с; modify(myMap[2]); //меняет значение myMap[2] int a = myMap[1]; int b = myMap[2]; assert(b == 25);//проверяем изменилось ли myMap[2] = myMap[2] + 20;//снова меняем значение With modify(myMap[2]); , the value in myMap[2] should also change, and therefore in the file.
I understand that for mymap[999] = 100; need a proxy class returned by the container. But for modify(myMap[2]); and myMap[2] = myMap[2] + 20; proxy does not fit. It is possible that all proxies of the same key can have shared memory, but I do not know how.
The container itself now looks like this:
template <typename Key, typename FileType, class Compare = std::less<Key>> class PersistentMap { public: typedef int size_type; typedef std::pair<Key, FileType> value_type; typedef FileType mapped_type; typedef AVL_Tree<Key, FileType> tree; private: std::string dirName; tree *strom; Compare comp; public: typedef typename tree::iterator iterator; typedef typename tree::const_iterator const_iterator; PersistentMap(std::string ag); const mapped_type operator[] (Key key); std::pair<iterator, bool> insert(const value_type& a); //... } I will be glad to advice how to implement it (perhaps an example).
A very long time I can not solve this problem.
UPD can create a stack in which there will be values, and after destroying the last instance of the proxy of this key, save the value? (I hope clearly explained the problem)
UPD_2
I know that at the moment the code is terribly structured, and where it is not logical. PersistentMap.hpp consists of PersistenMap+AVL_Tree I just divided them into 2 examples, because Avl right now does not matter, as they are in lib namespace.
At the moment I have with modify(myMap[2]); the value in myMap[2] increases, but with myMap[2] = myMap[2] + 20; the problem is, since the left object of the destructor collapses before the right, and is overwritten.
PS
Proxy related tasks:
You can return an instance of the proxy class that takes care of saving / loading the correct values.
Импimplement implicit conversion operators
Импinput the constructors, assignment operators
The solution will be correct if multiple proxies share a common data store.
modifymethod, your task has no solution. - ixSci