OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TypeIndex.cc
Go to the documentation of this file.
1 /*
2  * opencog/atomspace/TypeIndex.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 "TypeIndex.h"
23 #include "Atom.h"
24 #include "ClassServer.h"
25 
26 using namespace opencog;
27 
29 {
30  resize();
31 }
32 
34 {
37 }
38 
39 size_t TypeIndex::getNumAtomsOfType(Type type, bool count_subclasses) const
40 {
41  iterator it(type, count_subclasses);
42  size_t atom_count = 0;
43  it.s = idx.begin();
44  it.send = idx.end();
45 
46  // Loop over all the types looking for type and optional subclasses.
47  Type current_type = 0;
48  while (it.s != it.send)
49  {
50  // If this type is a match...
51  if ((type == current_type) ||
52  (count_subclasses && (classserver().isA(type, it.type))))
53  {
54  // Add the size of the atom set for this type.
55  atom_count += idx.at(current_type).size();
56  }
57  current_type++;
58  ++it.s;
59  }
60 
61  return atom_count;
62 }
63 
64 // ================================================================
65 
67 {
68  iterator it(t, sub);
69  it.send = idx.end();
70 
71  it.s = idx.begin();
72  it.currtype = 0;
73  while (it.s != it.send)
74  {
75  // Find the first type which is a subtype, and start iteration there.
76  if ((it.type == it.currtype) ||
77  (sub && (classserver().isA(it.currtype, it.type))))
78  {
79  it.se = it.s->begin();
80  if (it.se != it.s->end()) return it;
81  }
82  it.currtype++;
83  ++it.s;
84  }
85 
86  return it;
87 }
88 
90 {
91  iterator it(num_types, false);
92  it.se = idx.at(num_types).end();
93  it.s = idx.end();
94  it.send = idx.end();
95  it.currtype = num_types;
96  return it;
97 }
98 
100 {
101  type = t;
102  subclass = sub;
103 }
104 
106 {
107  s = v.s;
108  send = v.send;
109  se = v.se;
110  currtype = v.currtype;
111  type = v.type;
112  subclass = v.subclass;
113  return *this;
114 }
115 
117 {
118  if (s == send) return Handle::UNDEFINED;
119  return (*se)->getHandle();
120 }
121 
123 {
124  if ((v.s == v.send) && (s == send)) return true;
125  return v.se == se;
126 }
127 
129 {
130  if ((v.s == v.send) && (s != send)) return v.se != se;
131  if ((v.s != v.send) && (s == send)) return v.se != se;
132  return false;
133 }
134 
136 {
137  return operator++(1);
138 }
139 
141 {
142  if (s == send) return *this;
143 
144  ++se;
145  if (se == s->end())
146  {
147  do
148  {
149  ++s;
150  currtype++;
151 
152  // Find the first type which is a subtype, and start iteration there.
153  if ((type == currtype) ||
154  (subclass && (classserver().isA(currtype, type))))
155  {
156  se = s->begin();
157  if (se != s->end()) return *this;
158  }
159  } while (s != send);
160  }
161 
162  return *this;
163 }
164 
165 // ================================================================
void resize(void)
Definition: TypeIndex.cc:33
UnorderedAtomSet::const_iterator se
Definition: TypeIndex.h:87
Handle getHandle()
Definition: Atom.h:211
iterator & operator=(iterator)
Definition: TypeIndex.cc:105
iterator begin(Type, bool) const
Definition: TypeIndex.cc:66
std::vector< UnorderedAtomSet >::const_iterator s
Definition: TypeIndex.h:84
std::vector< UnorderedAtomSet > idx
ClassServer & classserver(ClassServerFactory *=ClassServer::createInstance)
Definition: ClassServer.cc:159
size_t getNumAtomsOfType(Type type, bool subclass) const
Definition: TypeIndex.cc:39
static const Handle UNDEFINED
Definition: Handle.h:77
bool isA(Type sub, Type super)
Definition: ClassServer.h:144
iterator end(void) const
Definition: TypeIndex.cc:89
unsigned short Type
type of Atoms, represented as short integer (16 bits)
Definition: types.h:40
std::vector< UnorderedAtomSet >::const_iterator send
Definition: TypeIndex.h:85