About Store Forum Documentation Contact



Post Reply 
Map function dummie questions
Author Message
KraaxLeNain Offline
Member

Post: #1
Map function dummie questions
Hi all,

I'm getting my hands and mind dirty into processing code with this shiny Esenthel right here... So far code has been quite intuitive and pleasant to write, but sometimes well it doesn't.

I'm trying here to use the function "Map<Key,Data>" wich I believe is far more efficient when you need to retrieve specific objects in a huge list.

Problem is :
  • I can't initiate it properly.
  • I just don't get how to add a new element... Get an old one hell yeah, destroy one no problem... But add a new one no way.

Initiation:
When I type something like (with NPC a class I created) :
"map<Int,NPC> npcs;" I get an error "No default constructor for map with [...] [Key = Int , Data=NPC]" ... So the code understand what I'm trying to do but it seems that I don't understand what he wants me to do (Code 1, Kraax 0). What am I missing here folks ?

Create a NewElement
If I use MemC instead of map, all I need to do is "npc.new().NpcConstructor(a,b,c)"... With map I juste don't get from the code description what I need to do to make it right.... Well there is get() wich seems to be able to create an element if missing but I'll have problem since constructor need parameters.

All these Dummy questionsblushing... Please don't laugh !
(This post was last modified: 12-10-2013 12:51 PM by KraaxLeNain.)
12-10-2013 09:33 AM
Find all posts by this user Quote this message in a reply
laugan Offline
Member

Post: #2
RE: Map function dummie questions
I faced the same issue and didn't succeed in solving it, so i'm ising std::map<> and std::multimap<> now.
12-10-2013 12:17 PM
Find all posts by this user Quote this message in a reply
Rubeus Offline
Member

Post: #3
RE: Map function dummie questions
Look at the Memory/Maps tutorial. It needs to be initialized with the constructor args.
Map<Key, Data> map(Compare, Create, null); as is used in the tutorial.
The simplest solution would be to use map<Int,NPC> npcs(Compare, null, null); but you may want to create your own create methods.
12-11-2013 07:52 PM
Find all posts by this user Quote this message in a reply
laugan Offline
Member

Post: #4
RE: Map function dummie questions
There are situations sometimes when i don't need creation or comparisson, i just need dynamically put and take information from map. EE::Map is not so flexible as STL map.
12-12-2013 11:53 AM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #5
RE: Map function dummie questions
(12-12-2013 11:53 AM)laugan Wrote:  There are situations sometimes when i don't need creation or comparisson, i just need dynamically put and take information from map. EE::Map is not so flexible as STL map.
You can set NULL for create, but you -do need- a comparison function, that's the point of Map, to have items sorted for quick access.
12-15-2013 10:26 PM
Find all posts by this user Quote this message in a reply
KraaxLeNain Offline
Member

Post: #6
RE: Map function dummie questions
Well for my part I'm finally getting to use maps, it's just that the way to use them seemed rather quite disturbing at first glance : problem was storing a new object without creating it, when you object need a create function with several parameters. I sorted it out by creating a pointer, initialize it with a .get() (which stands for .new() apparently, well kind of):


Code:
DATA::void CreateData(A a,B b,C c,D d)
{[initialization stuff]}

Bool CreateFunctionForMap (&Data, &Key, Ptr)
{ return true;} //Can't create the data we want since no place for entering create parameters (a,b,c,d)

Map<Key,Data> MyMap(Compare,CreateFunctionForMap,NULL);

DATA *TempPtr;
TempPtr=MyMap.get(key1); //Get pointer to a map memory place
TempPtr.CreateData(a,b,c,d) //Finally create the object.

This kind of approache is now working for me but... Maybe I can do better... I have several noob questions if some of you now the answers, please share smile

  1. Can I make quickercode by using directly something like (MyMap.get(key1)).Create(a,b,c,d) ?
    Or DATA temp;
    temp.create(a,b,c,d);
    *MyMap.get(key1)=temp;
  2. What exactly happens if I lock the Map? Other request of accessing the element for modification will be queued or waiting ? Should I avoid to lock data everytime I access the map or on the contrary should I lock it everytime I need to read/write in it ? If I'm guessing right this lock is designed to prevent multi-thread crash ?
  3. I tryed to do something like : void Function (Int a, Str b, Map<Key,Data> MyMap(Compare,Create,NULL))
    {
    [do stuff]
    }
    It compiles but when I try to call it with for example :
    Map<Key,Data> MyMap2(Compare,Create,NULL);
    Function(1,"test",MyMap2)

    I get an error like "Map does not take 1 argument". Is it forbiden to use map in functions or did I missed a subtility ?
  4. On the create function, do I need to check if the KEY is already taken or should I assume that it will be called only if element not found ?
  5. What's the use of the pointer Ptr define in the Map architecture ? In which case should I bother to manage it ?
  6. Is there a default size limitation ? I have created objects seen on the player screen which I stored in the map. For some strange reason when I spawn plenty of them, some of them disappears... I don't know why.

Yeah dummies got plenty questions smile
(This post was last modified: 12-16-2013 10:53 AM by KraaxLeNain.)
12-16-2013 10:52 AM
Find all posts by this user Quote this message in a reply
Rubeus Offline
Member

Post: #7
RE: Map function dummie questions
For #1, it looks like you can replace the .get(key1) with just MyMap(key1).create().
For #2, that's the usual reason for using a lock function.
For #3, it would probably be more efficient to just pass individual elements instead of the whole container.
For #4, if you use MyMap(key1).create(), it will overwrite if it exists, so you will want to write a check into it to see if it exists.
12-17-2013 02:44 AM
Find all posts by this user Quote this message in a reply
MrPi Offline
Member

Post: #8
RE: Map function dummie questions
4) Well, you can decide whether to use the get() or the find() function on the map. get() creates the object if it's not in the map yet. I find that very comfortable.

5) The ptr is for passing user data to your create function. I didn't really use that yet with a map, but in certain scenarios I can imagine it being useful.
12-20-2013 11:14 AM
Find all posts by this user Quote this message in a reply
Post Reply