OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ImportanceIndex.cc
Go to the documentation of this file.
1 /*
2  * opencog/atomspace/ImportanceIndex.cc
3  *
4  * Copyright (C) 2008-2011 OpenCog Foundation
5  * All Rights Reserved
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License v3 as
9  * published by the Free Software Foundation and including the exceptions
10  * at http://opencog.org/wiki/Licenses
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program; if not, write to:
19  * Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 #include <algorithm>
24 
25 #include <opencog/util/Config.h>
26 #include <opencog/util/functional.h>
27 #include <opencog/util/oc_assert.h>
28 
30 #include <opencog/atomspace/Atom.h>
32 
33 using namespace opencog;
34 
36 #define IMPORTANCE_INDEX_SIZE (1 << 11)
37 
39 {
41 }
42 
43 unsigned int ImportanceIndex::importanceBin(short importance)
44 {
45  // STI is in range of [-32768, 32767] so adding 32768 puts it in
46  // [0, 65535]
47  return (importance + 32768) / IMPORTANCE_INDEX_SIZE;
48 }
49 
51 {
52  int newbin = importanceBin(atom->getAttentionValue()->getSTI());
53  if (bin == newbin) return;
54 
55  remove(bin, atom);
56  insert(newbin, atom);
57 }
58 
60 {
61  int sti = atom->getAttentionValue()->getSTI();
62  int bin = importanceBin(sti);
63  insert(bin, atom);
64 }
65 
67 {
68  int sti = atom->getAttentionValue()->getSTI();
69  int bin = importanceBin(sti);
70  remove(bin, atom);
71 }
72 
74  const AtomTable* atomtable,
75  AttentionValue::sti_t lowerBound,
76  AttentionValue::sti_t upperBound) const
77 {
78  UnorderedAtomSet set;
79 
80  // The indexes for the lower bound and upper bound lists is returned.
81  int lowerBin = importanceBin(lowerBound);
82  int upperBin = importanceBin(upperBound);
83 
84  // Build a list of atoms whose importance is equal to the lower bound.
85  // For the lower bound and upper bound index, the list is filtered,
86  // because there may be atoms that have the same importanceIndex
87  // and whose importance is lower than lowerBound or bigger than
88  // upperBound.
89  const UnorderedAtomSet &sl = idx[lowerBin];
90  std::copy_if(sl.begin(), sl.end(), inserter(set),
91  [&](Atom* atom)->bool {
93  atom->getAttentionValue()->getSTI();
94  return (lowerBound <= sti and sti <= upperBound);
95  });
96 
97  // If both lower and upper bounds are in the same bin,
98  // Then we are done.
99  if (lowerBin == upperBin) {
100  UnorderedHandleSet ret;
101  std::transform(set.begin(), set.end(), inserter(ret),
102  [](Atom* atom)->Handle { return atom->getHandle(); });
103  return ret;
104  }
105 
106  // For every index within lowerBound and upperBound,
107  // add to the list.
108  while (++lowerBin < upperBin) {
109  const UnorderedAtomSet &ss = idx[lowerBin];
110  set.insert(ss.begin(), ss.end());
111  }
112 
113  // The two lists are concatenated.
114  const UnorderedAtomSet &uset = idx[upperBin];
115  std::copy_if(uset.begin(), uset.end(), inserter(set),
116  [&](Atom* atom)->bool {
117  AttentionValue::sti_t sti = atom->getAttentionValue()->getSTI();
118  return (lowerBound <= sti and sti <= upperBound);
119  });
120 
121  UnorderedHandleSet ret;
122  std::transform(set.begin(), set.end(), inserter(ret),
123  [](Atom* atom)->Handle { return atom->getHandle(); });
124  return ret;
125 }
AttentionValuePtr getAttentionValue()
Definition: Atom.cc:146
std::vector< UnorderedAtomSet > idx
static unsigned int importanceBin(short)
short sti_t
short-term importance type
std::unordered_set< Atom * > UnorderedAtomSet
#define IMPORTANCE_INDEX_SIZE
4092 importance bins means each bin has STI range of 32
void updateImportance(Atom *, int)
void insert(size_t i, Atom *a)
std::unordered_set< Handle, handle_hash > UnorderedHandleSet
a hash that associates the handle to its unique identificator
Definition: Handle.h:250
UnorderedHandleSet getHandleSet(const AtomTable *, AttentionValue::sti_t, AttentionValue::sti_t) const