Skip to content

Commit cee5fcb

Browse files
committed
Simplify code for AtomicStructureAdapter indexing.
Remove unnecessary branching in the get_slice function.
1 parent 64d6099 commit cee5fcb

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

srrealmodule/wrap_AtomicStructureAdapter.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -374,25 +374,22 @@ class atomadapter_indexing : public vector_indexing_suite<
374374
static object
375375
get_slice(Container& container, index_type from, index_type to)
376376
{
377+
// make sure slice is of a correct type and has a copy
378+
// of any additional structure data.
377379
StructureAdapterPtr rv = container.clone();
378380
AtomicStructureAdapterPtr rva;
379381
rva = boost::static_pointer_cast<AtomicStructureAdapter>(rv);
380-
// short-circuit for long slices
381-
if ((to - from) > rva->countSites() / 2)
382+
// handle index ranges for a valid and empty slice
383+
if (from <= to)
382384
{
383385
rva->erase(rva->begin() + to, rva->end());
384386
rva->erase(rva->begin(), rva->begin() + from);
385-
return object(rv);
386-
}
387-
// otherwise save memory for short slices
388-
rva->clear();
389-
rv = rva->clone();
390-
rva = boost::static_pointer_cast<AtomicStructureAdapter>(rv);
391-
if (from <= to)
392-
{
393-
rva->assign(container.begin() + from, container.begin() + to);
394387
}
395-
return object(rv);
388+
else rva->clear();
389+
// save memory by making a new copy for short slices
390+
const bool longslice = ((to - from) > rva->countSites() / 2);
391+
object pyrv(longslice ? rv : rv->clone());
392+
return pyrv;
396393
}
397394

398395

0 commit comments

Comments
 (0)