AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   Array Copy Stock (https://forums.alliedmods.net/showthread.php?t=166930)

Tirant 09-09-2011 11:08

Array Copy Stock
 
1 Attachment(s)
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:

/**
 * 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;
}


ConnorMcLeod 09-09-2011 11:17

Re: Array Copy Stock
 
So you don't need to cast anything with that stock ?

Tirant 09-09-2011 17:57

Re: Array Copy Stock
 
I don't think so. Does pawn have an instanceof operator? Like to verify one bar type == another.

fysiks 09-09-2011 18:17

Re: Array Copy Stock
 
Technically Pawn is typeless but what he is refering to is tags matching up.

Tirant 09-09-2011 18:23

Re: Array Copy Stock
 
I know it's typeless. So a tags instanceof operator was what I was referring to.

Emp` 09-09-2011 19:39

Re: Array Copy Stock
 
You could just tag both into and from as any and you won't get any tag mismatches.

Tirant 09-10-2011 13:35

Re: Array Copy Stock
 
Quote:

Originally Posted by Emp` (Post 1551817)
You could just tag both into and from as any and you won't get any tag mismatches.

I'm also thinking that they should be the same type though. Like, if someone wanted to copy explicitly from a[5] into Float:b[5], there should be a tag mismatch there, so they should place the any tag when copying. So what I'm saying, is maybe there should be an error.

fysiks 09-10-2011 13:39

Re: Array Copy Stock
 
Quote:

Originally Posted by Tirant (Post 1552369)
I'm also thinking that they should be the same type though. Like, if someone wanted to copy explicitly from a[5] into Float:b[5], there should be a tag mismatch there, so they should place the any tag when copying. So what I'm saying, is maybe there should be an error.

To correctly implement a tag mismatch error for a stock that can be used on multiple tags would require a function to detect the tag which doesn't exist (granted, it would be a preprocessor function).

Emp` 09-10-2011 13:44

Re: Array Copy Stock
 
Quote:

Originally Posted by fysiks (Post 1552373)
To correctly implement a tag mismatch error for a stock that can be used on multiple tags would require a function to detect the tag which doesn't exist (granted, it would be a preprocessor function).

tagof ?

fysiks 09-10-2011 13:55

Re: Array Copy Stock
 
Quote:

Originally Posted by Emp` (Post 1552377)

Does it work here?


All times are GMT -4. The time now is 03:36.

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