The solution with main/subroutine architectural style with shared data decomposes the system according to the four basic functiones performed: input, shift, alphabetize and output. These functiones are coordinated as subroutines by a main program, that sequences through them.
The current KWIC system represents its data in the following format:
chars_. Words are delimited by a single space character.
line_index_ is an integer array, which keeps indices of
characters from the character array. Each index kept in the line index
array is the index of the first character of a particular line.
circular_shifts_ represents the set of all circular
shifts of all original lines. A particular circular shift is
represented as a pair of indices. The first value in such a pair is an
index of the original line from the line index array. The second value
in the pair is an index of a character from the character array. The
character pointed by this index is the first character of a particular
circular shift.
alphabetized_ represents the sorted lines. This array has
the same format as the circular shifts array. The difference is that
in the alphabetized array lines come in a different order, i.e., they
are sorted alphabetically.
The main function controls the execution of the KWIC program:
input function is called to read and parse the lines
from an input file. This function reads and processes all lines at once
and represents it by means of the character (chars_) and
line index (line_index_) array, as described before.
main function calls the circularShift
function, which produces circular shifts of each particular line and
stores it in the circular shifts array
(circular_shifts_), as discussed before.
main
function calls the alphabetize function, which sorts
circular shifts alphabetically. The result is stored in the
alphabetized array (alphabetized_), as described before.
main function calls the output
function, which prints the sorted lines.