The USet (Unordered Set) Interface.

  1. A class supports the USet interface if it provides these functions. See Chapter 1.2.3 for the requirements on these functions.
  2. Suppose template<T> class USetImplementation is a class implementing USet. Then the following could be done.
    // establish empty USet H and designate -1 as "null".
    USetImplementation<int> H(-1); 
    H.size(); // returns 0.
    
    H.add(2); // returns true.
    H.add(5); // returns true.
    H.size(); // returns 2.
    
    H.add(2); // returns false. 2 is already in the USet.
    H.size(); // returns 2.
    
    H.remove(3); // returns null
    H.size(); // returns 2.
    
    H.find(2); // returns 2
    H.find(3); // returns null
    H.size(); // returns 2.
    
    H.remove(2); // returns 2
    H.size(); // returns 1.
    
  3. But what good is this, really? Does find really return to the caller what the caller already has? ...well, there are two uses of a USet: (1) as an oracle to answer whether a known item has the property the set represents, and (2) as a map (an association). In the map case we use the definition of == on type T to capture a useful notion of ``equivalent".
    class record 
    { public: 
        string name; 
        int phone;
        record(string n, int p): name(n), phone(p) {}
    };
    
    ///// support for USet implementations on T = record /////
    
    
    // Tell USet the meaning of 'equal' for records.
    bool operator==(record a, record b) { return a.name == b.name; }
    //////////////////////////////////////////////////////////
    
    // usage examples //
    
    record dummy("null", 0);
    // establish USet H, designate dummy as "null".
    USetImplementation<record> H(dummy); 
    
    H.size(); // returns 0.
    record me("saunders", 8316238);
    record pres("obama", 1234567);
    record vp("biden", 7654321);
    
    H.add(me); // returns true.
    H.add(pres); // returns true.
    H.size(); // returns 2.
    
    record me2("saunders", 0);
    H.add(me2); // returns false. 
                // record with name "saunders" is already in.
    H.size(); // returns 2.
    
    H.remove(vp); // returns dummy record
    H.size(); // returns 2.
    
    record rec;
    rec = H.find(me2); // returns me.  
    				   // IMPORTANT: phone # is now available in rec.
    H.find(vp); // returns dummy
    H.size(); // returns 2.
    
    H.remove(me2); // returns me.  
                   // Note that caller did not know me fully.
    H.size(); // returns 1.
    
  4. Bottom line, regardless of record type: if operator== is defined in terms of part 1 of the records, then find and remove are returning information about part 2 of the records to the caller. Also USet is insuring there is only one record stored per possible part 1 data.