[Plugin] CSTL - Data container(s) (Currently vector)
#1

CSTL - VECTOR AND MAP FOR PAWN

Implementing std::vector and std::map for PAWN

Natives

VECTOR

VECTORS DONT HAVE TO BE INITIALIZED, SPECIFY AN INTEGER AS VECID PARAMETER.
Vectors are typeless (you can push and set any datatypes without problem). But vector will do type checking when returning values (trying to vector_get_arr on float value will fail, you can use vector_entry_type to get datatype of entry).

Vector size is automatically increased when using push_back to add more data. Its also possible to increase vector size manually with vector_resize. You can NOT use vector_set to set anything into index bigger than vector size (size - 1, indexes are same as in arrays).
Disallowed example:

pawn Code:
// When nothing has been done to vector VECTOR_ID (size is default 0), following wont work
vector_set_arr(VECTOR_ID, 1, "Im going to set this into index 1");

Allowed example

pawn Code:
// When vector size is increased, its possible to change index value with vector_set
vector_push_back(VECTOR_ID, 2); // pushes integer '2' into vector (first element, index 0)
vector_push_back(VECTOR_ID, 5); // pushes integer '5' into vector (second element, index 1)
vector_set_arr(VECTOR_ID, 1, "Im going to set this into index 1"); // this will work
native vector_push_back(vecid, element); // push back integer
native vector_push_back_float(vecid, Float:element); // push back float
native vector_push_back_arr(vecid, element[]); // push back array (string)

native vector_size(vecid); // get vector size

native vector_get(vecid, id); // get integer by index
native Float:vector_get_float(vecid, id); // get floating point number by index
native vector_get_arr(vecid, id, buffer[], buflen); // get array element to buffer by index

native vector_set(vecid, id, element); // set index
native vector_set_float(vecid, id, Float:element); // set float
native vector_set_arr(vecid, id, element[]); // set array

native vector_clear(vecid); // clear vector
native vector_resize(vecid, newsize); // resize vector to new size

native vector_entry_type(vecid, id); // get element type (int, array or float)
native vector_remove(vecid, id); // remove element by index

native vector_find(vecid, element); // find index of int element
native vector_find_float(vecid, Float:element); // find index of float element
native vector_find_arr(vecid, element[]); // find index of array

native vector_globalize(vecid); // makes vector visible to all scripts
native vector_deglobalize(vecid); // deglobalizes vector (vector removed from all scripts except calling script)
native vector_is_globalized(vecid); // is vector ID globalized


MAP

MAPS ALSO DONT HAVE TO BE INITIALIZED

native map_insert(mapid, key[], value); // insert with integer value
native map_insert_float(mapid, key[], Float:value); // insert with float value
native map_insert_arr(mapid, key[], value[]); // insert with array as value

native map_get(mapid, key[]); // get integer
native Float:map_get_float(mapid, key[]); // get float
native map_get_arr(mapid, key[], buffer[], buflen); // get array to buffer

native map_size(mapid); // element count of array
native map_remove(mapid, key[]); // remove specific key
native map_contains(mapid, key[]); // does map contain specific key
native map_entry_type(mapid, key[]); // return value type of key (int, array or float)
native map_clear(mapid); // clear the map

native map_globalize(mapid); // makes map visible to all scripts
native map_deglobalize(mapid); // deglobalizes map (map removed from all scripts except calling script)
native map_is_globalized(mapid); // is map ID globalized

Example

Vector
pawn Code:
new vecid;
vecid = 0;
vector_push_back_arr(vecid, "A");
vector_push_back(vecid, 1);
vector_push_back_arr(vecid, "B");

for (new i = 0 ; i < vector_size(vecid) ; i++)
{
    if (vector_entry_type(vecid, i) == VEC_ENTRY_TYPE_CELL)
    {
        printf("INT: %i", vector_get(vecid, i));
    } else if (vector_entry_type(vecid, i) == VEC_ENTRY_TYPE_FLOAT)
    {
        printf("FLOAT: %f", vector_get_float(vecid, i));
    } else if (vector_entry_type(vecid, i) == VEC_ENTRY_TYPE_ARRAY)
    {
        new buffer[100];
        vector_get_arr(vecid, i, buffer, 100);
        printf("ARRAY: %s", buffer);
    }
}
vector_clear(vecid);
printf("Vector size: %i", vector_size(vecid));
outputs:
Code:
ARRAY: A
CELL: 1
ARRAY: B
Vector size: 0
Map
pawn Code:
#include <a_samp>
#include <cstl>

