OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
NodeIndex.cc
Go to the documentation of this file.
1 /*
2  * opencog/atomspace/NodeIndex.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 
23 #include <opencog/atomspace/Atom.h>
25 #include <opencog/atomspace/atom_types.h>
26 
27 using namespace opencog;
28 
30 {
31  resize();
32 }
33 
35 {
36  idx.resize(classserver().getNumberOfClasses());
37 }
38 
39 size_t NodeIndex::size() const
40 {
41  size_t cnt = 0;
42  for (const NameIndex& ni : idx) cnt += ni.size();
43  return cnt;
44 }
45 
46 UnorderedHandleSet NodeIndex::getHandleSet(Type type, const std::string& name,
47  bool subclass) const
48 {
50  if (subclass) {
51 
52  Type max = idx.size();
53  for (Type s = 0; s < max; s++) {
54  if (classserver().isA(s, type)) {
55  Atom* atom = getAtom(s, name);
56  if (atom)
57  hs.insert(atom->getHandle());
58  }
59  }
60  } else {
61  Atom* atom = getAtom(type, name);
62  if (atom) hs.insert(atom->getHandle());
63  }
64 
65  return hs;
66 }
67 
68 // ================================================================
Atom * getAtom(Type type, const std::string &str) const
Definition: NodeIndex.h:63
Handle getHandle()
Definition: Atom.h:211
ClassServer & classserver(ClassServerFactory *=ClassServer::createInstance)
Definition: ClassServer.cc:159
UnorderedHandleSet getHandleSet(Type type, const std::string &, bool subclass) const
Definition: NodeIndex.cc:46
std::vector< NameIndex > idx
Definition: NodeIndex.h:45
size_t size() const
Definition: NodeIndex.cc:39
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