Dynamic growth of Map

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Dynamic growth of Map

Post by Trond »

Dynamic map growth
-----------------------------

To have to choose the number of slots beforehand is a big limitation with maps. So I suggest an improvement, which will allow seamless growth of maps. Instead of allowing just a single map, PB maps should create more and bigger maps behind the scenes, when the current map is full, according to the following algorithm:

NewMap creates a list of "blocks", where each block is just a hashmap. Initially, this list has exactly one element with the number of slots specified in the creation (or the default number of slots).

Insertion:

Code: Select all

Get the first block in the list.
If the block is less than 75% (not sure of optimal fullness) full
    insert the item as normal.
else
    add a new block as the first element of the list
    this new block should be bigger than the previous, according to the following growth curve:
    128 -> 256 -> 512 -> 1024 -> 2048 -> 4096 -> ... (the powers of two), until a point where the new blocks no longer grows so fast (it makes no sense to allocate a 2 GB block, if your current is 1 GB, a limit of 1 000 000 or something should be enforced.
    Insert the item in this new block.
Lookup:

Code: Select all

Starting before the start of the list:
repeat
    Get the next block in the list
    check if the key can be located, if yes, return it
    if no:
until past_end_of_list
return: element not found
User avatar
Fig
Enthusiast
Enthusiast
Posts: 351
Joined: Thu Apr 30, 2009 5:23 pm
Location: Côtes d'Azur, France

Re: Dynamic growth of Map

Post by Fig »

Image

Clustered hastable ?
There are 2 methods to program bugless.
But only the third works fine.

Win10, Pb x64 5.71 LTS
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Dynamic growth of Map

Post by Mistrel »

What you're asking for is a complete replacement of PureBasic's map library to fit a niche requirement. There is no one solution and there are already other issues such as keys being limited to strings.

I think the current implementation is good for most use cases. If you want something to solve a specific problem then you can always write one yourself. Since we don't have a form of generics, PureBasic native containers are pretty limiting anyways.

What problem are you encountering that you need to optimize PureBasic's map?
User avatar
NicTheQuick
Addict
Addict
Posts: 1226
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Dynamic growth of Map

Post by NicTheQuick »

When you change the number of blocks you normally have to rehash all the data in the map again because it can move to a different position as before.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Post Reply