new const FIRST_MAP = 0;

public OnFilterScriptInit()
{
    map_insert_arr(FIRST_MAP, "key", "This is value");
   
    if (map_contains(FIRST_MAP, "key") == 1)
    {
        new buffer[100];
        map_get_arr(FIRST_MAP, "key", buffer, 100);
        printf("Map key \"key\" has value: %s", buffer);
    }
    printf("Map length is now: %i", map_size(FIRST_MAP));
    map_remove(FIRST_MAP, "key");
    printf("After removal size is: %i", map_size(FIRST_MAP));
    return 1;
}
outputs:
Code:
Map key "key" has value: This is value
Map length is now: 1
After removal size is: 0
Updates
  • 14.6.2011: Added vector_index_exists
  • 15.3.2011: Added support for globalization of specific containers (you can use one container from all scripts)
  • 14.3.2011: Vectors and maps are now specific to one AMX vm (every filterscript, GM can have own, with same ids)
  • 12.3.2011: Added std::map also
License
Copyright © 2011, Teprey
All rights reserved.
By using functions provided by this plugin within your software or otherwise using this software, you agree to comply with following terms and conditions:

You may not change copyright message or credit information in any file of this product.
You may redistribute and/or modify this software only if you preserve credit.

THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.



DOWNLOAD WINDOWS DLL + LINUX .SO + SOURCE
Reply


