From ca48c2abe2e2804a63f70e96a0dda99070dfe99b Mon Sep 17 00:00:00 2001
From: marco <marco@5a81b35b-ba03-0410-adc8-b2c5c5119f08>
Date: Fri, 14 May 2010 08:27:28 +0000
Subject: [PATCH] atom views hashcode is no longer based on AtomImpl's address

I've carefully audited all callers of AtomView::GetHashCode()
and changed them to AtomView::GetHandle().GetHashCode() where
required.

git-svn-id: https://dng.biozentrum.unibas.ch/svn/openstructure/trunk@2259 5a81b35b-ba03-0410-adc8-b2c5c5119f08
---
 modules/gfx/src/impl/entity_detail.cc      |  4 ++--
 modules/mol/base/pymod/export_atom.cc      |  4 ++--
 modules/mol/base/pymod/export_atom_view.cc |  3 +++
 modules/mol/base/src/atom_base.cc          |  6 ------
 modules/mol/base/src/atom_base.hh          |  5 -----
 modules/mol/base/src/atom_handle.cc        |  6 ++++++
 modules/mol/base/src/atom_handle.hh        |  7 +++++++
 modules/mol/base/src/atom_view.cc          | 10 ++++++++--
 modules/mol/base/src/atom_view.hh          |  5 +++++
 modules/mol/base/src/coord_source.cc       |  2 +-
 modules/mol/base/tests/test_view.cc        | 11 +++++++++++
 modules/seq/base/pymod/export_sequence.cc  |  1 -
 12 files changed, 45 insertions(+), 19 deletions(-)

diff --git a/modules/gfx/src/impl/entity_detail.cc b/modules/gfx/src/impl/entity_detail.cc
index 1225df457..572de5bb7 100644
--- a/modules/gfx/src/impl/entity_detail.cc
+++ b/modules/gfx/src/impl/entity_detail.cc
@@ -43,9 +43,9 @@ void GfxView::AddAtom(const AtomView& av)
   AtomEntry ae(a,default_radius,
                a.GetRadius(),
                GfxObj::Ele2Color(a.GetElement()));
-  atom_map[a.GetHashCode()]=ae;
+  atom_map[a.GetHandle().GetHashCode()]=ae;
   if(av.GetBondCount()==0) {
-    orphan_atom_list.push_back(a.GetHashCode());
+    orphan_atom_list.push_back(a.GetHandle().GetHashCode());
   }
 }
 
diff --git a/modules/mol/base/pymod/export_atom.cc b/modules/mol/base/pymod/export_atom.cc
index 83f2adc06..c2133beb9 100644
--- a/modules/mol/base/pymod/export_atom.cc
+++ b/modules/mol/base/pymod/export_atom.cc
@@ -42,8 +42,6 @@ void export_Atom()
     .add_property("qualified_name", &AtomBase::GetQualifiedName)
     .def("IsValid", &AtomBase::IsValid)
     .def(self_ns::str(self))
-    .add_property("hash_code", &AtomBase::GetHashCode)
-    .def("GetHashCode", &AtomBase::GetHashCode)
     .def("GetAtomProps", &AtomBase::GetAtomProps,
          return_value_policy<copy_const_reference>())
     .def("SetAtomProps", &AtomBase::SetAtomProps, args("prop"))
@@ -86,10 +84,12 @@ void export_Atom()
     .add_property("handle", &AtomHandle::GetHandle)
     .add_property("entity", &AtomHandle::GetEntity)
     .def("GetBondPartners", &AtomHandle::GetBondPartners)
+    .def("GetHashCode", &AtomHandle::GetHashCode)    
     .def("FindBondToAtom", &AtomHandle::FindBondToAtom, args("other_atom"))
     .def(self==self)
     .def(self!=self)
     .def("__hash__", &AtomHandle::GetHashCode)
+    .add_property("hash_code", &AtomHandle::GetHashCode)
   ;
 
   class_<AtomHandleList>("AtomHandleList", no_init)
diff --git a/modules/mol/base/pymod/export_atom_view.cc b/modules/mol/base/pymod/export_atom_view.cc
index af2dd063b..036a87ae0 100644
--- a/modules/mol/base/pymod/export_atom_view.cc
+++ b/modules/mol/base/pymod/export_atom_view.cc
@@ -43,6 +43,9 @@ void export_AtomView()
     .def("GetHandle", &AtomView::GetHandle)
     .def("GetBondCount", &AtomView::GetBondCount)
     .def("GetBondList", &AtomView::GetBondList)
+    .def("GetHashCode", &AtomView::GetHashCode)
+    .def("__hash__", &AtomView::GetHashCode)
+    .add_property("hash_code", &AtomView::GetHashCode)
     .def("GetBondPartners", &AtomView::GetBondPartners)
   ;
   class_<AtomViewList>("AtomViewList", init<>())
diff --git a/modules/mol/base/src/atom_base.cc b/modules/mol/base/src/atom_base.cc
index 8eaff3164..2818844b0 100644
--- a/modules/mol/base/src/atom_base.cc
+++ b/modules/mol/base/src/atom_base.cc
@@ -120,12 +120,6 @@ void AtomBase::CheckValidity() const
     throw InvalidHandle();
 }
 
-long AtomBase::GetHashCode() const 
-{
-  this->CheckValidity();  
-  return reinterpret_cast<long>(Impl().get());
-}
-
 std::ostream& operator<<(std::ostream& os, const AtomBase& atom) 
 {
   if (atom.IsValid()) {
diff --git a/modules/mol/base/src/atom_base.hh b/modules/mol/base/src/atom_base.hh
index 142f1a9db..9777aea71 100644
--- a/modules/mol/base/src/atom_base.hh
+++ b/modules/mol/base/src/atom_base.hh
@@ -153,11 +153,6 @@ public:
   /// \brief get atom implementation
   impl::AtomImplPtr& Impl();
 
-  /// \brief Get unique identifier for atom
-  /// 
-  /// Get hash code that uniquely identifies every atom. The hash code is
-  /// identical for all atom views pointing to a given atom.
-  long GetHashCode() const;  
 protected:
   
   GenericPropContainerImpl* GpImpl();
diff --git a/modules/mol/base/src/atom_handle.cc b/modules/mol/base/src/atom_handle.cc
index 486441683..61c975724 100644
--- a/modules/mol/base/src/atom_handle.cc
+++ b/modules/mol/base/src/atom_handle.cc
@@ -116,6 +116,12 @@ AtomHandle AtomHandle::GetHandle() const
   return *this;
 }
 
+long AtomHandle::GetHashCode() const 
+{
+  this->CheckValidity();  
+  return reinterpret_cast<long>(Impl().get());
+}
+
 }} // ns
 
 
diff --git a/modules/mol/base/src/atom_handle.hh b/modules/mol/base/src/atom_handle.hh
index 7d817f8e1..6ea9094c0 100644
--- a/modules/mol/base/src/atom_handle.hh
+++ b/modules/mol/base/src/atom_handle.hh
@@ -81,6 +81,13 @@ public:
   /// 
   /// Useful for duck-typing in Python and templated code.
   AtomHandle GetHandle() const;
+  
+  /// \brief Get unique identifier for atom
+  /// 
+  /// Get hash code that uniquely identifies every atom. The hash code is
+  /// identical for all atom views pointing to a given atom.
+  long GetHashCode() const;
+  
   bool operator==(const AtomHandle& ref) const;
   bool operator!=(const AtomHandle& ref) const;
 
diff --git a/modules/mol/base/src/atom_view.cc b/modules/mol/base/src/atom_view.cc
index 25518778e..5811a2d55 100644
--- a/modules/mol/base/src/atom_view.cc
+++ b/modules/mol/base/src/atom_view.cc
@@ -115,7 +115,7 @@ mol::AtomViewList AtomView::GetBondPartners() const
   mol::AtomViewList avl;
   mol::BondHandleList::const_iterator i;
   for (i=data_->bonds.begin();i!=data_->bonds.end();++i) {
-    if (i->GetFirst()!=*this) {
+    if (i->GetFirst().GetHandle()!=this->GetHandle()) {
       avl.push_back(this->GetEntity().FindAtom(i->GetFirst()));
     } else {
       avl.push_back(this->GetEntity().FindAtom(i->GetSecond()));
@@ -156,7 +156,13 @@ void AtomView::RemoveBondInternal(const BondHandle& bond)
     data_->bonds.erase(i);    
   }
 }
-  
+
+long AtomView::GetHashCode() const 
+{
+  this->CheckValidity();  
+  return reinterpret_cast<long>(data_.get());
+} 
+
 }} // ns
 
 
diff --git a/modules/mol/base/src/atom_view.hh b/modules/mol/base/src/atom_view.hh
index ad65accc6..a9e2d9937 100644
--- a/modules/mol/base/src/atom_view.hh
+++ b/modules/mol/base/src/atom_view.hh
@@ -69,6 +69,11 @@ public:
   void Apply(EntityVisitor& visitor);
   void Apply(EntityViewVisitor& visitor);
 
+  /// \brief get unique id
+  /// 
+  /// The unique id is the same for all AtomViews pointing to the same atom 
+  /// view data. 
+  long GetHashCode() const;
   bool operator==(const AtomView& rhs) const;
   bool operator!=(const AtomView& rhs) const;
 protected:
diff --git a/modules/mol/base/src/coord_source.cc b/modules/mol/base/src/coord_source.cc
index 9b9f853e3..0afa3597d 100644
--- a/modules/mol/base/src/coord_source.cc
+++ b/modules/mol/base/src/coord_source.cc
@@ -95,7 +95,7 @@ void CoordSource::CaptureInto(int pos)
        e=atoms_.end(); i!=e; ++i) {
     coords.push_back(i->GetPos());
   }
-  if(pos<0 || pos>=GetFrameCount()) {
+  if(pos<0 || pos>=static_cast<int>(this->GetFrameCount())) {
     this->AddFrame(coords);
   } else {
     this->InsertFrame(pos,coords);
diff --git a/modules/mol/base/tests/test_view.cc b/modules/mol/base/tests/test_view.cc
index 33bda815d..26399e10e 100644
--- a/modules/mol/base/tests/test_view.cc
+++ b/modules/mol/base/tests/test_view.cc
@@ -77,6 +77,17 @@ BOOST_AUTO_TEST_CASE(gen_full_view)
   BOOST_CHECK_EQUAL(avl[1].GetBondCount(),2);
   BOOST_CHECK_EQUAL(avl[2].GetBondCount(),2);
   BOOST_CHECK_EQUAL(avl[3].GetBondCount(),1);
+  
+  mol::AtomView av1=avl[0];
+  mol::AtomView av2=avl[1];
+  
+  BOOST_CHECK(av1!=av2);
+  BOOST_CHECK(av1.GetHashCode()!=av2.GetHashCode());
+  BOOST_CHECK(av1.GetHandle().GetHashCode()!=av1.GetHashCode());
+  
+  mol::AtomView av3=av1;
+  BOOST_CHECK(av1==av3);
+  BOOST_CHECK_EQUAL(av1.GetHashCode(), av3.GetHashCode());  
 }
 
 BOOST_AUTO_TEST_SUITE_END()
diff --git a/modules/seq/base/pymod/export_sequence.cc b/modules/seq/base/pymod/export_sequence.cc
index ce0e19864..0eb5ce9f2 100644
--- a/modules/seq/base/pymod/export_sequence.cc
+++ b/modules/seq/base/pymod/export_sequence.cc
@@ -38,7 +38,6 @@
 using namespace ost;
 using namespace ost::seq;
 using namespace boost::python;
-
 namespace {
 
 void (SequenceHandle::*attach_one)(const mol::EntityView&)=&SequenceHandle::AttachView;
-- 
GitLab