array.inc.mac

A forum for macro code snippets to be used in writing other macros. Post routines or .inc files here only, completed macros go to the Macro Depot. MQ2Data format only!

Moderator: MacroQuest Developers

Nightcrawler7514
orc pawn
orc pawn
Posts: 25
Joined: Mon Oct 27, 2003 6:43 pm
Location: Denmark

array.inc.mac

Post by Nightcrawler7514 » Fri Sep 03, 2004 5:07 pm

I have made some different functions for use with arrays I though people might find usefull. I plan on expand the list as I get the time.

If you have any problem or find any bugs let me know.

Code: Select all

|*********************************************************************
Filename     : array.inc.mac
Version      : 1.0
Made by      : Nightcrawler7514
EMail        ; nightcrawler7514@hotmail.com
Functions    : DumpArray, SingleArrayCopy, SingleArrayAdd, SingleArrayReSize
               MultiArrayCopy, MultiArrayAdd, MultiArrayReSize
Comments     :
This is a combilation of function all dealing with array manipulation of some sort.
All arrays that thise function work with has to be declared as outer, and all
reference to the arrays in parameters are the name of the array and not the array it self.

/declare SomeArray[20] int outer 5
/varset SomeArray[4] 20
/call DumpArray SomeArray

Functionlist : 
DumpArray(string DA_Array)
- Dumps the contents an array to screen. (mostly used for debugging)
- DA_Array -> The name of the array to dumped.

SingleArrayCopy(string SAC_Source, string SAC_Target, string SAC_TargetType)
- Copies on single dimension array to another.
- SAC_Source -> The name of the array you want to copy.
- SAC_Target -> The name of the array you want to copy to.
- SAC_Type -> The type of the target array.

SingleArrayAdd(string SAA_Array, int SAA_Size, string SAA_Type)
- Adds a sertain number of slots to an array.
- SAA_Array -> The name of the array to be added to.
- SAA_Size -> The size that needed added.
- SAA_Type -> The type of SAA_Array.

SingleArrayReSize(string SARS_Array, int SARS_Size, string SARS_Type)
- Resizes an array to a new size.
- SARS_Array -> The name of the array to be resized.
- SARS_Size -> The new size of the array.
- SARS_Type -> The type of SARS_Array.

MultiArrayCopy(string MAC_Source, string MAC_Target, string MAC_TargetType)
- Copies a multidimensional array from one array to another.
- MAC_Source -> The name of the array you want to copy.
- MAC_Target -> The name of the array you want to copy to.
- MAC_Type -> The type of the target array.

MultiArrayAdd(string MAA_Array, int MAA_Dimension, int MAA_Size, string MAA_Type)
- Adds a sertain number of slots to one of the dimensions in an array.
- MAA_Array -> The name of the array to be added to.
- MAA_Dimension -> Which dimension in the array that needs more slots.
- MAA_Size -> The size that needed added.
- MAA_Type -> The type of MAA_Array.

MultiArrayReSize(string MARS_Array, int MARS_Dimension, int MARS_Size, string MARS_Type)
- Resizes one of the dimensions in an array to a new size.
- MARS_Array -> The name of the array to be resized.
- MARS_Dimension -> Which dimension in the array that needs to be resized.
- MARS_Size -> The new size of the dimension.
- MARS_Type -> The type of MARS_Array.

History :
- Version 1.0
-> ADDED Functions
--> DumpArray
--> SingleArrayCopy
--> SingleArrayAdd
--> SimgleArrayReSize
--> MultiArrayCopy
--> MultiArrayAdd
--> MultoArrayReSize
*********************************************************************|

| ********************************************************************
| Dumps the contents of an array to screen.
| ********************************************************************
Sub DumpArray(string DA_Array)
| Simple errorchecking to see if the array is declared.
  /if (!${Defined[${DA_Array}]}) {
    /echo DumpArray -> ${DA_Array} is not defined. Declare the array before trying to dump it.
    /return
  }

| Local variables.
  /declare DA_ArrayCounter int local
  /declare DA_Counter int local
  /declare DA_DimensionString string local
  /declare DA_DimensionCounters[${${DA_Array}.Dimensions}] int local 1

| Runs though the array item by item.
  /for DA_ArrayCounter 1 to ${${DA_Array}.Size}
