SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Plugin Development

Reply
 
Thread Tools Display Modes
Old 31/12/2017, 11:34 AM   #1
ShapeGaz
Little Clucker
 
Join Date: Sep 2017
Posts: 19
Reputation: 4
Default unordered_map

unordered_map for SA:MP


Description
This plugin allows you working with unordered_map

Natives
PHP Code:
native map:Map_New(type);
native Map_Emplace(typemap:mapkeystring[]="",keyint=0,valuestring[]="",valueint=0);
native Map_Find(typemap:mapkeystring[]=""keyint=0dest[]=""size=sizeof dest);
native Map_Size(typemap:map);
native Map_Clear(typemap:map);
native Map_Erase(typemap:mapkeystring[]=""keyint=0);
native Map_Empty(typemap:map);
native Map_Load_Factor(typemap:map);
native Map_Bucket_Count(typemap:map); 
Example code
PHP Code:
#include <unordered_map>
main()
{
  
/*
  Types:
  Map_SS = <string,string>
  Map_II = <int,int>
  Map_SI = <string, int>
  Map_IS = <int,string>
  */

  // example code with <string, string>
  
new map:Map Map_New(Map_SS); // Create a new map
  
Map_Emplace(Map_SSMap"hello", .valuestring="hi"// Insert a new element
  
new value[3];
  if(
Map_Find(Map_SSMap"hello", .dest=value) != -1// Search our element
  
{
    
printf("Value: %s",value); // Output: Value: hi
  
}

  
// example code with <int,int>
  
new map:Map Map_New(Map_II);
  
Map_Emplace(Map_IIMap, .keyint=45, .valueint=10);
  new 
value Map_Find(Map_IIMap, .keyint=45);
  if(
value != -1)
  {
    
printf("Value: %d"value); // Output: Value: 10
  
}
  
  
// example code with <string,int>
  
new map:Map Map_New(Map_SI);
  
Map_Emplace(Map_SIMap"number", .valueint=15);
  new 
value Map_Find(Map_SIMap"number");
  if(
value != -1)
  {
    
printf("Value: %d",value); // Output: Value: 15
  
}

Installing
1. Download unordered_map from Releases page
2. Extract unordered_map.inc to pawno/include folder
3. In your mode include unordered_map.inc
4. Extract unordered_map.dll or unordered_map.so to plugins folder
5. In the server.cfg on line plugins add unordered_map.dll or unordered_map.so

Releases:
https://github.com/AnveSamp/unordered_map/releases

Source code:
https://github.com/AnveSamp/unordered_map

Happy new year

Last edited by ShapeGaz; 01/01/2018 at 09:24 PM.
ShapeGaz is offline   Reply With Quote
Old 31/12/2017, 11:40 AM   #2
AnGeL_KRAMER
Big Clucker
 
AnGeL_KRAMER's Avatar
 
Join Date: Nov 2011
Posts: 130
Reputation: 10
Default Re: unordered_map

WHAT? example where it really can come in handy will be?
AnGeL_KRAMER is offline   Reply With Quote
Old 31/12/2017, 12:21 PM   #3
ShapeGaz
Little Clucker
 
Join Date: Sep 2017
Posts: 19
Reputation: 4
Default Re: unordered_map

Quote:
Originally Posted by AnGeL_KRAMER View Post
WHAT? example where it really can come in handy will be?
unordered_map is one of hash table variants, you can read about this here. This is a special data structure for storing key pairs and their values. In essence, this is an associative array, in which the key is represented as a hash function. If you want examples, you can find them here. In the pawn, you can replace this large array, which is created to find the values.

Last edited by ShapeGaz; 31/12/2017 at 01:12 PM.
ShapeGaz is offline   Reply With Quote
Old 31/12/2017, 02:57 PM   #4
Yashas
Gangsta
 
Join Date: Jun 2012
Location: India
Posts: 891
Reputation: 290
Default Re: unordered_map

You should allow all of the following (<key, value>):
<string, int>
<string, string>
<int, string>
<int, int>

This would be very useful if you could make it more complete:
1. allow the scripter to set load factor, bucket count, etc.
2. set function which works like try_emplace
3. set function which sets iff the key exists
Yashas is offline   Reply With Quote
Old 31/12/2017, 03:59 PM   #5
ShapeGaz
Little Clucker
 
Join Date: Sep 2017
Posts: 19
Reputation: 4
Default Re: unordered_map

Quote:
Originally Posted by Yashas View Post
You should allow all of the following (<key, value>):
<string, int>
<string, string>
<int, string>
<int, int>

This would be very useful if you could make it more complete:
1. allow the scripter to set load factor, bucket count, etc.
2. set function which works like try_emplace
3. set function which sets iff the key exists
Why you cannot use it like:
PHP Code:
 // Get int value
new map:map map_new();
map_emplace(map,"12","14");
new 
value[2];
map_find(map,"12",value,sizeof(value));
new 
intvalue strval(value);

// Emplace int value
new value[2];
format(value,sizeof(value),"%d",10); // With the key you can do the same
map_emplace(map,"test",value); 
1. I'll add to the next updates
2. I'll add to the next updates
3. What? Explain pls

Last edited by ShapeGaz; 01/01/2018 at 09:37 PM.
ShapeGaz is offline   Reply With Quote
Old 31/12/2017, 07:14 PM   #6
chneubeul
Big Clucker
 
Join Date: May 2009
Posts: 56
Reputation: 0
Default Re: unordered_map

Hello,

nice woooorkks.. ? x) what's the interest of creating an hashed array ? its like iterator but hashed so ?
chneubeul is offline   Reply With Quote
Old 31/12/2017, 07:48 PM   #7
NaS
High-roller
 
NaS's Avatar
 
Join Date: Mar 2008
Posts: 1,255
Reputation: 373
Default Re: unordered_map

Quote:
Originally Posted by ShapeGaz View Post
Why you cannot use it like:
PHP Code:
 // Get int value
new map:map map_new();
map_emplace(map,"12","14");
new 
value[2];
map_find(map,"12",value,sizeof(value));
new 
intvalue strval(value);

// Emplace int value
new value[2];
format(value,sizeof(value),"%d",10); // With the key you can do the same
map_emplace(map,"test",value); 
1. I'll add to the next update
2. I'll add to the next update
3. What? Explain pls
If I have to convert every value to a string and vice-versa in PAWN why even use an unordered map?

Also it would be nice to be able to iterate through all elements.
NaS is online now   Reply With Quote
Old 31/12/2017, 08:02 PM   #8
ShapeGaz
Little Clucker
 
Join Date: Sep 2017
Posts: 19
Reputation: 4
Default Re: unordered_map

Quote:
Originally Posted by NaS View Post
If I have to convert every value to a string and vice-versa in PAWN why even use an unordered map?

Also it would be nice to be able to iterate through all elements.
Ok, I'll add new types to the next update

Quote:
Originally Posted by chneubeul View Post
Hello,

nice woooorkks.. ? x) what's the interest of creating an hashed array ? its like iterator but hashed so ?
For an example you can replace a loop to find values in an array
ShapeGaz is offline   Reply With Quote
Old 31/12/2017, 08:17 PM   #9
chneubeul
Big Clucker
 
Join Date: May 2009
Posts: 56
Reputation: 0
Default Re: unordered_map

Ok so its an iterator system, i already used the system from y_less, its was really useful, but with a plugin it should be faster ?
but why hashing the data ?
chneubeul is offline   Reply With Quote
Old 31/12/2017, 08:39 PM   #10
ShapeGaz
Little Clucker
 
Join Date: Sep 2017
Posts: 19
Reputation: 4
Default Re: unordered_map

Quote:
Originally Posted by chneubeul View Post
Ok so its an iterator system, i already used the system from y_less, its was really useful, but with a plugin it should be faster ?
but why hashing the data ?
Yes, it is.
Because when you search for each element of the map it takes about the same amount of time and there is no dependence

Last edited by ShapeGaz; 11/01/2018 at 03:05 PM.
ShapeGaz is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



All times are GMT. The time now is 02:53 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.