11.6.  Exercise: Generics

[ fromfile: ex-generics.xml id: ex-generics ]

This exercise requires you to come up with data structures that keep track of relationships between symbols. Our relationship is a boolean operator frop on a set S of symbols that has two properties:

  1. Reflexivity – For any symbol s in S,

    s frop s  is always true.

  2. Symmetry – For any symbols s and t in S,

    if s frop t is true, then t frop s is also true. 

frop is similar to the boolean operator is-friends-with between people on a social networking site. is-friends-with is not transitive; you do not automatically become friends with all your friends' friends.[85]

In this problem, you build a data structure for storing the relationships between symbols. Each symbol is an arbitrary string of alphanumerics. This data structure should use one or more QSet, QMap, or QMultiMap.

  1. Write a program that repeatedly asks the user to enter relationships or commands, and keeps track of relationships between all pairs of symbols that it sees. The interface should be pretty simple: The user enters a string that is parsed and processed by the dispatching function, processLine(). In other words, processLine() should expect strings of the following forms.

    1. To add a frop between two strings: string1=string2

    2. To list the friends of string1: string1 (no = symbol in the line)

  2. Add another function, takeback(int n), where n refers to the nth assertion. If the nth assertion added a frop, the function should "undo" that assertion, ensuring that relational integrity is kept. After this, show the updated friend lists of both symbols (string1 and string2) involved.

    Have the processLine() function scan for messages of the form "takeback n" and call the takeback() function in response.



[85] Transitivity is a third property of boolean operators that we are not assuming – for any symbols s, t, and u in S, if s op t and t op u are both true, then s op u is also true.

If a boolean operator op is Reflexive, Symmetric, and Transitive, then it is an Equivalence Relation.