| Builds a dimensionstring to be used with /varset and /echo commands.
    /varset DA_DimensionString
    /for DA_Counter 1 to ${DA_DimensionCounters.Size}
      /varset DA_DimensionString ${DA_DimensionString}${DA_DimensionCounters[${DA_Counter}]}
      /if (${DA_Counter}!=${DA_DimensionCounters.Size}) /varset DA_DimensionString ${DA_DimensionString},
    /next DA_Counter
    
    /echo ${DA_Array}[${DA_DimensionString}] -> ${${DA_Array}[${DA_DimensionString}]}.

| Calculates the location of the next item in the array.
    /varset DA_Counter 1
    :DA_Loop
      /varcalc DA_DimensionCounters[${DA_Counter}] ${DA_DimensionCounters[${DA_Counter}]}+1
      /if (${DA_DimensionCounters[${DA_Counter}]}>${${DA_Array}.Size[${DA_Counter}]}) {
        /varset DA_DimensionCounters[${DA_Counter}] 1
        /varcalc DA_Counter ${DA_Counter}+1
        /if (${DA_Counter}>${DA_DimensionCounters.Size}) /goto :DA_EndLoop
        /goto :DA_Loop
      } else /goto :DA_EndLoop
    
    :DA_EndLoop
  /next DA_ArrayCounter
/return

| ********************************************************************
| Copies a single dimension array from one array to another.
| ********************************************************************
Sub SingleArrayCopy(string SAC_Source, string SAC_Target, string SAC_Type)
| Simple errorchecking to see if the array is declared.
  /if (!${Defined[${SAC_Source}]}) {
    /echo SingleArrayCopy -> ${SAC_Source} is not defined. Declare the array before trying to copy it.
    /return
  }

| Local variables.
  /declare SAC_Counter int local

| Checks to see if the target array is allready declared and deletes it if it is and redeclaring it.
  /if (${Defined[${SAC_Target}]}) /deletevar ${SAC_Target}
  /declare ${SAC_Target}[${${SAC_Source}.Size}] ${SAC_Type} outer

| Copies the values from Source to Target.  
  /for SAC_Counter 1 to ${${SAC_Source}.Size}
    /varset ${SAC_Target}[${SAC_Counter}] ${${SAC_Source}[${SAC_Counter}]}
  /next SAC_Counter
/return

| ********************************************************************
| Adds a sertain number of slots to an array.
| ********************************************************************
Sub SingleArrayAdd(string SAA_Array, int SAA_Size, string SAA_Type)
| Simple errorchecking to see if the array is declared and that size is larger than 0
  /if (!${Defined[${SAA_Array}]}) {
    /echo SingleArrayAdd -> ${SAA_Array} is not defined. Declare the array before trying to add slots.
    /return
  }
  /if (${SAA_Size}<=0) {
    /echo SingleArrayAdd -> Size has to be over 0.
    /return
  }
  
| Local variables.
  /declare SAA_WorkArray[${${SAA_Array}.Size}] ${SAA_Type} local
  /declare SAA_Counter int local

| Copies the values from the main array over to the workarray.  
  /for SAA_Counter 1 to ${${SAA_Array}.Size}
    /varset SAA_WorkArray[${SAA_Counter}] ${${SAA_Array}[${SAA_Counter}]}
  /next SAA_Counter
  
| Destroying the main array and redeclaring it with the new size.
  /deletevar ${SAA_Array}
  /declare ${SAA_Array}[${Math.Calc[${SAA_WorkArray.Size}+${SAA_Size}].Int}] ${SAA_Type} outer

| Copies the values form workarray back to main array.
  /for SAA_Counter 1 to ${SAA_WorkArray.Size}
    /varset ${SAA_Array}[${SAA_Counter}] ${SAA_WorkArray[${SAA_Counter}]}
  /next SAA_Counter
/return

| ********************************************************************
| Resizes an array to a new size.
| ********************************************************************
Sub SingleArrayReSize(string SARS_Array, int SARS_Size, string SARS_Type)
| Simple errorchecking to see if the array is declared and that the new size is larger than the arrays.
  /if (!${Defined[${SARS_Array}]}) {
    /echo SingleArrayReSize -> ${SARS_Array} is not defined. Declare the array before trying to resize it.
    /return
  }
  /if (${SARS_Size}<${${SARS_Array}.Size}) {
    /echo SingleArrayReSize -> ${SARS_Size} is smaller that the size of ${SARS_Array}.
    /return
  }

