Monday, October 29, 2012

Qt Transition – Stacks

The next class to replace is the Stack class.  The Qt equivalent is the QStack class.  Internally the Stack class is implemented with an array that grows as needed.  The QStack class is based on the QVector class, where its members are in contiguous memory (essentially also an array).

The Stack class has two functions not present in QStack (or the underlying QVector).  The first is a push function that takes no arguments, which is used to add an element to the stack without copying a value to the new element (which was shortly followed by setting this new element using the top function).  The second is a null pop function that removes the top element from the stack without actually returning its value (like the regular pop function does).

The QStack (nor the QVector base) class have similar functions, but can be simulated using the resize and size functions of the QVector class.  For example, this line would be used to add an element to the top of the stack:
stack.resize(stack.size() + 1);
Similarly, the top element of the stack can be removed by using subtraction in the line above.

The four stacks using in the Translator were replaced with QStack using the substitutes for the absent functions.  The stack.h header files was removed (along with the program that tested stacks).  The next change in the Qt transition will be more radical, so this would be a good place to add the v0.2-1 development tag (and archives will be available).

[commit 5d7c634713]

No comments:

Post a Comment

All comments and feedback welcomed, whether positive or negative.
(Anonymous comments are allowed, but comments with URL links or unrelated comments will be removed.)