Class SORTEDARRAY

Name

SORTEDARRAY  --  Class are destined to control of sorted data.

Synopsis

 tSortedArrayNew() 	--> SortedArray object
 tSortedArray() 		--> SortedArray object

Description

Class are destined to control of sorted data.

Attributes

<Items> Array, an array of items for sorting
<Error> String, when error arisen, <Error> contains error description
<Found> Logical, if TRUE, then item founded by seeking.

Methods

ADD Add new element to sorted array.
DEL Removes some data from SORTEDARRAY.
DELITEM Delete element from sorted array by index.
GETDATA Get element data by index in sorted array.
GETFIRST Get first item data are sorted array.
GETITEM Get item are sorted array by index.
GETKEY Get element keys by index in sorted array.
GETLAST Get last item data are sorted array.
HARDSEEK Hard seek element in sorted array.
LEN Returns count items into SORTEDARRAY.
SEEK Seek element in sorted array.
SOFTSEEK Soft seek element in sorted array.
TSORTEDARRAYNEW SORTEDARRAY constructor.

Method SORTEDARRAY:ADD()

 Add(<nKey>, <Value>) 	--> .T./.F.

Add() is added new item to class SortedArray with key <nKey> and value <Value>. Every item of sorted array is array containes only two elements: key (<nKey>) and value (<Value>). Items with low keys <nKey> are sorted toward the top of the array. If <nKey> or <Value> not specified then attribute <Error> will be contain error description.

You can add several items with identical keys. In this case class SortedArray will contain all these items and item can be inserted at suitable place.

Method SORTEDARRAY:DEL()

 Del(<nKey>, <Value>)    --> .T. || .F.
 Delete(<nKey>, <Value>) --> .T. || .F.
 DelKey(<nKey>, <Value>) --> .T. || .F.

Del(), Delete(), DelKey() is deleted item from sorted array by key and value.

If parameters <nKey> or <Value> not spacified then methods returns FALSE and write error description to attribute <Error>.

When element with <nKey> and <Value> not found methods returns FALSE and write error description to attribute <Error>.

Method SORTEDARRAY:DELITEM()

 DelItem(<nPos>)    --> .T. || .F.

DelItem() is removed item from sorted array by index and returns TRUE.

If <nPos> without of bound sorted array then method return FALSE and write to attribute <Error> error descriptions.

Method SORTEDARRAY:GETDATA()

 GetData(<nPos>)    --> data

GetData() gets data value by item index in sorted array and returns it if found, otherwise it returns NIL. If index is without of bound sorted array or is NIL then attribute <Error> to be keep error description.

Method SORTEDARRAY:GETFIRST()

 GetFirst()    --> data value

GetFirst() gets data value first items sorted array and returns it.

If no items in sorted array it returns NIL.

Method SORTEDARRAY:GETITEM()

 GetItem(<nPos>)    --> aItem

GetItem() gets key value and data value by item index in sorted array and returns array containes it. In other case it returns NIL. If index is without of bound sorted array or is NIL then attribute <Error> to be keep error description.

Method SORTEDARRAY:GETKEY()

 GetKey(<nPos>)    --> nKeyValue

GetKey() gets key value by item index in sorted array and returns it if found, otherwise it returns NIL. If index is without of bound sorted array or is NIL then attribute <Error> to be keep error description.

Method SORTEDARRAY:GETLAST()

 GetLast()    --> data value

GetLast() gets data value last added items in to sorted array and returns it. If no items in sorted array it returns NIL.

Method SORTEDARRAY:HARDSEEK()

 HardSeek(<nKey>)    --> nPos

HardSeek() calls method Seek() as Seek(<nKey>, .F.)

Method SORTEDARRAY:LEN()

 Len() 	--> nCount

Len() is returned sorted array length what equal lenght of <Items>.

If sorted array is empty, then method returns zero.

Method SORTEDARRAY:SEEK()

 Seek(<nKey>[, <lSoft>])    --> nPos

Seek() moves to the first item whose key is equal to <nKey>. If such a item is found, it becomes to current item and Seek() returns item index in sorted array, attribute <Found> sets to TRUE, otherwise it returns Len() +1 position and attribute <Found> sets to FALSE The positioning in sorted array is as followes: for normal (hard) seek (lSoft is .F.), the sorted array is positioned to Len() + 1; for a soft seek, the sorted array positioned to the first item whose key value is greather than specified key <nKey>. If no such item exist, the sorted array positioned to Len()+1 and returns Len() + 1 and sets attribute <Found> sets to FALSE.

Method SORTEDARRAY:SOFTSEEK()

 SoftSeek(<nKey>)    --> nPos

SoftSeek() calls method Seek() as Seek(<nKey>, .T.)

Method SORTEDARRAY:TSORTEDARRAYNEW()

 tSortedArrayNew() 	--> SortedArray object
 tSortedArray() 		--> SortedArray object

tSortedArrayNew() is constructs and returns new sorted array object. That class can be used to storage, access, control sorted data.

Example:

  sArr := tSortedArrayNew()
 sArr:Add("January"  , {1 , "January"  })
 sArr:Add("February" , {2 , "February" })
 sArr:Add("March"    , {3 , "March"    })
 sArr:Add("April"    , {4 , "April"    })
 sArr:Add("May"	    , {5 , "May"      })
 sArr:Add("June"	    , {6 , "June"     })
 sArr:Add("July"	    , {7 , "July"     })
 sArr:Add("August"   , {8 , "August"   })
 sArr:Add("September", {9 , "September"})
 sArr:Add("October"  , {10, "October"  })
 sArr:Add("November" , {11, "November" })
 sArr:Add("December" , {12, "December" })
 
 ? sArr:Len()			// 12
 
 ? sArr:GetFirst()		// {         4, April}
 ? sArr:GetLast()		// {         9, September}
 
 ? sArr:Seek("May")		// 9
 ? sArr:Seek("Monday")		// 13
 ? sArr:SoftSeek("Monday")	// 10
 ? sArr:HardSeek("Monday")	// 13
 
 ? sArr:GetItem(3)		// {December, {        12, December}}
 ? sArr:GetData(3)		// {        12, December}
 
 ? sArr:DelItem(3)		// .T.
 ? sArr:Len()			// 11
 
 ? sArr:GetItem(3)		// {February, {         2, February}}
 ? sArr:GetData(3)		// {         2, February}
 

Platforms

No dependies of platform.