Commits

Anonymous committed 9b365f4

bug fixes in NavigableIterator and direct access to ElementLink

Comments (0)

Files changed (5)

+2004-02-23  Peter Loch <loch@physics.arizona.edu>
+	* Navigation/NavigableIterator.h: fix internal iterator handling
+	* Navigation/Navigable.h: added	feature: put/retrieve ElementLink 
+	directly
+	* Navigation/NavigationDefaults.h: added feature: put/retrieve 
+	ElementLink directly
+	
 2004-02-17  Peter Loch <loch@physics.arizona.edu>
 	* Navigation/NavigableIterator.h: fix all const/non-const pointers and
 	refs

Navigation/Navigable.h

  *         the container of children
  *
  * @author ATLAS Collaboration
- * $Id: Navigable.h,v 1.3 2004-02-17 16:52:32 lochp Exp $
+ * $Id: Navigable.h,v 1.4 2004-02-23 17:45:48 lochp Exp $
  **/
 
 // package includes for the template
+#include "Navigation/INavigable.h"
 #include "Navigation/NavigableIterator.h"
-#include "Navigation/INavigable.h"
 #include "Navigation/NavigationDefaults.h"
 
 // navigation token management
 #include "Navigation/NavigationToken.h"
 
 // services etc.
-#include "DataModel/DataVector.h"
+// #include "DataModel/DataVector.h"
+#include "DataModel/ElementLink.h"
 #ifndef DATAMODEL_TOOLS_GENERATEINDEXINGPOLICY_H
 # include "DataModel/tools/GenerateIndexingPolicy.h"
 #endif
   //                                                                        //
   // Template Parameters:   Defaults:                                       //
   //                                                                        //
-  // CONT                   --none--         external object collection     //
+  // CONT                   TerminalNode     external object collection     //
   // RPAR                   DefaultWeight    relational parameter type      //
   // COLL                   DefaultChildColl internal object collection     //
   //                                                                        //
   typedef typename CONT::const_reference constituent_const_ptr_ref;
   typedef typename CONT::value_type      constituent_ptr;
 
-  // container index type
+  // object container index type
   typedef typename SG::GenerateIndexingPolicy<CONT>::type::index_type
                                          external_index_type;
 
   // external iterator access
-  typedef NavigableIterator<constituent_type,COLL> object_iter;
+  typedef NavigableIterator<CONT,RPAR,COLL> object_iter;
 
   /////////////////////////////////
   // Constructors and Destructor //
 		  external_index_type& theIndex,
 		  const RPAR&          objectParameter=RPAR());
 
+  // add element to the Navigable: use element link direcly!
+  void putElement(const ElementLink<CONT>* objectLink,
+		  const RPAR&              objectParameter=RPAR());
+
   ///////////////////////
   // Data manipulation //
   ///////////////////////
   // Access to Constituents //
   ////////////////////////////
 
-  // public container access: iterators, sizes etc.
+  // public object access: iterators, sizes etc.
   virtual object_iter& begin() const;    
   virtual object_iter& end()   const;
   virtual unsigned int size()  const;
 
+  // public link access
+  //  virtual link_iter& beginLinks() const;
+  //  virtual link_iter& endLinks()   const;
+
   // public container access: relational parameter retrieval
   RPAR getParameter(const constituent_type* aConstituent) const;
   RPAR getParameter(const CONT& objectContainer,
 		    external_index_type& theIndex) const;
 
+  RPAR getParameter(const ElementLink<CONT>* theLink) const;
+
+  // public container access: retrieve ElementLink for given object pointer
+  virtual const ElementLink<CONT>* getElementLink(const constituent_type*
+						  aConstituent) const;
+
   ///////////////////////////
   // Protected data access //
   ///////////////////////////
 						    iter) const;
   virtual const constituent_type* getConstituentPtr(constituent_const_iter 
 						    iter) const;
+  virtual const 
+  ElementLink<CONT>* getElementPtr(constituent_const_iter iter) const;
+  virtual const 
+  ElementLink<CONT>* getElementPtr(const constituent_type* aChild) const;
 
   // get the parameter associated with the child, if any
   virtual RPAR getConstituentPar(constituent_iter iter)       const;

Navigation/Navigable.icc

 					const RPAR&      
 					refParameter)
 {
+  // replacement insertion policy in 
+  if ( this->contains(ptrObject) ) this->remove(ptrObject);
   NavigationDefaults::DefaultChildColl<CONT, RPAR>::insert(
 						    this->getConstituents(), 
 						    refObjectContainer,
 					refParameter) 
 						  
 {
+  if ( this->contains(refObjectContainer,refIndex) ) 
+    this->remove(refObjectContainer,refIndex);
   NavigationDefaults::DefaultChildColl<CONT, RPAR>::insert(
 						    this->getConstituents(),
 						    refObjectContainer,
 						          );
 }
 
+// insert ElementLink to object
+template <typename CONT, typename RPAR, typename COLL>
+void
+Navigable<CONT, RPAR, COLL>::putElement(const ElementLink<CONT>* ptrObjectLink,
+					const RPAR&              refParameter)
+{
+  // help compiler
+  const constituent_type* ptrObject = *ptrObjectlink;
+  if ( this->contains(ptrObject) ) this->remove(*ptrObject);
+  NavigationDefaults::DefaultChildColl<CONT, RPAR>::insert(
+						    this->getConstituents(),
+						    ptrObjectLink,
+						    refParameter
+							   );
+}
 ////////////////////////////////
 // Object Removal and Finding //
 ////////////////////////////////
 typename Navigable<CONT, RPAR, COLL>::object_iter& 
 Navigable<CONT, RPAR, COLL>::begin() const
 {
-  const constituent_coll* ptrToConstituents = this->getPtrToConstituents();
   return 
-    ( NavigableIterator<typename Navigable<CONT, RPAR, COLL>::constituent_type,
-    COLL>( ptrToConstituents ) ).begin();
+    NavigableIterator<CONT, RPAR, COLL>(this->getConstituents()).begin();
 }
 
 // end() iterator
 typename Navigable<CONT, RPAR, COLL>::object_iter& 
 Navigable<CONT, RPAR, COLL>::end() const
 {
-  const constituent_coll* ptrToConstituents = this->getPtrToConstituents();
   return 
-    ( NavigableIterator<typename Navigable<CONT, RPAR, COLL>::constituent_type,
-    COLL>( ptrToConstituents ) ).end();
+    NavigableIterator<CONT, RPAR, COLL>(this->getConstituents()).end();
 }
 
 // size 
   return this->getConstituents().size();
 }
 
+// return ElementLink for given object pointer
+template <typename CONT, typename RPAR, typename COLL>
+const ElementLink<CONT>* 
+Navigable<CONT, RPAR, COLL>::getElementPtr( const constituent_type* 
+					    aChild ) const
+{
+  return 
+    NavigationDefaults::DefaultChildColl<CONT, RPAR>::getElementPtr( 
+      NavigationDefaults::DefaultChildColl<CONT, RPAR>::find(
+					        this->getConstituents(),
+					                         aChild
+                                                            ));
+}
+
+// return ElementLink for a given object iterator
+template<typename CONT, typename RPAR, typename COLL>
+const ElementLink<CONT>*
+Navigable<CONT, RPAR, COLL>::getElementPtr( constituent_const_iter
+					    anIter) const
+{
+  return 
+    NavigationDefaults::DefaultChildColl<CONT, RPAR>::getElementPtr(anIter);
+}
+
 /////////////////////////
 // Retrieve Parameters //
 /////////////////////////
   return this->getParameter(ptrObject);
 }
 
+// by ElementLink pointer
+template<typename CONT, typename RPAR, typename COLL>
+RPAR
+Navigable<CONT, RPAR, COLL>::getParameter(const ElementLink<CONT>* 
+					  ptrLink) const
+{
+  return 
+    getConstituentPar(
+      NavigationDefaults::DefaultChildColl<CONT, RPAR>::find(getConstituents(),
+							     ptrLink)
+                     );
+}
+
 ///////////////////////////////////////////////////////////////////////////////
 //                         Internally Used Methods                           //
 ///////////////////////////////////////////////////////////////////////////////

Navigation/NavigableIterator.h

 #ifndef NAVIGATION_NAVIGABLEITERATOR_H
 #define NAVIGATION_NAVIGABLEITERATOR_H
 