Messages In This Thread
CSTL - Data containers (Vector, Map) - by Teprey - 12.03.2011, 13:38
Re: CSTL - Data container(s) (Currently vector) - by Zh3r0 - 12.03.2011, 14:24
Re: CSTL - Data container(s) (Currently vector) - by TheArcher - 12.03.2011, 14:59
Re: CSTL - Data container(s) (Currently vector) - by Teprey - 12.03.2011, 15:08
Re: CSTL - Data container(s) (Currently vector) - by Zh3r0 - 12.03.2011, 15:12
Re: CSTL - Data container(s) (Currently vector) - by [L3th4l] - 12.03.2011, 15:18
Re: CSTL - Data container(s) (Currently vector) - by Teprey - 12.03.2011, 15:41
Re: CSTL - Data container(s) (Currently vector) - by Dreftas - 12.03.2011, 16:41
Re: CSTL - Data container(s) (Currently vector) - by Medal Of Honor team - 12.03.2011, 17:03
Re: CSTL - Data container(s) (Currently vector) - by Teprey - 12.03.2011, 18:27
Re: CSTL - Data container(s) (Currently vector) - by hencz - 12.03.2011, 18:45
Re: CSTL - Data container(s) (Currently vector) - by anonymousx - 12.03.2011, 18:54
Re: CSTL - Data container(s) (Currently vector) - by 1337connor - 12.03.2011, 19:11
Re: CSTL - Data container(s) (Currently vector) - by xxmitsu - 12.03.2011, 19:17
Re: CSTL - Data container(s) (Currently vector) - by Teprey - 12.03.2011, 19:27
Re: CSTL - Data container(s) (Currently vector) - by Patrik356b - 12.03.2011, 19:38
Re: CSTL - Data container(s) (Currently vector) - by RyDeR` - 12.03.2011, 22:35
Re: CSTL - Data container(s) (Currently vector) - by CyNiC - 13.03.2011, 03:05
Re: CSTL - Data container(s) (Currently vector) - by anonymousx - 13.03.2011, 07:45
Re: CSTL - Data container(s) (Currently vector) - by Kyosaur - 13.03.2011, 07:54
Re: CSTL - Data container(s) (Currently vector) - by Minokon - 13.03.2011, 09:39
Re: CSTL - Data container(s) (Currently vector) - by Macluawn - 13.03.2011, 15:35
Re: CSTL - Data container(s) (Currently vector) - by Teprey - 14.03.2011, 13:54
Re: CSTL - Data container(s) (Currently vector) - by XFlawless - 14.03.2011, 14:22
Re: CSTL - Data container(s) (Currently vector) - by RyDeR` - 14.03.2011, 15:42
Re: CSTL - Data container(s) (Currently vector) - by Teprey - 14.03.2011, 16:08
Re: CSTL - Data container(s) (Currently vector) - by Teprey - 14.03.2011, 20:33
Re: CSTL - Data container(s) (Currently vector) - by Kyosaur - 15.03.2011, 05:19
Re: CSTL - Data container(s) (Currently vector) - by SlashPT - 15.03.2011, 06:58
Re: CSTL - Data container(s) (Currently vector) - by Macluawn - 15.03.2011, 07:05
Re: CSTL - Data container(s) (Currently vector) - by Retardedwolf - 15.03.2011, 10:00
Re: CSTL - Data container(s) (Currently vector) - by Slice - 15.03.2011, 11:43
Re: CSTL - Data container(s) (Currently vector) - by Teprey - 15.03.2011, 12:25
Re: CSTL - Data container(s) (Currently vector) - by Teprey - 15.03.2011, 15:18
Re: CSTL - Data container(s) (Currently vector) - by iggy1 - 21.03.2011, 08:42
Re: CSTL - Data container(s) (Currently vector) - by CaHbKo - 14.06.2011, 09:09
Re: CSTL - Data container(s) (Currently vector) - by Calgon - 14.06.2011, 09:16
Re: CSTL - Data container(s) (Currently vector) - by 0x5A656578 - 14.06.2011, 10:47
Re: CSTL - Data container(s) (Currently vector) - by Teprey - 14.06.2011, 15:36
Re: CSTL - Data container(s) (Currently vector) - by CaHbKo - 19.06.2011, 12:07
Re: CSTL - Data container(s) (Currently vector) - by Teprey - 19.06.2011, 15:19
Re: CSTL - Data container(s) (Currently vector) - by Macluawn - 24.06.2011, 21:48
Re: CSTL - Data container(s) (Currently vector) - by linuxthefish - 24.06.2011, 22:28
Re: CSTL - Data container(s) (Currently vector) - by Teprey - 29.06.2011, 11:54
Re: CSTL - Data container(s) (Currently vector) - by GangsTa_ - 29.06.2011, 15:00
Re: CSTL - Data container(s) (Currently vector) - by iggy1 - 29.06.2011, 17:12
Re: CSTL - Data container(s) (Currently vector) - by Teprey - 30.06.2011, 10:32
Re: CSTL - Data container(s) (Currently vector) - by Teprey - 14.07.2011, 11:28
Re: CSTL - Data container(s) (Currently vector) - by Dreftas - 25.08.2011, 10:10
Re: CSTL - Data container(s) (Currently vector) - by papedo - 16.09.2011, 17:11
Re: CSTL - Data container(s) (Currently vector) - by Teprey - 18.10.2011, 15:26
Re: CSTL - Data container(s) (Currently vector) - by Slice - 18.10.2011, 16:03
Re: CSTL - Data container(s) (Currently vector) - by SourceCode - 18.10.2011, 16:12
Re: CSTL - Data container(s) (Currently vector) - by Teprey - 18.10.2011, 19:11
Re: CSTL - Data container(s) (Currently vector) - by Babul - 20.10.2011, 18:22
Re: CSTL - Data container(s) (Currently vector) - by AndreT - 04.12.2011, 09:19
Re: CSTL - Data container(s) (Currently vector) - by SchurmanCQC - 07.01.2012, 19:40
Re: CSTL - Data container(s) (Currently vector) - by cyber_punk - 07.07.2012, 01:58
Re: CSTL - Data container(s) (Currently vector) - by AndreT - 07.07.2012, 06:16
Re: CSTL - Data container(s) (Currently vector) - by cyber_punk - 10.07.2012, 00:26
Re: CSTL - Data container(s) (Currently vector) - by Yiin - 09.05.2013, 13:05
Re: CSTL - Data container(s) (Currently vector) - by RCON1 - 22.09.2013, 09:34
Re: CSTL - Data container(s) (Currently vector) - by OxyG3N - 07.01.2014, 10:54
Re: CSTL - Data container(s) (Currently vector) - by detter - 12.01.2014, 15:29
Re: CSTL - Data container(s) (Currently vector) - by Michalec - 12.12.2015, 22:36

Forum Jump:


Users browsing this thread: 1 Guest(s)