Ensure map size does not grow too much
Moderator: Martin
-
- Posts: 33
- Joined: 08 Mar 2013 12:45
Ensure map size does not grow too much
hi.any idea how to solve this, to prevent the map size not too grow to much.I wanted my mapsize to contain only 10pairs of key/value and my priority is to remove the old values first. adv thanks.
Re: Ensure map size does not grow too much
What you're looking for is essentially a circular buffer - those are easier to implement with lists, as indexed addressing is possible with those - but they don't provide the hashed addressing offered by maps. Is the key to value lookup important to you?
-
- Posts: 33
- Joined: 08 Mar 2013 12:45
Re: Ensure map size does not grow too much
The map contains contact name and numbers and from time to time it grows.So that i want it to maintain it to 10-20 only. Im creating a flow that when i access and view the map using Input dialog, it will show only the 10 most recent contact names..
Im sorry i cant explain it clearly and also for bad english.
Im sorry i cant explain it clearly and also for bad english.

Re: Ensure map size does not grow too much
A map has no concept of "recent" or chronological sequence of additions. Only thing I can currently think of is an additional list, used to keep map keys, index addressed, with a wrap around index. Before a list item gets reused (overwritten), it is read, and the corresponding map element removed.
Maybe there's a better solution somebody else can think of, or I get another idea. What I do in similar cases is appending to file, and display tail -n lines from that file - but whether you could imagine replacing hashed map lookup against a grep on that file is something I don't know.
Maybe there's a better solution somebody else can think of, or I get another idea. What I do in similar cases is appending to file, and display tail -n lines from that file - but whether you could imagine replacing hashed map lookup against a grep on that file is something I don't know.
-
- Posts: 33
- Joined: 08 Mar 2013 12:45
Re: Ensure map size does not grow too much
Thanks..
I got the idea from this flow made by Martin. I want to tweak it to create other flow but map will grow for sure.
Sorry i dont know how to post that topic. Search "send sms by timeframe"
http://automagic4android.com/flow.php?i ... 151bf39282
I got the idea from this flow made by Martin. I want to tweak it to create other flow but map will grow for sure.
Sorry i dont know how to post that topic. Search "send sms by timeframe"
http://automagic4android.com/flow.php?i ... 151bf39282
Re: Ensure map size does not grow too much
The maps used by Automagic keep the iteration order of elements (insertion order) since it's often easier to understand and debug a map when the entries are not ordered "randomly".
However, re-inserting an element does not affect the order:
The map will contain following entries:
a->4
b->2
c->3
When you overwrite entries, you could change your script to always remove the entry first and then insert the entry to the map:
which yields:
b->2
c->3
a->4
You could use this special behavior to remove the first (oldest) element from the map:
However, re-inserting an element does not affect the order:
Code: Select all
map = newMap();
addMapEntry(map, "a", 1);
addMapEntry(map, "b", 2);
addMapEntry(map, "c", 3);
addMapEntry(map, "a", 4);
a->4
b->2
c->3
When you overwrite entries, you could change your script to always remove the entry first and then insert the entry to the map:
Code: Select all
map = newMap();
addMapEntry(map, "a", 1);
addMapEntry(map, "b", 2);
addMapEntry(map, "c", 3);
removeMapEntry(map, "a");
addMapEntry(map, "a", 4);
b->2
c->3
a->4
You could use this special behavior to remove the first (oldest) element from the map:
Code: Select all
if (length(map) > 10)
{
firstKey = getMapKeys(map)[0];
removeMapEntry(map, firstKey);
}