OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
IncomingIndex.cc
Go to the documentation of this file.
1 /*
2  * opencog/atomspace/IncomingIndex.cc
3  *
4  * Copyright (C) 2008,2013 Linas Vepstas <linasvepstas@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License v3 as
8  * published by the Free Software Foundation and including the exceptions
9  * at http://opencog.org/wiki/Licenses
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program; if not, write to:
18  * Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #include <opencog/util/oc_assert.h>
23 
25 #include <opencog/atomspace/Link.h>
26 
27 using namespace opencog;
28 
30 {
31 }
32 
34 {
35 }
36 
38 {
39  LinkPtr l(LinkCast(a));
40  if (NULL == l) return;
41 
42  Handle hin = a->getHandle();
43  const std::vector<Handle>& oset = l->getOutgoingSet();
44  for (std::vector<Handle>::const_iterator it = oset.begin(); it != oset.end(); ++it)
45  {
46  Handle h = *it;
47 
48  const UnorderedHandleSet& oldset = idx.get(h);
49 
50  // Check to see if h is already listed; it should not be...
51  // Unless a given handle appears twice in the outgoing set
52  // of a link, in which case we should ignore the second
53  // (and other) instances.
54  UnorderedHandleSet::const_iterator oit = oldset.begin();
55  for (; oit != oldset.end(); ++oit)
56  {
57  // OC_ASSERT(*oit != hin, "Same atom seems to be getting inserted twice!");
58  if (*oit == hin) break;
59  }
60  if (oit != oldset.end()) continue;
61 
62  // Add hin to the incoming set of h.
63  UnorderedHandleSet inset = oldset;
64  inset.insert(hin);
65  idx.remove(h);
66  idx.insert(h, inset);
67  }
68 }
69 
71 {
72  LinkPtr l(LinkCast(a));
73  if (NULL == l) return;
74 
75  Handle hin = a->getHandle();
76  const std::vector<Handle>& oset = l->getOutgoingSet();
77  for (std::vector<Handle>::const_iterator it = oset.begin(); it != oset.end(); ++it)
78  {
79  Handle h = *it;
80 
81  const UnorderedHandleSet& oldset = idx.get(h);
82  UnorderedHandleSet inset = oldset;
83 
84  UnorderedHandleSet::iterator oit = inset.begin();
85  for (; oit != inset.end(); ++oit)
86  {
87  if (*oit == hin) break;
88  }
89 
90  // Check to see if h was already deleted; it could happen that
91  // h appears twice, or more, in oset. The first time we see it,
92  // it gets removed from the index. The second and later times,
93  // we do nothing.
94  if (oit != inset.end())
95  {
96  // Remove hin from the incoming set of h.
97  inset.erase(oit);
98  idx.remove(h);
99  idx.insert(h, inset);
100  }
101  }
102 }
103 
105 {
106  return idx.get(h);
107 }
108 
109 // ================================================================
110 
112 {
113  iterator it(h);
114  if (Handle::UNDEFINED != h)
115  it._iset = idx.get(h);
116  it._s = it._iset.begin();
117  it._e = it._iset.end();
118  return it;
119 }
120 
122 {
124  it._s = it._e = it._iset.end();
125  return it;
126 }
127 
128 // Note that his iterator makes a copy ofthe incoming set,
129 // and is thusstable against insertions and deletions from
130 // the incoming set in the parent class.
132 {
133  _h = h;
134 }
135 
137 {
138  _h = v._h;
139  _iset = v._iset;
140  _s = v._s;
141  _e = v._e;
142  return *this;
143 }
144 
146 {
147  if (_s == _e) return Handle::UNDEFINED;
148  return *_s;
149 }
150 
152 {
153  // If v is end(), then _h is undefined ...
154  if (v._h != Handle::UNDEFINED and
155  _h != Handle::UNDEFINED and _h != v._h) return false;
156  return _s == v._s;
157 }
158 
160 {
161  if (v._h != Handle::UNDEFINED and
162  _h != Handle::UNDEFINED and _h != v._h) return true;
163  return _s != v._s;
164 }
165 
167 {
168  return operator++(1);
169 }
170 
172 {
173  while (0 < i and _s != _e) { ++_s; --i; }
174  return *this;
175 }
176 
177 // ================================================================
HandleSetIndex idx
Definition: IncomingIndex.h:52
std::shared_ptr< Atom > AtomPtr
Definition: Handle.h:48
void insert(Handle h, const UnorderedHandleSet &uset)
Handle getHandle()
Definition: Atom.h:211
std::shared_ptr< Link > LinkPtr
Definition: Atom.h:53
iterator begin(Handle) const
iterator end(void) const
static const Handle UNDEFINED
Definition: Handle.h:77
void removeAtom(const AtomPtr &a)
iterator & operator=(iterator)
UnorderedHandleSet::const_iterator _e
Definition: IncomingIndex.h:77
static LinkPtr LinkCast(const Handle &h)
Definition: Link.h:263
const UnorderedHandleSet & getIncomingSet(Handle) const
std::unordered_set< Handle, handle_hash > UnorderedHandleSet
a hash that associates the handle to its unique identificator
Definition: Handle.h:250
const UnorderedHandleSet & get(Handle h) const
void insertAtom(const AtomPtr &a)
UnorderedHandleSet::const_iterator _s
Definition: IncomingIndex.h:76