From d2d39c32bee847726da8dbe394920755734376b1 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Tue, 8 Dec 2009 10:01:48 +0100 Subject: [PATCH] HashSet and PackedList get an unset() method - provides a convenient (and lazy) means of removing entries --- applications/test/PackedList1/PackedListTest1.C | 6 ++++++ .../containers/HashTables/HashSet/HashSet.H | 6 ++++++ .../containers/Lists/PackedList/PackedList.H | 8 +++++++- .../containers/Lists/PackedList/PackedListI.H | 13 +++++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/applications/test/PackedList1/PackedListTest1.C b/applications/test/PackedList1/PackedListTest1.C index 05f3a077f8..4cd231cc3e 100644 --- a/applications/test/PackedList1/PackedListTest1.C +++ b/applications/test/PackedList1/PackedListTest1.C @@ -58,9 +58,15 @@ int main(int argc, char *argv[]) list1.print(Info); Info<< "\ntest set() with default argument (max_value)\n"; + list1.set(1); list1.set(3); list1.print(Info); + Info<< "\ntest unset() with in-range and out-of-range\n"; + list1.unset(3); + list1.unset(100000); + list1.print(Info); + Info<< "\ntest assign between references\n"; list1[2] = 3; list1[4] = list1[2]; diff --git a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H index 15d408bf87..808a927b9e 100644 --- a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H +++ b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H @@ -136,6 +136,12 @@ public: return insert(lst); } + //- Unset the specified key - same as erase + bool unset(const Key& key) + { + return HashTable::erase(key); + } + // Member Operators diff --git a/src/OpenFOAM/containers/Lists/PackedList/PackedList.H b/src/OpenFOAM/containers/Lists/PackedList/PackedList.H index 78c7306b6e..2ef6d29d0c 100644 --- a/src/OpenFOAM/containers/Lists/PackedList/PackedList.H +++ b/src/OpenFOAM/containers/Lists/PackedList/PackedList.H @@ -201,6 +201,10 @@ public: // Default value set is the max_value. inline bool set(const label, const unsigned int val = ~0u); + //- Unset the entry at index I. Return true if value changed. + // Never auto-vivify entries. + inline bool unset(const label); + //- Return the underlying packed storage inline List& storage(); @@ -240,6 +244,8 @@ public: //- Reserve allocation space for at least this size. // Never shrinks the allocated size. + // The list size is adjusted as per DynamicList with + // SizeInc=0, SizeMult=2, SizeDiv=1 inline void reserve(const label); //- Clear the list, i.e. set addressable size to zero. @@ -249,7 +255,7 @@ public: //- Clear the list and delete storage. inline void clearStorage(); - //- Shrink the allocated space to what is used. + //- Shrink the allocated space to what is actually used. inline void shrink(); //- Transfer the contents of the argument list into this list diff --git a/src/OpenFOAM/containers/Lists/PackedList/PackedListI.H b/src/OpenFOAM/containers/Lists/PackedList/PackedListI.H index ee993e28df..ea0267ccf4 100644 --- a/src/OpenFOAM/containers/Lists/PackedList/PackedListI.H +++ b/src/OpenFOAM/containers/Lists/PackedList/PackedListI.H @@ -810,6 +810,19 @@ inline bool Foam::PackedList::set } +template +inline bool Foam::PackedList::unset(const label i) +{ + // lazy - ignore out-of-bounds + if (i < 0 || i >= size_) + { + return false; + } + + return iteratorBase(this, i).set(0u); +} + + template inline void Foam::PackedList::append(const unsigned int val) {