SA-MP Forums Archive
[Plugin] unordered_map - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Plugin Development (https://sampforum.blast.hk/forumdisplay.php?fid=18)
+--- Thread: [Plugin] unordered_map (/showthread.php?tid=647190)



unordered_map - ShapeGaz - 31.12.2017

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


Re: unordered_map - AnGeL_KRAMER - 31.12.2017

WHAT? example where it really can come in handy will be?


Re: unordered_map - ShapeGaz - 31.12.2017

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.


Re: unordered_map - Yashas - 31.12.2017

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


Re: unordered_map - ShapeGaz - 31.12.2017

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


Re: unordered_map - chneubeul - 31.12.2017

Hello,

nice woooorkks.. ? x) what's the interest of creating an hashed array ? its like iterator but hashed so ?


Re: unordered_map - NaS - 31.12.2017

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.


Re: unordered_map - ShapeGaz - 31.12.2017

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


Re: unordered_map - chneubeul - 31.12.2017

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 ?


Re: unordered_map - ShapeGaz - 31.12.2017

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