| Local variables.
  /declare SARS_WorkArray[${${SARS_Array}.Size}] ${SARS_Type} local
  /declare SARS_Counter int local
  
| Copies the values from main array over to the workarray.
  /for SARS_Counter 1 to ${${SARS_Array}.Size}
    /varset WorkArray[${SARS_Counter}] ${${SARS_Array}[${SARS_Counter}]}
  /next SARS_Counter
  
| Destrouing the main array and redeclaring it with the new size.
  /deletevar ${SARS_Array}
  /declare ${SARS_Array}[${SARS_Size}] ${SARS_Type} outer
  
| Copies the values form workarray back to main array.
  /for SARS_Counter 1 to ${SARS_WorkArray.Size}
    /varset ${SARS_Array}[${SARS_Counter}] ${SARS_WorkArray[${SARS_Counter}]}
  /next SARS_Counter
/return

| ********************************************************************
| Copies a multidimensional array from one array to another.
| ********************************************************************
Sub MultiArrayCopy(string MAC_Source, string MAC_Target, string MAC_Type)
| Simple errorchecking to see if the array is declared.
  /if (!${Defined[${MAC_Source}]})
    /echo MultiArrayCopy -> ${MAC_Source} is not defined. Declare the array before trying to copy it.
    /return
  }
  
| Local variables.
  /declare MAC_Counter int local
  /declare MAC_ArrayCounter int local
  /declare MAC_DimensionString string local
  /declare MAC_DimensionCounters[${${MAC_Source}.Dimensions}] int local 1

| Builds a string containing all the array dimension sizes for use in declaring the target array.
  /for MAC_Counter 1 to ${${MAC_Source}.Dimensions}
    /varset MAC_DimensionString ${MAC_DimensionString}${${MAC_Source},Size[${MAC_Counter}]}
    /if (${MAC_Counter}!=${${MAC_Source}.Dimensions}) /varset MAC_DimensionString ${MAC_DimensionString},
  /next MAC_Counter

| Checks to see if the target array is allready declared and deletes it if it is and redeclaring it.
  /if (${Defined[${MAC_Target}]}) /deletevar ${MAC_Target}
  /declare ${MAC_Target}[${MAC_DimensionString}] ${MAC_Type} outer
  
| Copies the values from the source array to the targeted array.  
  /for MAC_ArrayCounter 1 to ${${MAC_Source}.Size}
| Builds another dimensionstring for use with the /varset command.
    /varset MAC_DimensionString
    /for MAC_Counter 1 to ${MAC_DimensionCounters.Size}
      /varset MAC_DimensionString ${MAC_DimensionString}${MAC_DimensionCounters[${MAC_Counter}]}
      /if (${MAC_Counter}!=${MAC_DimensionCounters.Size}) /varset MAC_DimensionString ${MAC_DimensionString},
    /next MAC_Counter
    /varset ${MAC_Target}[${MAC_DimensionString}] ${${MAC_Source}[${MAC_DimensionString}]}

| Calculates the next items location inside the main array.
    /varset MAC_Counter 1
    :MAC_Loop
      /varcalc MAC_DimensionCounters[${MAC_Counter}] ${MAC_DimensionCounters[${MAC_Counter}]}+1
      /if (${MAC_DimensionCounters[${MAC_Counter}]}>${${MAC_Source}.Size[${MAC_Counter}]}) {
        /varset MAC_DimensionCounters[${MAC_Counter}] 1
        /varcalc MAC_Counter ${MAC_Counter}+1
        /if (${MAC_Counter}>${MAC_DimensionCounters.Size}) /goto :MAC_EndLoop
        /goto :MAC_Loop
      } else /goto :MAC_EndLoop
    :MAC_EndLoop
  /next MAC_ArrayCounter
/return

| ********************************************************************
| Adds a sertain number of slots to one of the dimensions in an array.
| ********************************************************************
Sub MultiArrayAdd(string MAA_Array, int MAA_Dimension, int MAA_Size, string MAA_Type)
| Simple errorchecking to see if the array is declared and the new slot is to be added to an existing dimension.
  /if (!${Defined[${MAA_Array}]}) {
    /echo MultiArrayAdd -> ${MAA_Array} is not defined. Declare the array before trying to add slots.
    /return
  }
  /if (${MAA_Dimension}<1&&${MAA_Dimension}>${${MAA_Array}.Dimensions}) {
    /echo MultiArrayAdd -> ${MAA_Dimension} is out of bound for ${MAA_Array}
    /return
  }

