OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
NodeIndex.h
Go to the documentation of this file.
1 /*
2  * opencog/atomspace/NodeIndex.h
3  *
4  * Copyright (C) 2008,2015 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 #ifndef _OPENCOG_NODEINDEX_H
23 #define _OPENCOG_NODEINDEX_H
24 
25 #include <set>
26 #include <vector>
27 
30 
31 namespace opencog
32 {
42 class NodeIndex
43 {
44  private:
45  std::vector<NameIndex> idx;
46 
47  public:
48  NodeIndex();
49 
50  void insertAtom(Atom* a)
51  {
52  NameIndex &ni(idx[a->getType()]);
53  ni.insertAtom(a);
54  }
55  void removeAtom(Atom* a)
56  {
57  NameIndex &ni(idx.at(a->getType()));
58  ni.removeAtom(a);
59  }
60  void resize();
61  size_t size() const;
62 
63  Atom* getAtom(Type type, const std::string& str) const
64  {
65  const NameIndex &ni(idx.at(type));
66  return ni.get(str);
67  }
68 
69  UnorderedHandleSet getHandleSet(Type type, const std::string&, bool subclass) const;
70 
71  template <typename OutputIterator> OutputIterator
72  getHandleSet(OutputIterator result,
73  Type type, const std::string& name, bool subclass) const
74  {
75  if (not subclass)
76  {
77  Atom* atom = getAtom(type, name);
78  if (atom) *result++ = atom->getHandle();
79  }
80  else
81  {
82  Type max = idx.size();
83  for (Type s = 0; s < max; s++) {
84  if (classserver().isA(s, type)) {
85  Atom* atom = getAtom(s, name);
86  if (atom) *result++ = atom->getHandle();
87  }
88  }
89  }
90  return result;
91  }
92 };
93 
95 } //namespace opencog
96 
97 #endif // _OPENCOG_NODEINDEX_H
Atom * get(const std::string &str) const
Definition: StringIndex.h:50
void insertAtom(Atom *a)
Definition: NodeIndex.h:50
Atom * getAtom(Type type, const std::string &str) const
Definition: NodeIndex.h:63
Handle getHandle()
Definition: Atom.h:211
Type getType() const
Definition: Atom.h:197
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
void removeAtom(Atom *a)
Definition: NameIndex.h:50
void removeAtom(Atom *a)
Definition: NodeIndex.h:55
OutputIterator getHandleSet(OutputIterator result, Type type, const std::string &name, bool subclass) const
Definition: NodeIndex.h:72
size_t size() const
Definition: NodeIndex.cc:39
void insertAtom(Atom *a)
Definition: NameIndex.h:44
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