Skip to content

Instantly share code, notes, and snippets.

@Bara
Created April 18, 2021 14:10
Show Gist options
  • Save Bara/c2b93b5ff36f5c0d4d2133fcd139acc7 to your computer and use it in GitHub Desktop.
Save Bara/c2b93b5ff36f5c0d4d2133fcd139acc7 to your computer and use it in GitHub Desktop.
#if defined _intmap_included
#endinput
#endif
#define _intmap_included
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 < 0 || index >= this.Length)
{
ThrowError("Index is out of range. (Index: %d, Count: %d)", index, this.Length);
}
char buffer[255];
view_as<StringMapSnapshot>(this).GetKey(index, buffer, sizeof(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 key, any value, bool replace = true)
{
char buffer[6];
Keyize(key, buffer);
return view_as<StringMap>(this).SetValue(buffer, value, replace);
}
// 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_items, bool replace = true)
{
char buffer[6];
Keyize(key, buffer);
return view_as<StringMap>(this).SetArray(buffer, array, num_items, replace);
}
// 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[] value, bool replace = true)
{
char buffer[6];
Keyize(key, buffer);
return view_as<StringMap>(this).SetString(buffer, value, replace);
}
// Returns an integer in a Map.
//
// @param key The key.
// @return The value as integer.
public int GetInt(int key)
{
int value;
char buffer[6];
Keyize(key, buffer);
view_as<StringMap>(this).GetValue(buffer, value);
return value;
}
// 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 key, any& value)
{
char buffer[6];
Keyize(key, buffer);
return view_as<StringMap>(this).GetValue(buffer, value);
}
// 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 key, any[] array, int max_size, int& size = 0)
{
char buffer[6];
Keyize(key, buffer);
return view_as<StringMap>(this).GetArray(buffer, array, max_size, size);
}
// 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 key, char[] value, int max_size, int& size = 0)
{
char buffer[6];
Keyize(key, buffer);
return view_as<StringMap>(this).GetString(buffer, value, max_size, size);
}
// 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[6];
Keyize(key, 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;
}
}
}
stock void Keyize(any key, char buffer[6])
{
int i = key;
buffer[0] = ((i >>> 28) & 0x7F) | 0x80;
buffer[1] = ((i >>> 21) & 0x7F) | 0x80;
buffer[2] = ((i >>> 14) & 0x7F) | 0x80;
buffer[3] = ((i >>> 7) & 0x7F) | 0x80;
buffer[4] = (i & 0x7F) | 0x80;
buffer[5] = 0x00;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment