OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
LinkIndex.cc
Go to the documentation of this file.
1 /*
2  * opencog/atomspace/LinkIndex.cc
3  *
4  * Copyright (C) 2008 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/atomspace/Link.h>
25 #include <opencog/atomspace/atom_types.h>
26 
27 //#define DPRINTF printf
28 #define DPRINTF(...)
29 
30 using namespace opencog;
31 
33 {
34  resize();
35 }
36 
38 {
39  idx.resize(classserver().getNumberOfClasses());
40 }
41 
42 size_t LinkIndex::size() const
43 {
44  size_t cnt = 0;
45  for (HandleSeqIndex hsi: idx) cnt += hsi.size();
46  return cnt;
47 }
48 
50 {
51  Type t = a->getType();
52  HandleSeqIndex &hsi = idx[t];
53 
54  LinkPtr l(LinkCast(a));
55  if (NULL == l) return;
56 
57  hsi.insert(l->getOutgoingSet(), a->getHandle());
58 }
59 
61 {
62  Type t = a->getType();
63  HandleSeqIndex &hsi = idx[t];
64 
65  LinkPtr l(LinkCast(a));
66  if (NULL == l) return;
67 
68  hsi.remove(l->getOutgoingSet());
69 }
70 
72 {
73  if (t >= idx.size()) throw RuntimeException(TRACE_INFO,
74  "Index out of bounds for atom type (t = %lu)", t);
75  const HandleSeqIndex &hsi = idx[t];
76  return hsi.get(seq);
77 }
78 
79 void LinkIndex::remove(bool (*filter)(const Handle&))
80 {
81  for (HandleSeqIndex s : idx)
82  s.remove(filter);
83 }
84 
85 UnorderedHandleSet LinkIndex::getHandleSet(Type type, const HandleSeq& seq, bool subclass) const
86 {
88  if (subclass)
89  {
91  for (Type s = 0; s < max; s++)
92  {
93  // The 'AssignableFrom' direction is unit-tested in AtomSpaceUTest.cxxtest
94  if (classserver().isA(s, type))
95  {
96  if (s >= idx.size()) throw RuntimeException(TRACE_INFO,
97  "Index out of bounds for atom type (s = %lu)", s);
98  const HandleSeqIndex &hsi = idx[s];
99  Handle h = hsi.get(seq);
100  if (Handle::UNDEFINED != h)
101  hs.insert(h);
102  }
103  }
104  }
105  else
106  {
107  Handle h = getHandle(type, seq);
108  if (Handle::UNDEFINED != h)
109  hs.insert(h);
110  }
111  return hs;
112 }
113 
114 // ================================================================
std::vector< HandleSeqIndex > idx
Definition: LinkIndex.h:46
const Handle & get(const HandleSeq &seq) const
void remove(bool(*)(const Handle &))
Definition: LinkIndex.cc:79
std::vector< Handle > HandleSeq
a list of handles
Definition: Handle.h:246
UnorderedHandleSet getHandleSet(Type type, const HandleSeq &, bool subclass) const
Definition: LinkIndex.cc:85
std::shared_ptr< Atom > AtomPtr
Definition: Handle.h:48
void removeAtom(const AtomPtr &)
Definition: LinkIndex.cc:60
std::shared_ptr< Link > LinkPtr
Definition: Atom.h:53
ClassServer & classserver(ClassServerFactory *=ClassServer::createInstance)
Definition: ClassServer.cc:159
static const Handle UNDEFINED
Definition: Handle.h:77
void insert(const HandleSeq &seq, const Handle &h)
void insertAtom(const AtomPtr &)
Definition: LinkIndex.cc:49
static LinkPtr LinkCast(const Handle &h)
Definition: Link.h:263
unsigned short Type
type of Atoms, represented as short integer (16 bits)
Definition: types.h:40
std::unordered_set< Handle, handle_hash > UnorderedHandleSet
a hash that associates the handle to its unique identificator
Definition: Handle.h:250
size_t size() const
Definition: LinkIndex.cc:42
Handle getHandle(Type type, const HandleSeq &) const
Definition: LinkIndex.cc:71
void remove(const HandleSeq &seq)