AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   IntMap (StringMap for lazy devs) (https://forums.alliedmods.net/showthread.php?t=321526)

Ilusion9 02-15-2020 08:05

IntMap (StringMap for lazy devs)
 
PHP Code:

methodmap IntMapSnapshot Handle
{
    
// Returns the number of keys in the map snapshot.
    
property int Length
    
{
        public 
get()
        {
            return 
view_as<StringMapSnapshot>(this).Length;
        }
    }
    
    
// Retrieves the key of a given index in a map snapshot.
    // 
    // @param index      Key index (starting from 0).
    // @return           The key.
    // @error            Index out of range.
    
public int GetKey(int index)
    {
        if (
index || index >= this.Length)
        {
            
ThrowError("Index out of range");
        }
        
        
char buffer[255];
        
view_as<StringMapSnapshot>(this).GetKey(indexbuffersizeof(buffer));
        return 
StringToInt(buffer);
    }
}

methodmap IntMap Handle
{
    
// The IntMap must be freed via delete or CloseHandle().
    
public IntMap()
    {
        return 
view_as<IntMap>(new StringMap());
    }
    
    
// Sets a value in a Map, either inserting a new entry or replacing an old one.
    //
    // @param key        The key.
    // @param value      Value to store at this key.
    // @param replace    If false, operation will fail if the key is already set.
    // @return           True on success, false on failure.
    
public bool SetValue(int keyany valuebool replace true)
    {
        
char buffer[255];
        
IntToString(keybuffersizeof(buffer));
        return 
view_as<StringMap>(this).SetValue(buffervaluereplace);
    }
    
    
// Sets an array value in a Map, either inserting a new entry or replacing an old one.
    //
    // @param key        The key.
    // @param array      Array to store.
    // @param num_items  Number of items in the array.
    // @param replace    If false, operation will fail if the key is already set.
    // @return           True on success, false on failure.
    
public bool SetArray(int key, const any[] array, int num_itemsbool replace true)
    {
        
char buffer[255];
        
IntToString(keybuffersizeof(buffer));
        return 
view_as<StringMap>(this).SetArray(buffer, array, num_itemsreplace);
    }
    
    
// Sets a string value in a Map, either inserting a new entry or replacing an old one.
    //
    // @param key        The key.
    // @param value      String to store.
    // @param replace    If false, operation will fail if the key is already set.
    // @return           True on success, false on failure.
    
public bool SetString(int key, const char[] valuebool replace true)
    {
        
char buffer[255];
        
IntToString(keybuffersizeof(buffer));
        return 
view_as<StringMap>(this).SetString(buffervaluereplace);
    }
    
    
// Retrieves a value in a Map.
    //
    // @param key        The key.
    // @param value      Variable to store value.
    // @return           True on success.  False if the key is not set, or the key is set 
    //                   as an array or string (not a value).
    
public bool GetValue(int keyanyvalue)
    {
        
char buffer[255];
        
IntToString(keybuffersizeof(buffer));
        return 
view_as<StringMap>(this).GetValue(buffervalue);
    }
    
    
// Retrieves an array in a Map.
    //
    // @param key        The key.
    // @param array      Buffer to store array.
    // @param max_size   Maximum size of array buffer.
    // @param size       Optional parameter to store the number of elements written to the buffer.
    // @return           True on success.  False if the key is not set, or the key is set 
    //                   as a value or string (not an array).
    
public bool GetArray(int keyany[] array, int max_sizeintsize 0)
    {
        
char buffer[255];
        
IntToString(keybuffersizeof(buffer));
        return 
view_as<StringMap>(this).GetArray(buffer, array, max_sizesize);
    }
    
    
// Retrieves a string in a Map.
    //
    // @param key        The key.
    // @param value      Buffer to store value.
    // @param max_size   Maximum size of string buffer.
    // @param size       Optional parameter to store the number of bytes written to the buffer.
    // @return           True on success.  False if the key is not set, or the key is set 
    //                   as a value or array (not a string).
    
public bool GetString(int keychar[] valueint max_sizeintsize 0)
    {
        
char buffer[255];
        
IntToString(keybuffersizeof(buffer));
        return 
view_as<StringMap>(this).GetString(buffervaluemax_sizesize);
    }
    
    
// Removes a key entry from a Map.
    //
    // @param key        The key.
    // @return           True on success, false if the value was never set.
    
public bool Remove(int key)
    {
        
char buffer[255];
        
IntToString(keybuffersizeof(buffer));
        return 
view_as<StringMap>(this).Remove(buffer);
    }
    
    
// Clears all entries from a Map.
    
public void Clear()
    {
        
view_as<StringMap>(this).Clear();
    }
    
    
// Create a snapshot of the map's keys. See IntMapSnapshot.
    
public IntMapSnapshot Snapshot()
    {
        return 
view_as<IntMapSnapshot>(view_as<StringMap>(this).Snapshot());
    }
    
    
// Retrieves the number of elements in a map.
    
property int Size
    
{
        public 
get()
        {
            return 
view_as<StringMap>(this).Size;
        }
    }


All methods from IntMap are similar to StringMap methods, but the key is of type int.
For IntMapSnapshot, KeyBufferSize is not available.

It's not the best way to implement IntMap, but I hope https://github.com/alliedmodders/sou...pull/579/files will be added in the future.

GitHub: https://github.com/Ilusion9/intmap-inc-sm


All times are GMT -4. The time now is 18:55.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.