Modeling Services > Relations > Set-relations > Declaring a Set-relation
 
Declaring a Set-relation
Each flight that an airline company owns is identified by a unique alphanumeric string—FOO040, for example. Such a relation could very well be defined using a list-relation of type IlsOwnsList<Airline,Flight> in which access to flights would be sequential. For details about list-relations, see Declaring Simple List-relations. Suppose that you would like to know the departure time of one particular flight. You would first have to read the complete list sequentially until you find the identifier FOO040 and then consult the departure time. In this case, list-relations might be very time-consuming in terms of coding, and also response time would be very slow. To overcome these problems, the solution is to use set-relations which offer a direct access to objects by means of a key. Any type of key is allowed. A key can be a character string, an integer, or even an object.
Let us suppose that the class Flight is defined as follows:
class Flight:
public IlsObject
{
  public:
    Flight(IlsIdentifier identifier):
    _identifier(identifier)
      {}
    IlsIdentifier getIdentifier() const {
      return _identifier;
    }
  private:
    IlsIdentifier _identifier;
};
You can establish a relation of type set-relation between the class Airline and the class Flight like this:
class Airline:
public IlsObject
{
  public:
    Airline(): _flights(*this){}
    class KeyManager
    {
      public:
        static IlsIdentifier GetKey(Flight& flight){
          return flight.getIdentifier();
        }
        static IlsBoolean Match(IlsIdentifier id1,
                                IlsIdentifier id2){
          return !strcmp(id1,id2);
        }
static long Hash(IlsIdentifier key, int size);
    };
    IlsOwnsSet<Airline,Flight,IlsIdentifier,KeyManager> _flights;
};
The classes Airline and Flight are respectively the origin and the target of the relation. IlsIdentifier is the key used to differentiate between flights. It could have been any other type.

Version 6.1
Copyright © 2016, Rogue Wave Software, Inc. All Rights Reserved.