Modeling Services > Relations > Inverting Relations > Sorted Inverted List-relations
 
Sorted Inverted List-relations
In the same way as you can maintain a list of the origins of a list-relation, you can maintain a list of the origins of a sorted list-relation using the class template IlsSortedInvertedRelationList. Before reading further, we suggest that you turn to Sorted List-relations and Inverting Multiple Relations if necessary, since the principles behind sorted inverted list-relations is a development of both sorted list-relations and inverted list-relations.
The class template IlsSortedInvertedRelationList derives from IlsInvertedRelationList and like the class templates IlsSortedOwnsList and IlsSortedUsesList takes the following arguments:
*The target type of the relation to be inverted. This class must directly or transitively from IlsObject or IlsEntity. Also, derivation must be public.
*The origin-type. This class must directly or transitively derive from IlsObject or IlsEntity. Also, derivation must be public.
*A Comparator class that defines a sort criterion.
Let us go back to our Airport example. You have seen in section Inverting Multiple Relations, that it is possible to maintain the list of the routes arriving at, or departing from, a given airport using the class template IlsInvertedRelationList:
class Route;
 
class Airport:
typedef IlsSmartPointer<Airport> AirportP;
 
class Airport: public IlsObject
{
  public:
    Airport(): _routes(*this) {}
  private:
    IlsInvertedRelationList<Airport,Route> _routes;
  public:
    void setContext(Route& route,                     void*,IlsRelationId){_routes<<&route;}
    void unsetContext(Route& route,
                      void*, IlsRelationId){_routes>>&route;}
};
 
class Route: public IlsObject
{
  public:
    Route(AirportP departure, AirportP arrival):
    _departure(*this,departure),
    _arrival(*this,arrival)
      {}
  private:
    IlsUses<Route,Airport> _departure, _arrival;
};
Let us now suppose that you want to associate an alphanumeric identifier with the class Route:
class Route: public IlsObject
{
  public:
   Route(IlsIdentifier identifier,
         AirportP departure,
         AirportP arrival):
   _identifier(identifier),
   _departure(*this,departure),
   _arrival(*this,arrival)
     {}
   IlsIdentifier getIdentifier() const {return _identifier;}
private:
   IlsIdentifier _identifier;
   IlsUses<Route,Airport> _departure, _arrival;
};
Let us now suppose that you want to sort the list of the routes departing from and arriving at a given airport in alphabetical order. To do so, you can declare the inverted relation _routes like this:
class Route;
 
class Airport:
typedef IlsSmartPointer<Airport> AirportP;
 
class Airport: public IlsObject
{
  public:
    Airport(): _routes(*this) {}
  private:
    class Comparator{
      public:
        static IlsBoolean Compare(Route&, Route&);
    };
    IlsSortedInvertedRelationList<Airport,Route,Comparator> _routes;
  public:
   void setContext(Route& route,
                   void*,
                   IlsRelationId){_routes<<&route;}
   void unsetContext(Route& route,
                     void*,
                     IlsRelationId){_routes>>&route;}
};
 
class Route: public IlsObject
{
  public:
    Route(IlsIdentifier identifier,
          AirportP departure,
          AirportP arrival):
          _identifier(identifier),
          _departure(*this,departure),
          _arrival(*this,arrival)
            {}
    IlsIdentifier getIdentifier() const {return _identifier;}
  private:
    IlsIdentifier _identifier;
    IlsUses<Route,Airport> _departure, _arrival;
};
The Comparator class, here, is identical to the one described in section The Comparator Class, except that it compares two objects of type OriginType—here Route. For details, on this class refer to the related section.

Version 6.3
Copyright © 2018, Rogue Wave Software, Inc. All Rights Reserved.