Difference between revisions of "User:QQuix/Array sample code"

Converted part of the text to an User Function
imported>QQuix
m (User:QQuix/Array deep dump - sample code moved to User:QQuix/Array sample code: More generic title as new samples were added)
imported>QQuix
(Converted part of the text to an User Function)
 
|}
</dl></dl>
 
 
 
 
=Array walk=
 
==Introduction==
This sample code allows walking a tree-like array structure
 
Useful when one needs to access array data sequentially, one at a time, over many frames.
 
The code allow for returning the previous or the next entry in the structure. When one branch ends, it jumps to the first entry of the next branch.
 
This sample works on a multi level, tree like array. Each 'branch' of the tree being a StringMap array. The object at the end of the branch must not be a StringMap. Branches mey have different sizes.
 
==Setup==
 
This sample uses the following objects:
* qqArrayWalk - a persistent object that holds the script.
* qqGetNext, qqGetPrev and qqReset - persistent objects to be used as actionref
 
===Usage===
 
To get the next entry:
qqArrayWalk.Activate qqGetNext 1
let MyObject := qqArrayWalk.aReturnObject
 
To get the previous entry:
qqArrayWalk.Activate qqGetPrev 1
let MyObject := qqArrayWalk.aReturnObject
 
To reset the loop to the first entry of the first branch:
qqArrayWalk.Activate qqGetNext, 1
let MyObject := qqArrayWalk.aReturnObject
 
 
==Script==
 
scn qqArrayTreeWalkSCRIPT
array_var aReturnObject
ref xSelf
;-------------------------------------------------------
; These arrays hold the branch and key data for each level as the loop goes down a branch
; Used to walk the branch back up
;-------------------------------------------------------
array_var aBranches
array_var aKeys
;-------------------------------------------------------
; These vars hold the current position
;-------------------------------------------------------
array_var aBranch
string_var sKey
short xLevel
string_var sType
short xForward
begin onactivate
if getactionref == qqGetNext
set xForward to 1
elseif getactionref == qqGetPrev
set xForward to 0
endif
if getactionref == qqReset || xSelf == 0
;-----------------------------------------------
; [Re]initialize the loop
;-----------------------------------------------
set xSelf to getself
if aBranches == 0
let aBranches := ar_construct array
let aKeys := ar_construct array
endif
set xLevel to 0
let aBranch := g.gaObjectTree ; The array to be walked
let sKey := ar_Last aBranch
set xForward to 1
endif
if xForward
let sKey := ar_Next aBranch sKey
else
let sKey := ar_Prev aBranch sKey
endif
saveip 4
if eval ( sKey == ar_BadStringIndex )
set xLevel to xLevel - 1
if xLevel >= 0
;-----------------------------------------------------------
; End of array - go up one level
;-----------------------------------------------------------
let aBranch := aBranches [xLevel]
let sKey := aKeys [xLevel]
if xForward
let sKey := ar_Next aBranch sKey
else
let sKey := ar_Prev aBranch sKey
endif
goto 4
else
;-----------------------------------------------------------
; End of last array - start over
;-----------------------------------------------------------
set xLevel to 0
let aBranch := g.gaObjectTree
if xForward
let sKey := ar_First aBranch
else
let sKey := ar_Last aBranch
endif
goto 4
endif
endif
let sType := typeof aBranch [sKey]
if eval (sType == "StringMap")
;-----------------------------------------------------------
; Entry is an array - go down one level
;-----------------------------------------------------------
let aBranches [xLevel] := aBranch
let aKeys [xLevel] := sKey
set xLevel to xLevel + 1
let aBranch := aBranch [sKey]
if xForward
let sKey := ar_First aBranch
else
let sKey := ar_Last aBranch
endif
goto 4
else
;-----------------------------------------------------------
; End of branch - set aReturnObject
;-----------------------------------------------------------
let aReturnObject := aBranch [sKey]
endif
end
 
