Saturday, September 27, 2014

Error List – Use Standard Vector

Like other classes, the Error List class was publicly derived from the QList class.  It was changed to contain the list as a private std::vector member.  Several access functions were added for access to the list (since the list is no longer public) including the bracket operator (constant and non-constant), constant at, clear and count.  The at and count names were used so that callers didn't need to be changed.

A binary search was previously used to find an error by a line number or the closest error not greater than the line number.  The std::lower_bound() function does a similar operator except returns an iterator to an element instead of an index.  There are two forms of this function, one that assumes the elements being searched have the less than operator defined, and the other where a function object is passed defining the comparison.

The Error Item class does not have a less than operator defined so the second form of std::lower_bound() was used.  A C++11 lambda function was defined for comparing the line numbers of error items.  This lambda function definition and the call to std::lower_bound() was put into a new private find iterator function, which returns the resulting iterator.  Since only line numbers are used for searching, a new constructor was added to the Error Item class for initializing just the line number member to pass to std::lower_bound().

The find function returns the index of the error item found for a line number or the closest error to the line number.  This index is used an an insert point for a new error and is also passed to the edit box instance for maintaining the extra selections list (used for highlighting the errors).  This function was modified to call the new find iterator function, which converts the iterator to an index by subtracting the begin iterator or the vector.

In several places in the code, the error index is compared to the size of the error list.  Unlike with the Qt classes, the size of STL classes is returned a size_t type, which is an unsigned integer.  Throughout the code, the type of the error index variable was changed from an integer to either size_t or where possible auto.

The find index function is used to only return an index of an error item for a line number, or a value indicating the line does not have an error (a -1 value was used).  This function was also modified to call the new function, but a -1 value could not be used since the index returned is unsigned.  Instead, an index one past the end (in other words, the size of the vector) is returned to indicate a line number without an error.  The callers of this function was modified accordingly.

The std::vector class uses iterators instead of indexes to insert and remove items from the vector.  Similar to converting an iterator to an index, an index is converted to an iterator by adding it to the begin iterator of the vector.  The insert and remove at access functions were modified accordingly.

[branch stl commit 22e8013fb4]

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.)