Raised This Month: $12 Target: $400
 3% 

Array Copy Stock


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Tirant
Veteran Member
Join Date: Jul 2008
Location: Los Angeles, California
Old 09-09-2011 , 11:08   Array Copy Stock
Reply With Quote #1

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;
}
Attached Files
File Type: inc arraycopy.inc (1.5 KB, 468 views)
__________________

PM me if you're interested in buying the Credits addition for Base Builder
Battlefield Rebirth [66% done]
Call of Duty: MW2 [100% done]
Base Builder [100% done]

Last edited by Tirant; 09-10-2011 at 15:28.
Tirant is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 09-09-2011 , 11:17   Re: Array Copy Stock
Reply With Quote #2

So you don't need to cast anything with that stock ?
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
Tirant
Veteran Member
Join Date: Jul 2008
Location: Los Angeles, California
Old 09-09-2011 , 17:57   Re: Array Copy Stock
Reply With Quote #3

I don't think so. Does pawn have an instanceof operator? Like to verify one bar type == another.
__________________

PM me if you're interested in buying the Credits addition for Base Builder
Battlefield Rebirth [66% done]
Call of Duty: MW2 [100% done]
Base Builder [100% done]
Tirant is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 09-09-2011 , 18:17   Re: Array Copy Stock
Reply With Quote #4

Technically Pawn is typeless but what he is refering to is tags matching up.
__________________
fysiks is offline
Tirant
Veteran Member
Join Date: Jul 2008
Location: Los Angeles, California
Old 09-09-2011 , 18:23   Re: Array Copy Stock
Reply With Quote #5

I know it's typeless. So a tags instanceof operator was what I was referring to.
__________________

PM me if you're interested in buying the Credits addition for Base Builder
Battlefield Rebirth [66% done]
Call of Duty: MW2 [100% done]
Base Builder [100% done]
Tirant is offline
Emp`
AMX Mod X Plugin Approver
Join Date: Aug 2005
Location: Decapod 10
Old 09-09-2011 , 19:39   Re: Array Copy Stock
Reply With Quote #6

You could just tag both into and from as any and you won't get any tag mismatches.
Emp` is offline
Send a message via AIM to Emp` Send a message via MSN to Emp` Send a message via Yahoo to Emp` Send a message via Skype™ to Emp`
Tirant
Veteran Member
Join Date: Jul 2008
Location: Los Angeles, California
Old 09-10-2011 , 13:35   Re: Array Copy Stock
Reply With Quote #7

Quote:
Originally Posted by Emp` View Post
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.
__________________

PM me if you're interested in buying the Credits addition for Base Builder
Battlefield Rebirth [66% done]
Call of Duty: MW2 [100% done]
Base Builder [100% done]
Tirant is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 09-10-2011 , 13:39   Re: Array Copy Stock
Reply With Quote #8

Quote:
Originally Posted by Tirant View Post
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).
__________________

Last edited by fysiks; 09-10-2011 at 14:18.
fysiks is offline
Emp`
AMX Mod X Plugin Approver
Join Date: Aug 2005
Location: Decapod 10
Old 09-10-2011 , 13:44   Re: Array Copy Stock
Reply With Quote #9

Quote:
Originally Posted by fysiks View Post
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 ?
Emp` is offline
Send a message via AIM to Emp` Send a message via MSN to Emp` Send a message via Yahoo to Emp` Send a message via Skype™ to Emp`
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 09-10-2011 , 13:55   Re: Array Copy Stock
Reply With Quote #10

Quote:
Originally Posted by Emp` View Post
Does it work here?
__________________
fysiks is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 16:56.


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