Difference between revisions of "GetAt - FormList"

From the CreationKit Wiki
Jump to navigation Jump to search
imported>Tunaisafish
(→‎Examples: Added binary search method)
imported>JustinOther
m (→‎Examples: Optimized examples per talk page/listed examples separately)
Line 18: Line 18:


== Examples ==
== Examples ==
<source lang="papyrus">
*Print out the forms in the list
; Print out the forms in the list
<source lang="papyrus">Int iIndex = kFormList.GetSize()
FormList property list auto
While iIndex > 0
int index = 0
iIndex -= 1
While (index < list.GetSize())
Debug.Trace("Form " + iIndex + " is " + kFormList.GetAt(iIndex))
  Debug.Trace("Form " + index + " is " + list.GetAt(index))
EndWhile</source>
  index = index + 1
EndWhile
</source>


<source lang="papyrus">
*Get the index of member in list. Return -1 if member is not found.
int Function GetFormIndex(FormList List, Form Member) global
<source lang="papyrus">Int Function iGetFormIndex(FormList akList, Form akMember) Global
{Gets the index of Member in List. Returns -1 if not found}
If akList.HasForm(akMember)
if (!List.HasForm(Member))
Int iIndex = akList.GetSize() ; Will always return a finite value
Return -1
While iIndex > 0
endif
iIndex -= 1
 
If akList.GetAt(iIndex) == akMember
int Index = 0
Return iIndex
While (List.GetAt(Index) != Member)
EndIf
Index += 1
EndWhile
EndWhile
EndIf
 
Return -1 ; Either the form is not in the list, is not currently loaded if non-persistent
Return Index
EndFunction</source>
EndFunction
* Search a LONG FormList of ORDERED FORMS (by FormID) and return. -1 if not found. Binary Search Method adapted to Papyrus from Wikipedia's [[http://en.wikipedia.org/wiki/Binary_search_algorithm#Deferred_detection_of_equality|deferred detection of equality]] example.
</source>
<source lang="papyrus">Int Function GetFormIndexOrdered(FormList akList, Form akMember) Global
 
<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 iFormID = akMember.GetFormID()
Int imin = 0
Int iMin = 0
Int imax = List.GetSize() - 1
Int iMax = akList.GetSize() - 1


While (imax > imin)
While imax > iMin
Int imid = (imin + imax) / 2
Int iMid = (iMin + iMax) / 2
If (tgt > List.GetAt(imid).GetFormID())
If iFormID > akList.GetAt(iMid).GetFormID()
imin = imid + 1
iMin = iMid + 1
Else
Else
imax = imid
iMax = iMid
EndIf
EndIf
EndWhile
EndWhile
If ((imax == imin) && (tgt == List.GetAt(imin).GetFormID() ))
If (iMax == iMin) && (iFormID == akList.GetAt(iMin).GetFormID())
Return imin
Return iMin
Else
Else
Return -1
Return -1
EndIf
EndIf
EndFunction
EndFunction</source>
</source>


== See Also ==
== See Also ==

Revision as of 12:40, 12 October 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
Int iIndex = kFormList.GetSize()
While iIndex > 0
	iIndex -= 1
	Debug.Trace("Form " + iIndex + " is " + kFormList.GetAt(iIndex))
EndWhile
  • Get the index of member in list. Return -1 if member is not found.
Int Function iGetFormIndex(FormList akList, Form akMember) Global
	If akList.HasForm(akMember)
		Int iIndex = akList.GetSize() ; Will always return a finite value
		While iIndex > 0
			iIndex -= 1
			If akList.GetAt(iIndex) == akMember
				Return iIndex
			EndIf
		EndWhile
	EndIf
	Return -1 ; Either the form is not in the list, is not currently loaded if non-persistent
EndFunction
  • Search a LONG FormList of ORDERED FORMS (by FormID) and return. -1 if not found. Binary Search Method adapted to Papyrus from Wikipedia's [detection of equality] example.
Int Function GetFormIndexOrdered(FormList akList, Form akMember) Global

	Int iFormID = akMember.GetFormID()
	Int iMin = 0
	Int iMax = akList.GetSize() - 1

	While imax > iMin
		Int iMid = (iMin + iMax) / 2
		If iFormID > akList.GetAt(iMid).GetFormID()
			iMin = iMid + 1
		Else
			iMax = iMid
		EndIf
	EndWhile
	
	If (iMax == iMin) && (iFormID == akList.GetAt(iMin).GetFormID())
		Return iMin
	Else
		Return -1
	EndIf
EndFunction

See Also