PrevNextUpHome SophiaFramework UNIVERSE 5.3

13.3. Stack

SFXStack is data structure for operating a multiple of the elements as a stack. In this class, the last added element is removed first.

Since SFXStack inherits from the base class of SFXArray, this class is handled as an array internally. The last element of the internal array is the top element, and the size of the internal array is that of the stack.

In the default settings, when the first element is inserted, the internal buffer will be allocated by the size set with the SFXStack::SetThreshold function(default: size for 4 elements). Then, in case memory becomes insufficient while elements are inserted one by one, the internal buffer will be expanded by the cluster size set with the SFXStack::SetCluster function(default: size for 8 elements). Here, the size of one element is 4 byte.

When the internal buffer is expanded, new consecutive memory area bigger than the current one by the cluster size will be allocated and all data of existing elements will be copied there. Therefore, if expansion is performed frequently, it may become the cause of performance deterioration

If the number of the elements are known in advance, it is recommended to set the internal buffer size with the SFXStack::SetThreshold / SFXStack::SetCluster functions.

[Caution] Caution
If the SFXStack::SetSize function is called, the size of the stack will be set to the value specified in the argument of this function. The elements added by this function will be initialized with 0.(The value of the exisiting elemet is same as before).
[Note] Note
Though SFXStack can be handled as an array, it is recommended to use only the stack oriented functions as follows:

The code below is to define the stack with data of the SInt32 type as an elements.

// define the stack with data of the SInt32 type as elements
SFXStack<SInt32> stack;

// current status: stack = ()

To push an element onto the stack, call the SFXStack::Push function.

// current status: stack = ()

//  push 3 onto the stack
stack.Push(3);
// current status: stack = (3)

//  push -15 onto the stack
stack.Push(-15);
// current status: stack = (3, -15)

//  push 22 onto the stack
stack.Push(22);
// current status: stack = (3, -15,  22)

To peek the top element of the stack, call the SFXStack::Peek function. After this function is executed, the status of the stack will be same as before.

// current status: stack = (3, -15,  22)

// peek the top element of the stack
SInt32 r = stack.Peek(); // r = 22

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

To get the size (number of the elements) of the stack, call the SFXStack::GetSize function.

// get the size (number of the elements) of the stack
SInt32 n = stack.GetSize(); // n = 3

To pop the element from the stack, call the SFXStack::Pop function. Elements will be popped in the reversed order of pushing them.

// current status: stack = (3, -15,  22)

// pop the top element from the stack
SInt32 i = stack.Pop(); // i =  22
// current status: stack = (3, -15)

// pop the top element from the stack
SInt32 j = stack.Pop(); // j = -15
// current status: stack = (3)

// pop the top element from the stack
SInt32 k = stack.Pop(); // k =   3
// current status: stack = ()
[Caution] Caution
If the empty stack is popped, no error will occur. In this case, the SFXStack::Pop function will return 0 or null depending on the type of the element.