@@ -316,23 +316,30 @@ vector< unordered_set<int> > OverlapCalculator::neighborhoods() const
316316 typedef unordered_set<int > SiteSet;
317317 typedef std::vector< boost::shared_ptr<SiteSet> > SiteSetPointers;
318318 SiteSetPointers rvptr (cntsites);
319+ // helper function that initializes neighbor set at site i
320+ auto initsiteset = [&](int i, int & counter) {
321+ if (!rvptr[i]) {
322+ rvptr[i].reset (new SiteSet (&i, &i + 1 ));
323+ ++counter;
324+ }
325+ };
326+ // create self-neighborhoods unless prohibited by mask
327+ for (int j0 = 0 , cnt = 0 ; j0 < cntsites && cnt < cntsites; ++j0)
328+ {
329+ for (int j1 = j0; j1 < cntsites; ++j1)
330+ {
331+ if (!this ->getPairMask (j0, j1)) continue ;
332+ initsiteset (j0, cnt);
333+ initsiteset (j1, cnt);
334+ }
335+ }
319336 int n = this ->count ();
320337 for (int index = 0 ; index < n; ++index)
321338 {
322339 double olp = this ->suboverlap (index);
323340 if (olp <= 0.0 ) continue ;
324341 int j0 = int (this ->subvalue (SITE0_OFFSET, index));
325342 int j1 = int (this ->subvalue (SITE1_OFFSET, index));
326- if (!rvptr[j0].get ())
327- {
328- rvptr[j0].reset (new SiteSet);
329- rvptr[j0]->insert (j0);
330- }
331- if (!rvptr[j1].get ())
332- {
333- rvptr[j0]->insert (j1);
334- rvptr[j1] = rvptr[j0];
335- }
336343 assert (rvptr[j0].get () && rvptr[j1].get ());
337344 if (rvptr[j0].get () == rvptr[j1].get ()) continue ;
338345 // here we need to unify the j0 and j1 sets
@@ -344,18 +351,12 @@ vector< unordered_set<int> > OverlapCalculator::neighborhoods() const
344351 rvptr[*idx1] = rvptr[j0];
345352 }
346353 }
347- // replace any unassigned items with a self-neighborhoods
348- for (int i = 0 ; i < cntsites; ++i)
349- {
350- if (rvptr[i].get ()) continue ;
351- rvptr[i].reset (new SiteSet (&i, &i + 1 ));
352- }
353354 unordered_set<const SiteSet*> duplicate;
354355 vector< unordered_set<int > > rv;
355356 SiteSetPointers::const_iterator ssp;
356357 for (ssp = rvptr.begin (); ssp != rvptr.end (); ++ssp)
357358 {
358- if (duplicate.count (ssp->get ())) continue ;
359+ if (!ssp-> get () || duplicate.count (ssp->get ())) continue ;
359360 duplicate.insert (ssp->get ());
360361 rv.push_back (**ssp);
361362 }
0 commit comments