=Array deep dump=
<dl>
{| class="wikitable" style="width:90%; text-align:left"
| align="left" width="600"|Deprecated. Replaced by [[ArrayDeepDump]] ([[User Function]])
|}
 
</dl>
 
 
==Introduction==
endif
loop
endif
end
 
 
 
=Array walk=
 
==Introduction==
This sample code allows walking a tree-like array structure
 
Useful when one needs to access array data sequentially, one at a time, over many frames.
 
The code allow for returning the previous or the next entry in the structure. When one branch ends, it jumps to the first entry of the next branch.
 
This sample works on a multi level, tree like array. Each 'branch' of the tree being a StringMap array. The object at the end of the branch must not be a StringMap. Branches mey have different sizes.
 
==Setup==
 
This sample uses the following objects:
* qqArrayWalk - a persistent object that holds the script.
* qqGetNext, qqGetPrev and qqReset - persistent objects to be used as actionref
 
===Usage===
 
To get the next entry:
qqArrayWalk.Activate qqGetNext 1
let MyObject := qqArrayWalk.aReturnObject
 
To get the previous entry:
qqArrayWalk.Activate qqGetPrev 1
let MyObject := qqArrayWalk.aReturnObject
 
To reset the loop to the first entry of the first branch:
qqArrayWalk.Activate qqGetNext, 1
let MyObject := qqArrayWalk.aReturnObject
 
 
==Script==
 
scn qqArrayTreeWalkSCRIPT
array_var aReturnObject
ref xSelf
;-------------------------------------------------------
; These arrays hold the branch and key data for each level as the loop goes down a branch
; Used to walk the branch back up
;-------------------------------------------------------
array_var aBranches
array_var aKeys
;-------------------------------------------------------
; These vars hold the current position
;-------------------------------------------------------
array_var aBranch
string_var sKey
short xLevel
string_var sType
short xForward
begin onactivate
if getactionref == qqGetNext
set xForward to 1
elseif getactionref == qqGetPrev
set xForward to 0
endif
if getactionref == qqReset || xSelf == 0
;-----------------------------------------------
; [Re]initialize the loop
;-----------------------------------------------
set xSelf to getself
if aBranches == 0
let aBranches := ar_construct array
let aKeys := ar_construct array
endif
set xLevel to 0
let aBranch := g.gaObjectTree ; The array to be walked
let sKey := ar_Last aBranch
set xForward to 1
endif
if xForward
let sKey := ar_Next aBranch sKey
else
let sKey := ar_Prev aBranch sKey
endif
saveip 4
if eval ( sKey == ar_BadStringIndex )
set xLevel to xLevel - 1
if xLevel >= 0
;-----------------------------------------------------------
; End of array - go up one level
;-----------------------------------------------------------
let aBranch := aBranches [xLevel]
let sKey := aKeys [xLevel]
if xForward
let sKey := ar_Next aBranch sKey
else
let sKey := ar_Prev aBranch sKey
endif
goto 4
else
;-----------------------------------------------------------
; End of last array - start over
;-----------------------------------------------------------
set xLevel to 0
let aBranch := g.gaObjectTree
if xForward
let sKey := ar_First aBranch
else
let sKey := ar_Last aBranch
endif
goto 4
endif
endif
let sType := typeof aBranch [sKey]
if eval (sType == "StringMap")
;-----------------------------------------------------------
; Entry is an array - go down one level
;-----------------------------------------------------------
let aBranches [xLevel] := aBranch
let aKeys [xLevel] := sKey
set xLevel to xLevel + 1
let aBranch := aBranch [sKey]
if xForward
let sKey := ar_First aBranch
else
let sKey := ar_Last aBranch
endif
goto 4
else
;-----------------------------------------------------------
; End of branch - set aReturnObject
;-----------------------------------------------------------
let aReturnObject := aBranch [sKey]
endif
Anonymous user