Array Copy Stock

For scripting examples for learning how to use AMXX.
User avatar
Archive-Poster
Posts: 1347
Gender:
United States of America

Array Copy Stock

Post by Archive-Poster »

I made this the other day because I could not find one. Maybe someone else will need it. It will copy data from one array to another for a max length of cells and you're able to set starting positions. If someone sees optimization errors or has any other, better, ideas feel free to edit/comment.

Code: Select all

/**
 * Copies information from one array into another.
 * 
 * @param into        The array to place the data into
 * @param from        The array to get the data from
 * @param len         The number of cells to copy
 * @param ignoretags  True to enable tag comparison, false to
 *                    ignore them.
 * @param intotag     The tagof constant for the into array
 * @param intosize    The max size of data to place into
 *                    the array
 * @param intopos     The starting position to place data
 *                    into
 * @param fromtag     The tagof constant for the from array
 * @param fromsize    The max size of data to place from
 *                    the array
 * @param frompos     The starting position to begin copying
 *                    data from
 */
stock arraycopy( any:into[], any:from[], len = sizeof into, bool:ignoretags = false, intotag = tagof into, intosize = sizeof into, intopos = 0, fromtag = tagof from, fromsize = sizeof from, frompos = 0) {
    if (!ignoretags && intotag != fromtag) {
        //So we know no elements were copied (we did not remove an element ie. returning -1)
        return 0;
    }
    
    new i
    while (i < len) {
        if (intopos >= intosize || frompos >= fromsize) {
            break;
        }
        
        into[intopos++] = from[frompos++];
        i++;
    }
    
    return i;
}
Attachments
arraycopy.inc.zip
(873 Bytes) Downloaded 5 times
User avatar
Archive-Poster
Posts: 1347
Gender:
United States of America

Re: Array Copy Stock

Post by Archive-Poster »

you still need to tag both into and from as any so that any tag can be used. Currently it will give you a tag mismatch.

I suggest you move the tags to the end of the parameter list, so people don't have to use an underscore to skip the argument.

I would also suggest a parameter for ignoring tags.

Personally, how I would make it:

Code: Select all

stock arraycopy( any:into[], any:from[], len, bool:ignoretags = false, intotag = tagof into, intosize = sizeof into, intopos = 0, fromtag = tagof from, fromsize = sizeof from, frompos = 0) {
    if (!ignoretags && intotag != fromtag) {
        //So we know no elements were copied (we did not remove an element ie. returning -1)
        return 0;
    }
    
    new i
    while (i < len) {
        if (intopos >= intosize || frompos >= fromsize) {
            break;
        }
        
        into[intopos++] = from[frompos++];
        i++;
    }
    
    return i;
}
Did you test your stock ? Because I'm doing some tests just by curiosity, and by using tagof in the header will result 0 always or using multi-tag (any, or {Float, bool, etc..} ) and the value will be always the same. In others words, and if I don't say bullshits, it works only with mono-tag, and tagof has to used inside the function.
Ah, it does seem to bugger up when used as a default value for a parameter. I thought it might work like sizeof/charsmax.

However, this works fine:

Code: Select all

arraycopy( copyarray, initarray, sizeof copyarray, .intotag = tagof copyarray, .fromtag = tagof initarray );
The only way I see it working well is by using a macro to auto fill the required info.

Something like:

Code: Select all

#define array_copy(%1, %2, %3) arraycopy( %1, %2, %3, .intotag = tagof %1, .fromtag = tagof %2 )
User avatar
Archive-Poster
Posts: 1347
Gender:
United States of America

Re: Array Copy Stock

Post by Archive-Poster »

Amxx pawn doesn't directly support dynamic memory allocation. You would need to implement the cell array structure, which I believe is an array list . I don't know of any real fast way other than copying each element iteratively. It would definitely be easier in another language like java or c, but you wouldn't need to then (since an array list is still an array). In java you can just return the array copy (System.arraycopy or Arrays.copyRangeOf), in c just get a pointer and length. But if your making a new copy, just make a new array and copy iteratively.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests