mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: add List::resize_unsafe(label)
- changes the addressed list size without affecting list allocation. Can be useful for the following type of coding pattern: - pre-allocate a List with some max content length - populate with some content (likely not the entire pre-allocated size) - truncate the list to the length of valid content - process the List - discard the List Since the List is being discarded, using resize_unsafe() instead of resize() avoids an additional allocation with the new size and copying/moving of the elements. This programming pattern can also be used when the List is being returned from a subroutine, and carrying about a bit of unused memory is less important than avoiding reallocation + copy/move. If used improperly, it can obviously result in addressing into unmanaged memory regions (ie, 'unsafe').
This commit is contained in:
committed by
Andrew Heather
parent
0f53871fd3
commit
96c9bf8615
@ -348,6 +348,32 @@ int main(int argc, char *argv[])
|
||||
Info<< "input = " << flatOutput(input1) << nl
|
||||
<< "remove " << range << " = " << flatOutput(list1) << nl;
|
||||
}
|
||||
|
||||
{
|
||||
input1 = identity(5);
|
||||
list1 = identity(4, 5);
|
||||
list1.reserve(10);
|
||||
|
||||
Info<< nl << "test swap(List)" << nl;
|
||||
Info<< " input: " << input1.size()
|
||||
<< '/' << input1.capacity() << ' '
|
||||
<< flatOutput(input1) << nl;
|
||||
Info<< " list: " << list1.size() << '/'
|
||||
<< list1.capacity() << ' '
|
||||
<< flatOutput(list1) << nl;
|
||||
|
||||
|
||||
// input1.swap(list1); // This is wrong!
|
||||
list1.swap(input1); // Correct
|
||||
|
||||
Info<< "after swap:" << nl;
|
||||
Info<< " input: " << input1.size()
|
||||
<< '/' << input1.capacity() << ' '
|
||||
<< flatOutput(input1) << nl;
|
||||
Info<< " list: " << list1.size() << '/'
|
||||
<< list1.capacity() << ' '
|
||||
<< flatOutput(list1) << nl;
|
||||
}
|
||||
}
|
||||
|
||||
Info<< "\nEnd\n";
|
||||
|
||||
Reference in New Issue
Block a user