Ensure map size does not grow too much

Post your questions and help other users.

Moderator: Martin

Post Reply
houdinix64
Posts: 33
Joined: 08 Mar 2013 12:45

Ensure map size does not grow too much

Post by houdinix64 » 21 Jun 2016 18:19

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.

User avatar
Bushmills
Posts: 286
Joined: 23 Sep 2014 21:56

Re: Ensure map size does not grow too much

Post by Bushmills » 21 Jun 2016 18:43

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?

houdinix64
Posts: 33
Joined: 08 Mar 2013 12:45

Re: Ensure map size does not grow too much

Post by houdinix64 » 21 Jun 2016 19:07

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. ✌️

User avatar
Bushmills
Posts: 286
Joined: 23 Sep 2014 21:56

Re: Ensure map size does not grow too much

Post by Bushmills » 21 Jun 2016 19:33

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.

houdinix64
Posts: 33
Joined: 08 Mar 2013 12:45

Re: Ensure map size does not grow too much

Post by houdinix64 » 21 Jun 2016 19:45

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

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Ensure map size does not grow too much

Post by Martin » 21 Jun 2016 20:13

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:

Code: Select all

map = newMap();
addMapEntry(map, "a", 1);
addMapEntry(map, "b", 2);
addMapEntry(map, "c", 3);
addMapEntry(map, "a", 4);
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:

Code: Select all

map = newMap();
addMapEntry(map, "a", 1);
addMapEntry(map, "b", 2);
addMapEntry(map, "c", 3);
removeMapEntry(map, "a");
addMapEntry(map, "a", 4);
which yields:
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);
}

Post Reply