Difference between revisions of "GetAt - FormList"
Jump to navigation
Jump to search
imported>Evernewjoy |
imported>Tunaisafish (→Examples: Added binary search method) |
||
Line 41: | Line 41: | ||
Return Index | Return Index | ||
EndFunction | |||
</source> | |||
<source lang="papyrus"> | |||
; Searching a LONG formlist of ORDERED FORMS (by FormID) | |||
; Binary Search Method adapted to Papyrus from | |||
; http://en.wikipedia.org/wiki/Binary_search_algorithm#Deferred_detection_of_equality | |||
Int Function GetFormIndexOrdered(FormList List, Form Member) Global | |||
{Gets the index of Member in an ordered List - by FormID. Returns -1 if not found} | |||
Int tgt = Member.GetFormID() | |||
Int imin = 0 | |||
Int imax = List.GetSize() - 1 | |||
While (imax > imin) | |||
Int imid = (imin + imax) / 2 | |||
If (tgt > List.GetAt(imid).GetFormID()) | |||
imin = imid + 1 | |||
Else | |||
imax = imid | |||
EndIf | |||
EndWhile | |||
If ((imax == imin) && (tgt == List.GetAt(imin).GetFormID() )) | |||
Return imin | |||
Else | |||
Return -1 | |||
EndIf | |||
EndFunction | EndFunction | ||
</source> | </source> |
Revision as of 15:45, 19 April 2012
Member of: FormList Script
Returns the form at a specified index in the list.
Syntax
Form Function GetAt(int aiIndex) native
Parameters
- aiIndex: The index in the list we want to fetch the form from
- The index is 0-based. If a list has 3 items in it, valid indices are: 0, 1 and 2
Return Value
Returns the form at index aiIndex, none in case of error (such as wrong index value)
Examples
; Print out the forms in the list
FormList property list auto
int index = 0
While (index < list.GetSize())
Debug.Trace("Form " + index + " is " + list.GetAt(index))
index = index + 1
EndWhile
int Function GetFormIndex(FormList List, Form Member) global
{Gets the index of Member in List. Returns -1 if not found}
if (!List.HasForm(Member))
Return -1
endif
int Index = 0
While (List.GetAt(Index) != Member)
Index += 1
EndWhile
Return Index
EndFunction
; Searching a LONG formlist of ORDERED FORMS (by FormID)
; Binary Search Method adapted to Papyrus from
; http://en.wikipedia.org/wiki/Binary_search_algorithm#Deferred_detection_of_equality
Int Function GetFormIndexOrdered(FormList List, Form Member) Global
{Gets the index of Member in an ordered List - by FormID. Returns -1 if not found}
Int tgt = Member.GetFormID()
Int imin = 0
Int imax = List.GetSize() - 1
While (imax > imin)
Int imid = (imin + imax) / 2
If (tgt > List.GetAt(imid).GetFormID())
imin = imid + 1
Else
imax = imid
EndIf
EndWhile
If ((imax == imin) && (tgt == List.GetAt(imin).GetFormID() ))
Return imin
Else
Return -1
EndIf
EndFunction