I have one use case that I need to save the data into a Map. I'm trying to use the concurrentMap
I need to update/extract the value of the record later so that I have to use a Map(Queue, List won't work because I can't extract the value when the collection it self is updated).
Now my question is: Since I need to check the size of the Map before executing, how could I do that? Do I have to wrap map with synchronized or try to lock it? Is there any other way I could do that?
Related
I'm going to use the same value in lots of statements in the SQL Expression. So it is possible to declare and assign the value to a variable at the beginning of the query and refer the value by it?
(I'm writing an execution plan in WSO2 DAS)
This is not supported as of now. However, supporting this has been under discussion, hence this might be implemented in a future release.
If you want to store a value and use it in a query, the currently available ways are:
Putting that value into an indexed event table and then doing a join with the event table to read that value whenever required.
Indexed In-memory Event Table internally uses a Hash-Map, therefore you could use one to store your variables, in such a way that the key of the hashmap will be the name of your varaible and the value of the hashmap will be the value of your variable.
However I feel that above solution is too complicated for your requirement.
Using the Map Extension in Siddhi
I want to generate an event when the vector changes state from empty to non-empty or from non-empty to empty.
What is the easiest way to check this ?
Create a class that wraps a vector. In insert remove operations, add checks for your transition.
Write or find an event framework. Fire said events when you want them to occur. Subscribe where you want to recieve.
vector is a light weight class that solves the problem of a dynamic, resizable array of contiguous elements well. It does not contain event hooks: std does not make you pay for things you do not use (and most use cases do not require event hooks).
I am trying to determine if an element exists in a boost::heap::binomial_heap because I need to know if I should call update() (if the node already exists) or push() (if the node does not exist). Some queues provide a push_or_update() function for exactly this purpose. The only thing I could figure out to do is keep a property map with the same index type as the nodes in the queue and value_type 'handle_t'. Then I can lookup in the map if the item has a valid handle so that I can push if it does not, or update if it does.
Is there a better way to do this?
Here is the doc for reference.
This is not something a binomial heap is supposed to do.
A usual way to solve your problem would be: use a hash map (or other data structure you like) to store a mapping between values and handles. You can then query the hash map for the handle. If it exists, this handle will let you modify the value in the heap. If it doesn't exist, you can just add a new value to the heap (of course, and a new mapping in the hash map)
Another way to solve the problem is to use a tree set/map, which is easier and may be more efficient than the solution I described above, depending on the actual use case.
I want to save some files in a particular folder (/tmp/) using C++ code.
Before store the file
I need to check whether enough memory is available or not. If not, I need to delete the oldest file from the storage and then I need to store a new one.
I also need to know the stored file details in my code to access them.
My implementation is:
std::map<string IfileName, int iDetail>
I created a Map instance and whenever I create a file data, I stored the details in the map.
Problems with the map are:
Map automatically sort the filename. So I am not able to identify the oldest file.
Suppose, file is deleted in the storage, then also map has the detail about that file.
For the first your question try to read here. And about second part of your question. Why you do not want to use vector of pairs. In this case you can use push_back.
At some point I used a combination of a map and deque to solve a similar problem. The trick is that each value_type (in both map and deque) need to be extended with cross pointers. So that when you run map.find() using a certain key, the value that you receive has a pointer to an element in the deque. And vice versa. To locate the oldest (LRU) file you just execute deque.pop_back(), dereference the pointer that you get into the map, view the details and decide if you need to remove one more file or not.
I need to use multiple keys(int type) to store and retrieve a single value from a hash table. I would use multiple key to index a single item. I need fast insertion and look up for the hash table. By the way, I am not allowed to use the Boost library in the implementation.
How could I do that?
If you mean that two ints form a single key then unordered_map<std::pair<int,int>, value_type>. If you want to index the same set of data by multiple keys then look at Boost.MultiIndex.
If the key to your container is comprised of the combination of multiple ints, you could use boost::tuple as your key, to encapsulate the ints without more work on your part. This holds provided your count of key int subcomponents is fixed.
Easiest way is probably to keep a map of pointers/indexes to the elements in a list.
A few more details are needed here though, do you need to support deletion? how are the elements setup? Can you use boost::shared pointers? (rather helpful if you need to support deletion)
I'm assuming that the value object in this case is large, or there is some other reason you can't simply duplicate values in a regular map.
If its always going to be a combination for retrieval.
Then its better to form a single compound key using multiple keys.
You can do this either
Storing the key as a concatenated string of ints like
(int1,int2,int3) => data
Using a higher data type like uint64_t where in u can add individual values to form a key
// Refer comment below for the approach