#line 1 "/user/cvsspst/ees1cg/RAVL/RAVL-0.7/Core/Base/Pair.hh" // This file is part of RAVL, Recognition And Vision Library // Copyright (C) 2001, University of Surrey // This code may be redistributed under the terms of the GNU Lesser // General Public License (LGPL). See the lgpl.licence file for details or // see http://www.gnu.org/copyleft/lesser.html // file-header-ends-here #ifndef RAVL_PAIR_HEADER #define RAVL_PAIR_HEADER 1 /////////////////////////////////////////////////////////////////////// //! file="Ravl/Core/Base/Pair.hh" //! author="Radek Marik" //! docentry="Ravl.Core.Tuples" //! date="27/02/1997" //! rcsid="$Id: Pair.hh,v 1.3 2002/07/07 21:38:57 craftit Exp $" //! lib=RavlCore //! userlevel=Default #include "Ravl/Types.hh" #include "Ravl/Index.hh" #include "Ravl/Assert.hh" namespace RavlN { //! userlevel=Normal //: Pair of homogenious objects // The class PairC represents a pair of objects of the same type. // It has basic features of arrays. The object class must provide // the default constructor, the copy constructor, the assigment operator, // and the destructor. template class PairC { public: // Constructors, assigment, and destructor // --------------------------------------- inline PairC() {} // The default constructor. inline PairC(const PairC & p); // The copy constructor. inline PairC(const DataC & o1, const DataC & o2); // Creates the pair of the objects 'o1' and 'o2'. inline const PairC & operator=(const PairC & p); // The assigment. // Access to the objects // --------------------- inline const DataC & operator[](IndexC i) const; // Returns the i-th object of the pair. inline DataC & operator[](IndexC i); // Access to the i-th object of the pair. inline const DataC & A() const { return item[0]; } // Returns the first object of the pair. inline DataC & A() { return item[0]; } // Access to the first object of the pair. inline const DataC & B() const { return item[1]; } // Returns the first object of the pair. inline DataC & B() { return item[1]; } // Access to the first object of the pair. inline IndexC Size() const { return 2; } // Returns 2 as the number of elements of the pair. inline bool IsValid() const { return true; } // Returns always true confirming that the pair exists. inline bool Contains(const IndexC i) const { return (i>=0) && (i <= 1); } // Returns true if the pair contains an item with the index 'i'. // Special operations // ------------------ inline PairC & Swap(); // Exchanges the order of items in the pair. inline PairC & Reverse(); // Exchanges the order of items in the pair. private: // Object representation // --------------------- DataC item[2]; // The pair of items. }; template ostream & operator<<(ostream & s, const PairC & p); // Prints the pair 'p' into the stream 's'. template istream & operator>>(istream & s, PairC & p); // Assigns the values into the pair 'p'. template inline PairC::PairC(const PairC & p) { item[0] = p.item[0]; item[1] = p.item[1]; } template inline PairC::PairC(const DataC & o1, const DataC & o2) { item[0] = o1; item[1] = o2; } template inline const PairC & PairC::operator=(const PairC & p) { item[0] = p.item[0]; item[1] = p.item[1]; return *this; } template inline const DataC & PairC::operator[](IndexC i) const { RavlAssert(Contains(i)); return item[i.V()]; } template inline DataC & PairC::operator[](IndexC i) { RavlAssert(Contains(i)); return item[i.V()]; } template inline PairC & PairC::Swap() { DataC tmp = item[0]; item[0] = item[1]; item[1] = tmp; return *this; } template ostream & operator<<(ostream & s, const PairC & p) { s << p.A() << ' ' << p.B(); return s; } template istream & operator>>(istream & s, PairC & p) { s >> p.A() >> p.B(); return s; } } #endif