PrevNextUpHome BREW C++ Class Library & GUI Framework & XML Middleware : SophiaFramework 4.1

13.1. Collection Class

There are 4 types of collection classes: variable-length array, stack, list, and hashmap.

To add, update, delete, or retrieve items against the collection of multiple items, use the Collection class.

Table 13.1. Collection Class

Class Name Description
SFXArray Data structure for operating the variable-length array.
SFXStack Data structure for operating the stack.
SFXList Data structure for operating the two-way list.
SFXHashmap Data structure for operating the hashmap where its item consist of key and value.
[Caution] Limitation on the Collection Class

The item stored in the Collection class must not exceed 4 bytes in length. In case of the item more than 4 bytes, its pointer can be stored in the Collection class.

Reference: Collection of Instances

13.1.1. Variable-length Array Class

SFXArray is the data structure to store a number of items sequentially.

Example 13.1. Define the variable-length array

// Define variable-length array for storing items of SInt32
SFXArray<SInt32> array;

Example 13.2. Append items

array.InsertLast(3);
array.InsertLast(-15);
array.InsertLast(0);
array.InsertLast(1003);

// array = (3, -15, 0, 1003) 

Example 13.3. Set and get items

SInt32 n = array[1]; // n = -15

array[2] = 6;        // array = (3, -15, 6, 1003)

SInt32 m = array.GetFirst(); // m = 3
SInt32 r = array.GetLast();  // r = 1003

Example 13.4. Get the number of items

SInt32 n = array.GetSize(); // n = 4 

Example 13.5. Insert items

// insert 99 after second item
array.Insert(2, 99);     // array = (3, -15, 99, 6, 1003)

// insert -100 at first position
array.InsertFirst(-100); // array = (-100, 3, -15, 99, 6, 1003) 

Example 13.6. Delete items partially

// delete from array[3] to array[4]
array.Remove(3, 5); // array = (-100, 3, -15, 1003) 

Example 13.7. Delete all items

array.Clear(); // array = (): array becomes empty

Example 13.8. Extend the size of array

array.SetSize(5); // array = (undefined, undefined, undefined, undefined, undefined)

SInt32 i;
for (i = 0; i < array.GetSize(); ++i) {
    array[i] = 10 * (i - 2) * (i - 2);
}

// array becomes (40, 10, 0, 10, 40)

Example 13.9. Retrieve and check items

SInt32 n1 = array.FirstIndexOf(10);     // n1 = 1
SInt32 n2 = array.FirstIndexOf(5);      // n2 = -1
SInt32 n3 = array.LastIndexOf(10);      // n3 = 3
Bool b = array.Contains(40);            // b = true

Example 13.10. Process each item using the iterator 1

SFXArray<SInt32>::Iterator iterator = array.GetFirstIterator();
while(iterator.HasNext()) {
    SInt32 n = iterator.GetNext(); // n holds item

    // process n

}

The above code is similar to the following:

Example 13.11. Process each item using the iterator 2

SInt32 i;
for (i = 0; i < array.GetSize(); ++i) {
    SInt32 n = array[i]; // n holds item

    // process n

}

13.1.2. Stack Class

SFXStack is the data structure in which the most recently added item is removed first.

Example 13.12. Define the stack

// Define stack for storing items of SInt32
SFXStack<SInt32> stack;

Example 13.13. Push items

stack.Push(3);
stack.Push(-15);
stack.Push(22);
// stack becomes (3, -15, 22)

Example 13.14. Peek the top item

SInt32 r = stack.Peek(); // r = 22
// stack = ( 3 , -15 , 22 )

Example 13.15. Pop items

SInt32 n1 = stack.Pop(); // n1 = 22, Stack = ( 3 , -15 )
SInt32 n2 = stack.Pop(); // n2 = -15, Stack = ( 3 )
SInt32 n3 = stack.Pop(); // n3 = 3, Stack = ()

13.1.3. List Class

SFXList is the data structure for operating on the two-way list.

Example 13.16. Define the list

// Define list for storing items of SInt32
SFXArray<SInt32> list;

Example 13.17. Append items

list.InsertLast(3);
list.InsertLast(-15);
list.InsertLast(0);
list.InsertLast(1003);

// list = ( 3 , -15 , 0 , 1003 )

Example 13.18. Set and get items

// SInt32 n = list[1]; invalid statement

// list[2] = 6; invalid statement

SInt32 m = list.GetFirst(); // m = 3
SInt32 r = list.GetLast();  // r = 1003

Example 13.19. Get the number of items

SInt32 n = list.GetSize();  // n = 4

Other operations are similar to those of SFXArray class.

13.1.4. Hashmap Class

SFXHashmap is the data structure for operating on the hashmap where its item consist of key and value.

Example 13.20. Define the hashmap

// Define hashmap for storing items of SFXAnsiString and SInt32
// (Exception) SFXAnsiString and SFXWideString can be used in hashmap. 
SFXHashmap<SFXAnsiString, SInt32> hashmap;

Example 13.21. Set items

hashmap.Set("abc", 7);
hashmap.Set("def", 15);

SFXAnsiString str("ghi");

hashmap.Set(str, 31);

Example 13.22. Get items

SInt32 n = hashmap.Get("def"); // n = 15