- Created by Mark Juras , last modified on Jul 30, 2018
You are viewing an old version of this page. View the current version.
Compare with Current View Page History
« Previous Version 3 Current »
The SortPair Service Class
The service class SortPair maintains an open ended list of entry-value pairs in sort order by value. Pairs can be retrieved by their value and then the corresponding entry is immediately available. The algorithm underlying the sortpair methods is designed to make the positioning into the list at or near a particular specified value as quick as possible. Once this positioning has been achieved, the operation of writing a new entry to the list is optimal. In particular, the problem is to maintain various lists of values in sorted ascending order. The algorithm needs to be able to add new values to the list with no guarantee that those values were not already there. To make sorting possible, values are assigned binary representation types and each type has a technique available which compares a current value to another and computes a signed comparison value. A negative value means that the current value is below the other one in their sort order; a zero value means that the two values are equal with regards to their sort order; and a positive value means that the current value is greater than the other one. Using the Java terminolgy we will refer to this technique as "compareTo(newValue,listValue)" Given that we know how to compare individual values, an addEntryToList algorithm is now needed which will return the index in the sorted list where the new value was stored or a -1 if the value was already in the list. The brute-force way to perform this operation would be to simply scan forward in the list comparing the new value to each value in the list.int addEntryToList(object list[], object new, int nList) { for(i = 0; i < nList; i++) { int iRelation = compareTo(object,list[i])); if(iRelation == 0) return -1; if(iRelation > 0) { insert object before i in list; return i; } } append object to list; return nList; }
Value | Meaning |
+seq | A positive search index means that the entry is presently in the list. The seq value is the sequence number relative to one of the actual position of the entry in the list. |
0 | A zero value indicates that the entry is not in the list and is below all entries in the sort order. |
-seq | A negative search index means that the entry is not presently in the list, but it is above some entries now in the list in the sort order. The seq value is the negative sequence number relative to one of that entry in the list immediately below the entry itself. |
The algorithm for computing the search index is shown below.
int searchIndex(Value list[],Value new,int nList) { int iBottom = 0; int iMiddle = 0; int iRelation = 0; int iTop = nList - 1; while(iBottom <= iTop) { iMiddle = (iBottom + iTop) / 2; iRelation = compareTo(new,list[iMiddle]); if(iRelation == 0) return iMiddle + 1; if(iRelation < 0) iTop = iMiddle - 1; else iBottom = iMiddle + 1; } if(iRelation > 0) return -(iMiddle + 1); else if(iMiddle == 0) return 0; return -iMiddle; }
int addEntryToList(object list[], object new, int nList) { int searchIndex = searchIndex(list,new,nList) if(SearchIndex > 0) return -1; if(searchIndex < 0) { int i = -searchIndex; insert object before i in list; return i; } append object to list; return nList; }
SORT_MAX_LEVELS*(searchTime(maxPairsInList)+insertTime(maxPairsInList)), where: SORT_MAX_LEVES = 10 maxPairsInList = 31.
The field SortCaseSensitive
Prototypeextern int SortCaseSensitive;
The method SortPair_ChangePosting
Prototypevoid SortPair_ChangePosting(void* listStore,int basePoint,int index,int value);
Parameter | Description |
listStore | Specifies the handle for the LongMemory area that contains the posting pair. |
basePoint | Specifies the root offset of the posting pair in the LongMemory area as returned by the method SortPair_CreatePostingPair. |
index | Specifies the index value for the pair. This is the value used to find the pair. This method assumes that a pair with the specified index already exists. |
value | Specifies the new value to be associated with the pair. |
The method SortPair_Close
Prototypevoid SortPair_Close(void* This);
Parameter | Description |
This | Specifies the handle of the information structure controlling the sort pair list. |
The method SortPair_Create
Prototypeint SortPair_Create(void* This,void* listStore,int valueType,int blkSize);
Parameter | Description |
This | Specifies the handle of the information structure controlling the sort pair list. The memory for this structure most be allocated as an int vector and managed by the caller via a reference to the SortPair_Stack constant in the header. A typical declaration is "auto int sortPair[SortPair_StackSize];". |
listStore | Specifies the handle for the LongMemory area that contains the sort pair. |
valueType | Specifies the type of value being used to order the entries in the list. This must be one of the basic representation type constants REP_* from the header. |
blkSize | Specifies the block size to be used for the sorted values. If zero, then the value of LongMemory_GetPostingSize() is used. |
If all goes well then the method returns the location in the LongMemory area of the control information vector for the sort pair list. If there is a problem, then a zero is returned.
The method SortPair_CreatePostingPair
Prototypeint SortPair_CreatePostingPair(void* listStore);
Parameter | Description |
listStore | Specifies the handle for the LongMemory area that contains the posting pair. |
If all goes well, then the method returns the root offset in the LongMemory area of the control information vector for the posting pair. If there is a problem, then a zero is returned.
The method SortPair_Find
Prototypeint* SortPair_Find(void* This,void* value,int delta);
Parameter | Description |
This | Specifies the handle for the information structure controlling the sort pair list. |
value | Specifies the value being sought in the list. It must be of the type specified when the list was created. |
delta | Specifies whether the caller wants to change the pair value. A nonzero value indicates yes, while a zero value indicates no. |
If there is a pair in the list with the specified value, then a pointer to that pair is returned. If there is no pair with a matching value then a NULL is returned.
The method SortPair_FindPosting
Prototypeint SortPair_FindPosting(void* listStore,int basePoint,int pairIndex);
Parameter | Description |
listStore | Specifies the handle for the LongMemory area that contains the posting pair. |
basePoint | Specifies the root offset of the posting pair in the LongMemory area as returned by the method SortPair_CreatePostingPair. |
pairIndex | Specifies the index value for which a corresponding posting value is desired. |
If the posting pair contains an entry for the index, then its associated value is returned; else a zero is returned.
The method SortPair_GetLocation
Prototypevoid SortPair_GetLocation(void* This,int* location1,int* location2);
Parameter | Description |
This | Specifies the handle for the information structure controlling the sort pair list. |
location1 | returns the primary location value stored in the sort pair structure, if not NULL. |
location2 | returns the secondary location value stored in the sort pair structure, if not NULL. |
The method SortPair_Next
Prototypeint* SortPair_Next(void* Store,tPosting* PostSet);
Parameter | Description |
Store | Specifies the handle for the LongMemory area that contains the sorted pair list. Note that this handle is returned by the SortPair_Rewind method when it initializes the iterator. |
PostSet | Contains the user allocated structure tPostSet which controls the movement though pair postings. |
If there is another pair in the sorted list, then a pointer to that pair is returned. If there is no additional pair, then a NULL is returned.
The method SortPair_Position
Prototypeint* SortPair_Position(void* This,tPosting* PostSet,void* value);
Parameter | Description |
This | Specifies the handle for the information structure controlling the sort pair list. |
PostSet | Specifies a user allocated structure tPostSet that controls the movement though pair postings. |
value | Specifies the value being sought in the list. It must be of the type specified when the list was created. |
If there is no pair in the sorted list that is greater than or equal to the indicated value, then a NULL is returned; else a pointer to that pair is returned.
The method SortPair_Open
Prototypevoid SortPair_Open(void* This,void* listStore,int baseIndex,int blkSize);
Parameter | Description |
This | Contains the structure controlling the sort pair. The memory for this structure must be allocated and managed by the caller. |
listStore | Specifies the handle for the LongMemory area that contains the sort pair itself. |
baseIndex | Specifies the location in the LongMemory area of the initial block of the sort pair. |
blkSize | Specifies the block size to be used for the sorted values. If zero, then the value of LongMemory_GetPostingSize() is used. |
The method SortPair_Rewind
Prototypevoid* SortPair_Rewind(void* This,tPosting* PostSet);
Parameter | Description |
This | Specifies the handle for the information structure controlling the sort pair list. |
PostSet | Receives the information needed to iterate though the sort pair. It is a user allocated structure tPostSet. |
The method returns the handle for the storage area containing the sort pair list.
The method SortPair_SetLocation
Prototypevoid SortPair_SetLocation(void* This,int location1,int location2);
Parameter | Description |
This | Specifies the structure containing the sort pair information. |
location1 | Specifies the primary location value to be stored in the sort pair structure. |
location2 | Specifies the secondary location value to be stored in the sort pair structure. |
The method SortPair_Write
Prototypevoid SortPair_Write(void* This,int index,int value);
Parameter | Description |
This | Specifies the handle for the sort pair list which contains a valid search path. |
index | Specifies the index to be associated with value. This is typically either a sequence number or an offset into the LongMemory area. This value is stored by this method but it is not used. It can be anything that can be stored in a 32-bit word. |
value | Specifies the value to be associated with index. For simple representation types this is literally a value, for more complex types it is the address of a value. |
The method SortPair_WritePosting
Prototypeint SortPair_WritePosting(void* listStore,int basePoint,int index,int value);
Parameter | Description |
listStore | Specifies the handle that controls the LongMemory area that contains the posting pair. |
basePoint | Specifies the root offset of the posting pair in the LongMemory area as returned by the method SortPair_CreatePostingPair. |
index | Specifies the index value for the pair, This is the value used to find the pair. |
value | Specifies the value associated with the pair that is returned when it is found. |
If all goes well, the method returns a one. If the pair already exists and has a nonzero value associated with it, the method returns a zero.
Table of Contents
- No labels