JoeCool Posted July 18, 2005 Share Posted July 18, 2005 (edited) Why ? Because u can't have an empty array ! So I start a vector kindof implementation. Where u can have an empty vector Really useful ! Nota, the first element is allways the size of the vector . expandcollapse popup#include-once func vectNew( $a1 = 0, $a2 = 0, $a3 = 0, $a4 = 0, $a5 = 0, $a6 = 0, $a7 = 0, $a8 = 0, $a9 = 0, $a10 = 0 ) local $p = @NumParams local $nv[1] local $i if $p > 0 then redim $nv[$p + 1] for $i = 1 to $p $nv[$i] = Eval( "a" & string( $i ) ) next $nv[0] = $p else $nv[0] = 0 endif return( $nv ) endfunc func vectAdd( byref $v, $a1 = 0, $a2 = 0, $a3 = 0, $a4 = 0, $a5 = 0, $a6 = 0, $a7 = 0, $a8 = 0, $a9 = 0, $a10 = 0 ) local $i, $vns local $p = @NumParams if $p > 1 then $vns = $v[0] + $p redim $v[$vns] for $i = 1 to $p - 1 $v[$v[0] + $i] = Eval( "a" & string( $i ) ) next $v[0] = $vns - 1 endif return( $v ) endfunc func vectInclude( byref $v, $val ) local $i for $i = 1 to $v[0] if $v[$i] = $val then return( $i ) endif next return( -1 ) endfunc func vectRemoveIndex( byref $v, $index ) local $i if $index > 0 and $index <= $v[0] then for $i = $index to $v[0] - 1 $v[$i] = $v[$i + 1] next redim $v[$v[0]] $v[0] -= 1 endif return( $v ) endfunc func vectDisplay( byref $v, $title ) local $i local $str = "" for $i = 0 To ubound( $v ) - 1 $str &= "[" & $i & "] = " & stringStripCR( $v[$i] ) & @CR next msgbox( 4096, $title, $str ) endfunc global $v = vectNew() global $i vectDisplay( $v, "v" ) vectAdd( $v, 1, 3, 4, 2 ) vectDisplay( $v, "v" ) $i = vectInclude( $v, 3 ) vectRemoveIndex( $v, $i ) vectDisplay( $v, "v" ) Edited July 18, 2005 by JoeCool Link to comment Share on other sites More sharing options...
blindwig Posted July 18, 2005 Share Posted July 18, 2005 Yes this is a great idea you are using. What you call a Vector is what is also called a 1-based array (since the first element is in the 1 index instead of the 0 index). All the routines I write use 1-based arrays, because you don't have to ReDim them all the time. My Array1Add() routine looks something like this: Func _Array1Add(ByRef $aSource, $vValue) $aSource[0] = $aSource[0] + 1 If $aSource[0] >= UBound($aSource) Then ReDim $aSource[$aSource[0] * 2] EndIf $aSource[$aSource[0]] = $vValue EndFunc I call my functions Array1* because they deal with 1-based arrays. My UDF Threads:Pseudo-Hash: Binary Trees, Flat TablesFiles: Filter by Attribute, Tree List, Recursive Find, Recursive Folders Size, exported to XMLArrays: Nested, Pull Common Elements, Display 2dSystem: Expand Environment Strings, List Drives, List USB DrivesMisc: Multi-Layer Progress Bars, Binary FlagsStrings: Find Char(s) in String, Find String in SetOther UDF Threads I Participated:Base64 Conversions Link to comment Share on other sites More sharing options...
JoeCool Posted July 22, 2005 Author Share Posted July 22, 2005 Func _Array1Add(ByRef $aSource, $vValue) $aSource[0] = $aSource[0] + 1 If $aSource[0] >= UBound($aSource) Then ReDim $aSource[$aSource[0] * 2] EndIf $aSource[$aSource[0]] = $vValue EndFuncI call my functions Array1* because they deal with 1-based arrays.<{POST_SNAPBACK}>Just a remark on your code, if u want to resize your array one in a while, use somethink like ReDim $aSource[$aSource[0] + 20 ] instead of ReDim $aSource[$aSource[0] * 2]so you while not "overgrow" your array for nothing when you get a big array(1000 elements or more ... )your array size now are 1, 2, 4, 8, 16, ..., 512, 1024, 2048 etc ...of course you can also shrink you array in the same way $aSource[$aSource[0] - 20 ];-) Link to comment Share on other sites More sharing options...
busysignal Posted July 22, 2005 Share Posted July 22, 2005 Fabulous.. Very nice code. You might want to document your functions so that their usage is much more clear. Cheers.. Link to comment Share on other sites More sharing options...
blindwig Posted July 22, 2005 Share Posted July 22, 2005 (edited) Just a remark on your code, if u want to resize your array one in a while, use somethink like ReDim $aSource[$aSource[0] + 20 ] instead of ReDim $aSource[$aSource[0] * 2]so you while not "overgrow" your array for nothing when you get a big array(1000 elements or more ... )your array size now are 1, 2, 4, 8, 16, ..., 512, 1024, 2048 etc ...of course you can also shrink you array in the same way $aSource[$aSource[0] - 20 ];-)<{POST_SNAPBACK}>I go back and forth on that issue. Usually I end up having to customize for the use I need. Sometimes I want an array to grow steadily (by adding 20 or 50 or so) and sometimes I just want to make sure that I do as few increases as possible. I usaully start my array out with 100 elements (Dim $aMyArray[101], and $aMyArray[0] evaluates to zero, so the first element will always be set to [1]) I figure that if I fill up 100 elements, it's not unreasonable to assume that I might fill up 100 more. And if I fill those 200, then it's not unreasonable to think that I might fill up 200 more, etc. So starting with 100 and doubling from there has been a good basis to start with. And like I said, I customize it if I need to.And for trimming the array - well, if you know you're done adding, then ReDim $aMyArray[$aMyArray[0] + 1] works pretty well. To see some examples of how I use 1-based arrays, see my Table and Binary Tree UDFs:http://www.autoitscript.com/forum/index.php?showtopic=13114http://www.autoitscript.com/forum/index.php?showtopic=12136 Edited July 22, 2005 by blindwig My UDF Threads:Pseudo-Hash: Binary Trees, Flat TablesFiles: Filter by Attribute, Tree List, Recursive Find, Recursive Folders Size, exported to XMLArrays: Nested, Pull Common Elements, Display 2dSystem: Expand Environment Strings, List Drives, List USB DrivesMisc: Multi-Layer Progress Bars, Binary FlagsStrings: Find Char(s) in String, Find String in SetOther UDF Threads I Participated:Base64 Conversions Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now