| Local variables.
  /declare MAA_Counter int local
  /declare MAA_ArrayCounter int local
  /declare MAA_DimensionString string local
  /declare MAA_DimensionCounters[${${MAA_Array}.Dimensions}] int local 1
  
| Builds a string containing all the array dimension sizes for use in declaring a Temp array where the
| orginal array is copied to before exspansion.
  /for MAA_Counter 1 to ${${MAA_Array}.Dimensions}
    /varset MAA_DimensionString ${MAA_DimensionString}${${MAA_Array}.Size[${MAA_Counter}]}
    /if (${MAA_Counter}!=${${MAA_Array}.Dimensions}) /varset MAA_DimensionString ${MAA_DimensionString},
  /next MAA_Counter
  /declare MAA_WorkArray[${MAA_DimensionString}] ${MAA_Type} local
  
| Copies the original array to the MAA_WorkArray
  /for MAA_ArrayCounter 1 to ${${MAA_Array}.Size}
| Builds another dimensionstring for use with the /varset command.
    /varset MAA_DimensionString
    /for MAA_Counter 1 to ${MAA_DimensionCounters.Size}
      /varset MAA_DimensionString ${MAA_DimensionString}${MAA_DimensionCounters[${MAA_Counter}]}
      /if (${MAA_Counter}!=${MAA_DimensionCounters.Size}) /varset MAA_DimensionString ${MAA_DimensionString},
    /next MAA_Counter
    /varset MAA_WorkArray[${MAA_DimensionString}] ${${MAA_Array}[${MAA_DimensionString}]}

| Calculates the next items location inside the main array.
    /varset MAA_Counter 1
    :MAA_Loop1
      /varcalc MAA_DimensionCounters[${MAA_Counter}] ${MAA_DimensionCounters[${MAA_Counter}]}+1
      /if (${MAA_DimensionCounters[${MAA_Counter}]}>${${MAA_Array}.Size[${MAA_Counter}]}) {
        /varset MAA_DimensionCounters[${MAA_Counter}] 1
        /varcalc MAA_Counter ${MAA_Counter}+1
        /if (${MAA_Counter}>${MAA_DimensionCounters.Size}) /goto :MAA_EndLoop1
        /goto :MAA_Loop1
      } else /goto :MAA_EndLoop1
    :MAA_EndLoop1
  /next MAA_ArrayCounter

| Rebuilds the dimension string to include the added slots.  
  /varset MAA_DimensionString
  /for MAA_Counter 1 to ${${MAA_Array}.Dimensions}
    /if (${MAA_Counter}==${MAA_Dimension}) {
      /varset MAA_DimensionString ${MAA_DimensionString}${Math.Calc[${${MAA_Array}.Size[${MAA_Counter}]}+${MAA_Size}].Int}
    } else {
      /varset MAA_DimensionString ${MAA_DimensionString}${${MAA_Array}.Size[${MAA_Counter}]}
    }
    /if (${MAA_Counter}!=${${MAA_Array}.Dimensions}) /varset MAA_DimensionString ${MAA_DimensionString},    
  /next MAA_Counter
  
| Destroying the main array and redeclaring it with the new sizes.
  /deletevar ${MAA_Array}
  /declare ${MAA_Array}[${MAA_DimensionString}] ${MAA_Type} outer
  
| Copies the information stored in the MAA_WorkArray back into main array.
  /for MAA_ArrayCounter 1 to ${MAA_WorkArray.Size}
| Builds yet another dimensionstring to use with the /varset command    
    /varset MAA_DimensionString
    /for MAA_Counter 1 to ${MAA_DimensionCounters.Size}
      /varset MAA_DimensionString ${MAA_DimensionString}${MAA_DimensionCounters[${MAA_Counter}]}
      /if (${MAA_Counter}!=${MAA_DimensionCounters.Size}) /varset MAA_DimensionString ${MAA_DimensionString},
    /next MAA_Counter
    /varset ${MAA_Array}[${MAA_DimensionString}] ${MAA_WorkArray[${MAA_DimensionString}]}
    
