Skip to content

Commit 5943c67

Browse files
Merge pull request #965 from htm-community/optimize-getOverlap
Optimize SDR::getOverlap to use sparse instead of dense.
2 parents 58dd087 + bb7a752 commit 5943c67

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/htm/types/Sdr.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,25 @@ namespace htm {
283283
NTA_ASSERT( dimensions == sdr.dimensions );
284284

285285
UInt ovlp = 0u;
286-
const auto a = this->getDense();
287-
const auto b = sdr.getDense();
288-
for( UInt i = 0u; i < size; i++ )
289-
ovlp += a[i] && b[i];
286+
const auto a_sparse = this->getSparse();
287+
const auto b_sparse = sdr.getSparse();
288+
long unsigned int a_idx = 0u;
289+
long unsigned int b_idx = 0u;
290+
while( a_idx < a_sparse.size() && b_idx < b_sparse.size() ) {
291+
auto a = a_sparse[a_idx];
292+
auto b = b_sparse[b_idx];
293+
if( a == b ) {
294+
ovlp += 1u;
295+
a_idx += 1u;
296+
b_idx += 1u;
297+
}
298+
else if( a > b ) {
299+
b_idx += 1u;
300+
}
301+
else {
302+
a_idx += 1u;
303+
}
304+
}
290305
return ovlp;
291306
}
292307

0 commit comments

Comments
 (0)