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

4,155 bytes added ,  10:06, 16 June 2009
Sample code added - Array walk
imported>QQuix
(Added a limit to the depth of the dump)
imported>QQuix
(Sample code added - Array walk)
Line 9: Line 9:
</dl></dl>
</dl></dl>


=Introduction=
=Array deep dump=
 
==Introduction==
With the availability of arrays in OBSE 0017, it is quite easy (an useful) to create tree-like arrays to keep data available and organized.
With the availability of arrays in OBSE 0017, it is quite easy (an useful) to create tree-like arrays to keep data available and organized.


Line 58: Line 60:
  [ObjectTree][Clutter] = #Empty#
  [ObjectTree][Clutter] = #Empty#
   
   
=Setup=
==Setup==


The following script must be attached to some persistent objects.
The following script must be attached to some persistent objects.
Line 83: Line 85:




=Script=
==Script==


  scn aaqqArrayDumpSCRIPT
  scn aaqqArrayDumpSCRIPT
Line 293: Line 295:
       endif
       endif
     loop
     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
   endif
  end
  end
Anonymous user