| Calculates the next item location inside the main array
    /varset MAA_Counter 1
    :MAA_Loop2
      /varcalc MAA_DimensionCounters[${MAA_Counter}] ${MAA_DimensionCounters[${MAA_Counter}]}+1
      /if (${MAA_DimensionCounters[${MAA_Counter}]}>${MAA_WorkArray.Size[${MAA_Counter}]}) {
        /varset MAA_DimensionCounters[${MAA_Counter}] 1
        /varcalc MAA_Counter ${MAA_Counter}+1
        /if (${MAA_Counter}>${MAA_DimensionCounters.Size}) /goto :MAA_EndLoop2
        /goto :MAA_Loop2
      } else /goto :MAA_EndLoop2
    :MAA_EndLoop2
  /next MAA_ArrayCounter
/return

| ********************************************************************
| Resizes one of the dimensions in an array to a new size.
| ********************************************************************
Sub MultiArrayReSize(String MARS_Array, int MARS_Dimension, int MARS_Size, string MARS_Type)
| Simple errorchecking to see if the array is declared and the new size is for an existing dimension
  /if (!${Defined[${MARS_Array}]}) {
    /echo MultiArrayReSize -> ${MARS_Array} is not defined. Declare the array before trying to resize it.
    /return
  }
  /if (${MARS_Dimension}<1&&${MARS_Dimension}>${${MARS_Array}.Dimensions}) {
    /echo MultiArrayReSize -> ${MARS_Dimension} is out of bound for ${MARS_Array}
    /return
  }

| Local variables.
  /declare MARS_ArrayCounter int local
  /declare MARS_Counter int local
  /declare MARS_DimensionString string local
  /declare MARS_DimensionCounters[${${MARS_Array}.Dimensions}] int local 1
  
| Builds a string containing all the array dimension sizes for use in declaring a Temp array where the
| orginal array is copied to before resizing.
  /for MARS_Counter 1 to ${${MARS_Array}.Dimensions}
    /varset MARS_DimensionString ${MARS_DimensionString}${${MARS_Array}.Size[${MARS_Counter}]}
    /if (${MARS_Counter}!=${${MARS_Array}.Dimensions}) /varset MARS_DimensionString ${MARS_DimensionString},
  /next MARS_Counter
  /declare MARS_WorkArray[${MARS_DimensionString}] ${MARS_Type} local
  
| Copies the original array to the MARS_WorkArray
  /for MARS_ArrayCounter 1 to ${${MARS_Array}.Size}
| Builds another dimensionstring for use with the /varset command.
    /varset MARS_DimensionString
    /for MARS_Counter 1 to ${MARS_DimensionCounters.Size}
      /varset DimensionString ${MARS_DimensionString}${MARS_DimensionCounters[${MARS_Counter}]}
      /if (${MARS_Counter}!=${MARS_DimensionCounters.Size}) /varset MARS_DimensionString ${MARS_DimensionString},
    /next MARS_Counter
    /varset MARS_WorkArray[${MARS_DimensionString}] ${${MARS_Array}[${MARS_DimensionString}]}

| Calculates the next item location inside the main array
    /varset MARS_Counter 1
    :MARS_Loop1
      /varcalc MARS_DimensionCounters[${MARS_Counter}] ${MARS_DimensionCounters[${MARS_Counter}]}+1
      /if (${MARS_DimensionCounters[${MARS_Counter}]}>${${MARS_Array}.Size[${MARS_Counter}]}) {
        /varset MARS_DimensionCounters[${MARS_Counter}] 1
        /varcalc MARS_Counter ${MARS_Counter}+1
        /if (${MARS_Counter}>${MARS_DimensionCounters.Size}) /goto :MARS_EndLoop1
        /goto :MARS_Loop1
      } else /goto :MARS_EndLoop1
    :MARS_EndLoop1
  /next MARS_ArrayCounter
  
| Rebuilds the dimension string to include the resized dimension.
  /varset MARS_DimensionString
  /for MARS_Counter 1 to ${MARS_WorkArray.Dimensions}
    /if (${MARS_Counter}==${MARS_Dimension}) {
      /varset MARS_DimensionString ${MARS_DimensionString}${MARS_Dimension}
    } else {
      /varset MARS_DimensionString ${MARS_DimensionString}${MARS_WorkArray.Size[${MARS_Counter}]}
    }
    /if (${MARS_Counter}!=${MARS_WorkArray.Dimensions}) /varset MARS_DimensionString ${MARS_DimensionString},
  /next MARS_Counter