-template <typename CHILD, typename CHILDCOLL>
+#include "Navigation/NavigationDefaults.h"
+
+template <typename CONT, typename RPAR, typename COLL>
 class NavigableIterator
 {
  public:
   // Constructor and Destructor //
   ////////////////////////////////
 
-  NavigableIterator(const CHILDCOLL* theCollection)
-    : m_collection(theCollection)
-    { }
+  NavigableIterator(const COLL& theCollection)
+    : m_firstIter(theCollection.begin())
+    , m_lastIter(theCollection.end())
+    {
+      m_actual = m_firstIter;
+    }
 
   virtual ~NavigableIterator() { };
 
   // Iterator Methods //
   //////////////////////
 
+  typedef typename CONT::base_value_type  child_type;
+
   NavigableIterator& begin()
     {
-      m_actual = m_collection->begin();
+      m_actual = m_firstIter;
       return *this;
     }
 
   NavigableIterator& end()
     {
-      m_actual = m_collection->end();
+      m_actual = m_lastIter;
       return *this;
     }
 
   NavigableIterator& next()
     {
-      if ( m_actual != m_collection->end() )
+      if ( m_actual != m_lastIter )
 	{
 	  m_actual++;
 	}
 
   NavigableIterator& previous()
     {
-      if ( m_actual != m_collection->begin() )
+      if ( m_actual != m_firstIter )
 	{
 	  m_actual--;
 	}
     }
 
   // operators for advance, setp back and comparisons
-  NavigableIterator& operator++()    { this->next(); }
-  NavigableIterator  operator++(int) { this->next(); }
-  NavigableIterator& operator--()    { this->previous(); }
-  NavigableIterator  operator--(int) { this->previous(); }
-  bool operator==( NavigableIterator<CHILD,CHILDCOLL>& 
+  NavigableIterator& operator++()    { return this->next(); }
+  NavigableIterator  operator++(int) { return this->next(); }
+  NavigableIterator& operator--()    { return this->previous(); }
+  NavigableIterator  operator--(int) { return this->previous(); }
+  bool operator==( NavigableIterator<CONT, RPAR, COLL>& 
 		   anOtherIterator) const
     { return m_actual == anOtherIterator.m_actual; }
-  bool operator!=( NavigableIterator<CHILD,CHILDCOLL>& 
+  bool operator!=( NavigableIterator<CONT, RPAR, COLL>& 
 		   anOtherIterator) const
     { return !( *this == anOtherIterator ); }
 
   // operators for value retrieval
-  const CHILD* operator*() const
+  const child_type* operator*() const
     {
-      return ( m_actual != m_collection->end() )
-	? m_collection->getChildPtr(m_actual)
+      
+      return m_actual != m_lastIter
+	? NavigationDefaults::DefaultChildColl<CONT,RPAR>::getChildPtr(m_actual)
 	: 0;
     }
-  const CHILD* operator->() const
+  const child_type* operator->() const
     {
-      return m_actual != m_collection->end() 
-	? m_collection->getChildPtr(m_actual)
+      return m_actual != m_lastIter 
+	? NavigationDefaults::DefaultChildColl<CONT,RPAR>::getChildPtr(m_actual)
 	: 0;
     }
 
  protected:
 
-  typename CHILDCOLL::const_iterator m_actual;
-  const CHILDCOLL*                   m_collection;
+  typename COLL::const_iterator m_actual;
+  typename COLL::const_iterator m_firstIter;
+  typename COLL::const_iterator m_lastIter;
 
 };  
 #endif

Navigation/NavigationDefaults.h

 
 #include <utility>
 #include <vector>
+#include <algorithm>
 
 namespace NavigationDefaults
 {
 
   struct DefaultType{};   // completely dummy
 
-  //////////////////////
-  // DefaultContainer //
-  //////////////////////
+  ///////////////////////
+  // Default Container //
+  ///////////////////////
 
   struct DefaultContainer // needs to provide a "type"
   {
   typedef DefaultContainer TerminalNode;
 
   ////////////////////////////////
+  // Default Insertion Handling //
+  ////////////////////////////////
+
+  ////////////////////////////////
   // Default Relation Parameter //
   ////////////////////////////////
 
     struct DefaultChildColl
     {
       // default container is a vector of (ElementLink,RPAR) pairs
-      typedef typename std::pair< ElementLink<CONT>, RPAR > value_type;
-      typedef typename std::vector< value_type >                  type;
-      typedef typename type::iterator                    type_iterator;
-      typedef typename type::const_iterator        type_const_iterator;
-      //      typedef typename CONT::const_reference           const_child_ptr;
-      //      typedef typename CONT::reference                       child_ptr;
-      typedef typename CONT::base_value_type*                child_ptr;
-      typedef const typename CONT::base_value_type*    const_child_ptr;
+      typedef typename std::pair< const ElementLink<CONT>*, RPAR > value_type;
+      typedef typename std::vector< value_type >                         type;
+      typedef typename type::iterator                           type_iterator;
+      typedef typename type::const_iterator               type_const_iterator;
+      typedef typename CONT::base_value_type*                       child_ptr;
+      typedef const typename CONT::base_value_type*           const_child_ptr;
       typedef typename SG::GenerateIndexingPolicy<CONT>::type::index_type
-                                                   external_index_type;
+                                                          external_index_type;
     
       // object pointer (key) access (note: outer *() operates on ElementLink!)
       static const_child_ptr getChildPtr(type_const_iterator thisIter)
       { 
-        child_ptr thePointer = *((*thisIter).first);
-	return (const_child_ptr) thePointer; }
+        child_ptr thePointer = *(*((*thisIter).first));
+	return (const_child_ptr) thePointer; 
+      }
 
       // relational parameter (data) access
       static RPAR getChildPar(type_const_iterator thisIter) 
       { return (*thisIter).second; }
 
+      // ElementLink pointer access 
+      static const ElementLink<CONT>* getElementPtr(type_const_iterator 
+						    thisIter)
+      {
+	return (*thisIter).first;
+      }
+
+      ///////////////////////// Insertion /////////////////////////////////////
+
       // insertion of ElementLink by object pointer
-      //      template<typename OBJT>
       static void insert(type& cont, 
 			 const CONT& aChildContainer,
 			 const_child_ptr aChild, 
 			 const RPAR& aPar=RPAR()) 
       {
-	ElementLink<CONT> theLink;
-	theLink.toContainedElement(aChildContainer,
-				   ( child_ptr ) aChild );
+	ElementLink<CONT>* theLink = new ElementLink<CONT>();
+	theLink->toContainedElement(aChildContainer,
+				    ( child_ptr ) aChild );
 	value_type theEntry(theLink,aPar);
 	cont.push_back(theEntry); 
       }
 			 external_index_type& anIndex,
 			 const RPAR& aPar=RPAR())
       {
-	ElementLink<CONT> theLink;
-	theLink.toIndexedElement(aChildContainer,anIndex);
+	ElementLink<CONT>* theLink = new ElementLink<CONT>();
+	theLink->toIndexedElement(aChildContainer,anIndex);
 	value_type theEntry(theLink,aPar);
 	cont.push_back(theEntry);
       }
 
-      // find method implementation for const_iterator
+      // insertion of ElementLink by ElementLink
+      static void insert(type& cont, 
+			 const ElementLink<CONT>* aLink,
+			 const RPAR& aPar=RPAR())
+      {
+	value_type theEntry(aLink,aPar);
+	cont.push_back(theEntry);
+      }
+
+      //////////////////////////// allocation/find ////////////////////////////
+
+      // find method implementation for const_iterator on objects
       static type_const_iterator 
       find(const type& cont, const_child_ptr aChild)
       {
 	type_const_iterator lastEntry  = cont.end();
 	for ( ; firstEntry != lastEntry ; firstEntry++ )
 	  {
-	    const_child_ptr ptrChild = *((*firstEntry).first);
+	    const_child_ptr ptrChild = *(*((*firstEntry).first));
 	    if ( ptrChild == aChild ) break;
 	  }
 	return firstEntry;
       }
       
-      // find method for iterator
+      // find method for iterator on objects
       static type_iterator
       find(type& cont, const_child_ptr aChild)
       {
 	type_iterator lastEntry  = cont.end();
 	for ( ; firstEntry != lastEntry; firstEntry++ )
 	  {
-	   const_child_ptr ptrChild = *((*firstEntry).first);
+	   const_child_ptr ptrChild = *(*((*firstEntry).first));
 	    if ( ptrChild == aChild ) break;
 	  }
 	return firstEntry;
       }  
 
-      // removal 
+      // find method for ElementLinks
+      static type_const_iterator
+      find(const type& cont, const ElementLink<CONT>* anElement)
+      {
+	const_child_ptr theChild = *(*anElement);
+	return find(cont,theChild);
+      }
+
+      // find method for ElementLinks
+      static type_iterator
+      find(type& cont, const ElementLink<CONT>* anElement)
+      {
+	child_ptr theChild = *(*anElement);
+	return find(cont,theChild);
+      }
+
+      /////////////////////////// container management ////////////////////////
+
+      // removal by object pointer
       static bool
       remove(type& cont, const_child_ptr aChild)
       {
 	type_iterator found = find(cont,aChild);
 	if ( found != cont.end() )
 	  {
+	    // we assume the container owns the ElementLink/parameter pair, 
+            // but not the object linked by ElementLink --- FIXME ??
+	    delete (*found).first;
+	    cont.erase(found);
+	    return true;
+	  }
+	else
+	  {
+	    return false;
+	  }
+      }
+
+      // removal by ElementLink
+      static bool
+      remove(type& cont, const ElementLink<CONT>* theElement)
+      {
+	type_iterator found = find(cont,theElement);
+	if ( found != cont.end() )
+	  {
+	    delete (*found).first;
 	    cont.erase(found);
 	    return true;
 	  }
     struct DefaultChildColl<CONT, DefaultWeight> 
     {
       // default container is a vector of ElementLinks
-      typedef ElementLink<CONT>                      value_type;
+      typedef const ElementLink<CONT>*               value_type;
       typedef typename std::vector< value_type >           type;
       typedef typename type::iterator             type_iterator;
       typedef typename type::const_iterator type_const_iterator;
 
       // object pointer access
       static const_child_ptr  getChildPtr(type_const_iterator iter) 
-      { return *(*iter); }
+      { return *(*(*iter)); }
 
       // default weight is dummy
       static DefaultWeight getChildPar(type_const_iterator /*iter*/) 
       { return DefaultWeight(); }
 
+      // ElementLink pointer access 
+      static const ElementLink<CONT>* getElementPtr(type_const_iterator 
+						    thisIter)
+      {
+	return *thisIter;
+      }
+
       // insertion of ElementLink by object pointer 
       static void insert(type& cont, const CONT& aChildContainer,
 			 const_child_ptr aChild, 
 			 const DefaultWeight&) 
       {
-	ElementLink<CONT> theLink;
+	ElementLink<CONT>* theLink = new ElementLink<CONT>();
 	// ugly fix!
-	theLink.toContainedElement(aChildContainer,
-				   ( child_ptr ) aPtr);
+	theLink->toContainedElement(aChildContainer,
+				    ( child_ptr ) aPtr);
 	cont.push_back(theLink); 
       }
 
 			 const external_index_type& anIndex,
 			 const DefaultWeight&)
       {
-	ElementLink<CONT> theLink;
-	theLink.toIndexedElement(aChildContainer,aChild);
+	ElementLink<CONT>* theLink = new ElementLink<CONT>();
+	theLink->toIndexedElement(aChildContainer,aChild);
 	cont.push_back(theLink);
       }
+
+      // direct insertion of ElementLink
+      static void insert(type& cont, const ElementLink<CONT>* aLink,
+			 const DefaultWeight&)
+      {
+	cont.push_back(alink);
+      }
     
       // allocation of object pointer in list
       static type_const_iterator find(const type& cont, 
 	return firstEntry;
       }
 
+      // find ElementLink directly
+      static type_const_iterator find(const type& cont, 
+				      const ElementLink<CONT>* anElement)
+      {
+	return std::find(anElement,cont.begin(),cont.end());
+      }
+
+      // find ElementLink directly
+      static type_iterator find(type& cont,
+				const ElementLink<CONT>* anElement)
+      {
+	return std::find(anElement,cont.begin(),cont.end());
+      }
+
       // removal 
       static bool remove(type& cont, const_child_ptr aChild)
       {
 	type_iterator found = find(cont,aChild);
 	if ( found != cont.end() )
 	  {
+	    // ownership of ElementLink!! FIXME
+	    delete *found;
 	    cont.erase(found);
 	    return true;
 	  }
 	  }
       }
 
+      // removal of ElementLink
+      static bool remove(type& cont, const ElementLink<CONT>* anElement)
+      {
+	type_iterator found = find(cont,anElement);
+	if ( found != cont.end() )
+	  {
+	    delete *found;
+	    cont.erase(anElement);
+	    return true;
+	  }
+	else
+	  {
+	    return false;
+	  }
+      }
+
       // test on containment
       static bool
       contains(type& cont, const_child_ptr aChild)