| Destroying the main array and redeclaring it with the new sizes.
  /deletevar ${MARS_Array}
  /declare ${MARS_Array}[${MARS_DimensionString}] ${MARS_Type} outer
  
| Copies the information stored in the MAA_WorkArray back into main array.
  /for MARS_ArrayCounter 1 to ${MARS_WorkArray.Size}
| Builds another dimensionstring for use with the /varset command.
    /varset MARS_DimensionString
    /for MARS_Counter 1 to ${MARS_DimensionCounters.Size}
      /varset DimensionString ${MARS_DimensionString}${MARS_DimensionCounters[${MARS_Counter}]}
      /if (${MARS_Counter}!=${MARS_DimensionCounters.Size}) /varset MARS_DimensionString ${MARS_DimensionString},
    /next MARS_Counter
    /varset ${MARS_Array}[${MARS_DimensionString}] ${MARS_WorkArray[${MARS_DimensionString}]}
  
| Calculates the next item location inside the main array
    /varset MARS_Counter 1
    :MARS_Loop2
      /varcalc MARS_DimensionCounters[${MARS_Counter}] ${MARS_DimensionCounters[${MARS_Counter}]}+1
      /if (${MARS_DimensionCounters[${MARS_Counter}]}>${MARS_WorkArray.Size[${MARS_Counter}]}) {
        /varset MARS_DimensionCounters[${MARS_Counter}] 1
        /varcalc MARS_Counter ${MARS_Counter}+1
        /if (${MARS_Counter}>${MARS_DimensionCounters.Size}) /goto :MARS_EndLoop2
        /goto :MARS_Loop2
      } else /goto :MARS_EndLoop2
    :MARS_EndLoop2
  /next MARS_ArrayCounter
/return
Currected the missing $.

Nightcrawler7514
Last edited by Nightcrawler7514 on Thu Sep 09, 2004 7:52 pm, edited 1 time in total.

s0rcier
a grimling bloodguard
a grimling bloodguard
Posts: 876
Joined: Mon Aug 02, 2004 10:49 pm

Post by s0rcier » Mon Sep 06, 2004 1:28 am

in Sub SingleArrayAdd missing $ in the declare

Code: Select all

/declare ${SAA_Array}[${Math.Calc[${SAA_WorkArray.Size}+{SAA_Size}].Int}] ${SAA_Type} outer 
replace by

Code: Select all

/declare ${SAA_Array}[${Math.Calc[${SAA_WorkArray.Size}+${SAA_Size}].Int}] ${SAA_Type} outer 
I will post future problems as i see them! good works!
s0rCieR

s0rcier
a grimling bloodguard
a grimling bloodguard
Posts: 876
Joined: Mon Aug 02, 2004 10:49 pm

Post by s0rcier » Mon Sep 06, 2004 1:43 am

Other suggestion i could make is to make only one generic sub for single/multi ... u can easy find out if you are working on a sigle/multi dimensional array ...

"Optional parameters dimension" could be passed last so one sub could do both ...


ie:
Sub MultiArrayAdd(string MAA_Array, int MAA_Dimension, int MAA_Size, string MAA_Type)

Sub SingleArrayAdd(string SAA_Array, int SAA_Size, string SAA_Type)

could be something like

Sub Array_Add(string AA_Array,,string AA_Type,int AA_Size,int AA_Dimension)


Well it's just a suggestion!

s0rCieR!

Nightcrawler7514
orc pawn
orc pawn
Posts: 25
Joined: Mon Oct 27, 2003 6:43 pm
Location: Denmark

Post by Nightcrawler7514 » Thu Sep 09, 2004 7:56 pm

Well I have actually started another project insted of this one. Do to the many loops the functions are quite slow, so I have started to make a plugin insted, with the idea of combining the functions that do the same, only in multi and single, into one, and I hope I will get a much smother running that isnt that slow.

Nightcrawler7514

s0rcier
a grimling bloodguard
a grimling bloodguard
Posts: 876
Joined: Mon Aug 02, 2004 10:49 pm

Post by s0rcier » Mon Sep 13, 2004 11:28 pm

Well i'm happy you are working on a plugin instead, ya function are so slow :( i was disapointed when i benchmark a quicksort routine i implemented for sorting arrays :( (well single dimesion) ... here is my code if anyone want to use it ;P

Code: Select all

|==============================================================================
| Array_Sort (string _Array,string _Type,int _Dim)
|------------------------------------------------------------------------------
| _Array 				The Name of the array you want to sort.
| _Type					The Type of the sort Array. (string/int/float) etc.
| _Dim					The Dimension on array you want to sort.
|------------------------------------------------------------------------------
| Sort 1 dimension of the array using quicksort to get them in alpha/numerical
| orders. (Currently Supporting only 1st Dimesion)
|==============================================================================
Sub Array_Sort(string _Array,string _Type,int _Dim)
    /if (${Defined[_SortTest]}) /deletevar _SortTest
    /if (${Defined[_SortSwap]}) /deletevar _SortSwap
    /declare _SortSwap string outer _Array_Swap
    /declare _SortTest string outer ${If[${_Type.Equal[string]},_Array_Test_String,_Array_Test_Calcul]}
    /if (!${Defined[_Dim]}) /declare _Dim int local 1
    /call _Array_Sort ${_Array} 1 ${${_Array}.Size[${_Dim}]} 1
/return

Sub _Array_Sort(string Array,Lo,Hi,D)
    /declare L int local ${Lo}
    /declare R int local ${Hi}
    /declare P int local ${Math.Calc[(${Lo}+${Hi})\2]}
    :while1
    /if (${L}<${R}) {
        :while2
        /call ${_SortTest} ${Array} ${L} ${P} ${D}
        /if (${Int[${Macro.Return}]}<0) {
            /varcalc L ${L}+1
            /goto :while2
        }
        :while3
        /call ${_SortTest} ${Array} ${R} ${P} ${D}
        /if (${Int[${Macro.Return}]}>0) {
            /varcalc R ${R}-1
            /goto :while3
        }
        /if (${L}<=${R}) {
            /call ${_SortSwap} ${Array} ${L} ${R} ${D}
            /varcalc L ${L}+1
            /varcalc R ${R}-1
        }
        /goto :while1
    }
    /if (${Lo}<${R}) /call _Array_Sort ${Array} ${Lo} ${R} ${D}
    /if (${L}<${Hi}) /call _Array_Sort ${Array} ${L} ${Hi} ${D}
/return

Sub _Array_Swap(string Array,int Lo,int Hi,int D)
|buildup of xdimensional store place for the current value to be swapped to
|another xdimensional var not implemented yet ;(
    /declare S string local ${${Array}[${Lo}]}
    /varset ${Array}[${Lo}] ${${Array}[${Hi}]}
    /varset ${Array}[${Hi}] ${S}
/return

Sub _Array_Test_Calcul(string Array,int Lo,int Hi,int D)
|buildup of test for Lo,Hi variable in multi-dimesion is not implemented yet.
/return ${Int[${Math.Calc[${${Array}[${Lo}]}-${${Array}[${Hi}]}]}]}

Sub _Array_Test_String(string Array,int Lo,int Hi,int D)
|buildup of test for Lo,Hi variable in multi-dimesion is not implemented yet.
/return ${${Array}[${Lo}].Compare[${${Array}[${Hi}]}]}
sorting 100 string record took over 12secs :( should be done in less then 3 easy :(

s0RCieR

Nightcrawler7514
orc pawn
orc pawn
Posts: 25
Joined: Mon Oct 27, 2003 6:43 pm
Location: Denmark

Post by Nightcrawler7514 » Tue Sep 14, 2004 3:27 pm

Hehe. Thats exagtly why Im making the plugin version insted. I timed a simple copy of a 3 dimensional array with a total of 1000 slots, and then dump it to screen. 1 min 30 sec for that operation. After that I thought:

Damn thats slow. hhhmmmm. I think I better start dusting off my C++ skills and make a good plugin.

And version 1.0 could have been done today, but I need to do some serious testing first before releasing it. But with the EQ update today that isnt gonna happen before we have the new offsets. And that is something I can't find myself. Or rather will not do, because I will just get them wrong anyway.

Nightcrawler7514