OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

PackedList.H

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Class
00026     Foam::PackedList
00027 
00028 Description
00029     A dynamically allocatable list of packed unsigned integers.
00030 
00031     The list resizing is similar to DynamicList, thus the methods clear()
00032     and setSize() behave like their DynamicList counterparts and the methods
00033     reserve() and setCapacity() can be used to influence the allocation.
00034 
00035     The number of bits per item is specified by the template parameter nBits.
00036 
00037 Note
00038     In a const context, the '[]' operator simply returns the stored value,
00039     with out-of-range elements returned as zero.
00040     In a non-const context, the '[]' operator returns an iteratorBase, which
00041     might not have a valid reference for out-of-range elements.
00042     The iteratorBase class handles the assignment of new values.
00043 
00044     Using the iteratorBase as a proxy allows assignment of values
00045     between list elements. Thus the following bit of code works as expected:
00046     @code
00047         list[1] = list[5];      // value assignment, not iterator position
00048         list[2] = list[5] = 4;  // propagates value
00049         list[1] = list[5] = list[6];  // propagates value
00050     @endcode
00051 
00052     Using get() or the '[]' operator are similarly fast. Looping and reading
00053     via an iterator is approx. 15% slower, but can be more flexible.
00054 
00055     Using the set() operator (and the '[]' operator) are marginally slower
00056     (approx. 5%) than using an iterator, but the set() method has the
00057     advantage of also returning a bool if the value changed.  This can be
00058     useful for branching on changed values.
00059 
00060     @code
00061         list[5] = 4;
00062         changed = list.set(5, 8);
00063         if (changed) ...
00064     @endcode
00065 
00066     The lazy evaluation used means that reading an out-of-range element
00067     returns zero, but does not affect the list size.  Even in a non-const
00068     context, only the assigment itself causes the element to be created.
00069     For example,
00070     @code
00071         list.resize(4);
00072         Info<< list[10] << "\n";  // print zero, but doesn't adjust list
00073         list[8] = 1;
00074     @endcode
00075 
00076 SeeAlso
00077     Foam::DynamicList
00078 
00079 SourceFiles
00080     PackedListI.H
00081     PackedList.C
00082 
00083 \*---------------------------------------------------------------------------*/
00084 
00085 #ifndef PackedList_H
00086 #define PackedList_H
00087 
00088 #include "labelList.H"
00089 #include "StaticAssert.H"
00090 
00091 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00092 
00093 namespace Foam
00094 {
00095 
00096 // Forward declaration of friend functions and operators
00097 template<unsigned nBits> class PackedList;
00098 
00099 // template<unsigned nBits>
00100 // Ostream& operator<<(Ostream&, const PackedList<nBits>&);
00101 
00102 
00103 /*---------------------------------------------------------------------------*\
00104                       Class PackedListName Declaration
00105 \*---------------------------------------------------------------------------*/
00106 
00107 TemplateName(PackedList);
00108 
00109 /*---------------------------------------------------------------------------*\
00110                          Class PackedList Declaration
00111 \*---------------------------------------------------------------------------*/
00112 
00113 template<unsigned nBits=1>
00114 class PackedList
00115 :
00116     private List<unsigned int>
00117 {
00118     typedef unsigned int      StorageType;
00119     typedef List<StorageType> StorageList;
00120 
00121     //- nBits must be positive (non-zero) and fit within the storage
00122     //  For simplicity, assume 8-bit bytes
00123     StaticAssert(nBits && nBits < (sizeof(StorageType) << 3));
00124 
00125     // Private data
00126 
00127         //- Number of nBits entries
00128         label size_;
00129 
00130     // Private Member Functions
00131 
00132         //- Calculate the list length when packed
00133         inline static label packedLength(const label);
00134 
00135 public:
00136 
00137     // Public data
00138 
00139         //- The max. number of bits that can be templated.
00140         //  Might someday be useful for a template assert.
00141         inline static unsigned int max_bits();
00142 
00143         //- The max. value for an entry, which simultaneously the bit-mask
00144         //  eg, ((1 << 2) - 1) yields 0b0011
00145         inline static unsigned int max_value();
00146 
00147         //- The number of entries per packed storage element
00148         inline static unsigned int packing();
00149 
00150         //- Masking for all bits below the offset
00151         inline static unsigned int maskLower(unsigned offset);
00152 
00153     // Forward declaration of iterators
00154 
00155         class iteratorBase;
00156         class iterator;
00157         class const_iterator;
00158 
00159     // Constructors
00160 
00161         //- Null constructor
00162         inline PackedList();
00163 
00164         //- Construct with given size, initializes list to 0.
00165         inline PackedList(const label size);
00166 
00167         //- Construct with given size and value for all elements.
00168         PackedList(const label size, const unsigned val);
00169 
00170         //- Copy constructor.
00171         inline PackedList(const PackedList<nBits>&);
00172 
00173         //- Construct by transferring the parameter contents
00174         inline PackedList(const Xfer< PackedList<nBits> >&);
00175 
00176         //- Construct from a list of labels
00177         PackedList(const UList<label>&);
00178 
00179         //- Clone
00180         inline autoPtr< PackedList<nBits> > clone() const;
00181 
00182     // Member Functions
00183 
00184         // Access
00185 
00186         //- The number of elements that can be stored before reallocating
00187         inline label capacity() const;
00188 
00189         //- Number of entries.
00190         inline label size() const;
00191 
00192         //- Return true if the list is empty (ie, size() is zero).
00193         inline bool empty() const;
00194 
00195         //- Get value at index I.
00196         //  Never auto-vivify entries.
00197         inline unsigned int get(const label) const;
00198 
00199         //- Set value at index I. Return true if value changed.
00200         //  Does auto-vivify for non-existent entries.
00201         //  Default value set is the max_value.
00202         inline bool set(const label, const unsigned int val = ~0u);
00203 
00204         //- Return the underlying packed storage
00205         inline List<unsigned int>& storage();
00206 
00207         //- Return the underlying packed storage
00208         inline const List<unsigned int>& storage() const;
00209 
00210         //- Count number of bits set, O(log(n))
00211         //  Uses the Hamming weight (population count) method
00212         //  http://en.wikipedia.org/wiki/Hamming_weight
00213         unsigned int count() const;
00214 
00215         //- Return the values as a labelList
00216         labelList values() const;
00217 
00218         //- Print values and information
00219         Ostream& print(Ostream&) const;
00220 
00221         // Edit
00222 
00223         //- Trim any trailing zero elements
00224         bool trim();
00225 
00226         //- Invert the bits in the addressable region.
00227         void flip();
00228 
00229         //- Alter the size of the underlying storage.
00230         //  The addressed size will be truncated if needed to fit, but will
00231         //  remain otherwise untouched.
00232         inline void setCapacity(const label);
00233 
00234         //- Reset addressable list size, does not shrink the allocated size.
00235         //  Optionally specify a value for new elements.
00236         inline void resize(const label, const unsigned int& val = 0);
00237 
00238         //- Alias for resize()
00239         inline void setSize(const label, const unsigned int& val = 0);
00240 
00241         //- Reserve allocation space for at least this size.
00242         //  Never shrinks the allocated size.
00243         inline void reserve(const label);
00244 
00245         //- Clear the list, i.e. set addressable size to zero.
00246         //  Does not adjust the underlying storage
00247         inline void clear();
00248 
00249         //- Clear the list and delete storage.
00250         inline void clearStorage();
00251 
00252         //- Shrink the allocated space to what is used.
00253         inline void shrink();
00254 
00255         //- Transfer the contents of the argument list into this list
00256         //  and annull the argument list.
00257         inline void transfer(PackedList<nBits>&);
00258 
00259         //- Transfer contents to the Xfer container
00260         inline Xfer< PackedList<nBits> > xfer();
00261 
00262 
00263     // Member operators
00264 
00265         //- Append a value at the end of the list
00266         inline void append(const unsigned int val);
00267 
00268         //- Remove and return the last element
00269         inline unsigned int remove();
00270 
00271         //- Get value at index I
00272         //  Never auto-vivify entries.
00273         inline unsigned int operator[](const label) const;
00274 
00275         //- Set value at index I.
00276         //  Returns iterator to perform the actual operation.
00277         //  Does not auto-vivify entries, but will when assigned to.
00278         inline iteratorBase operator[](const label);
00279 
00280         //- Assignment of all entries to the given value. Takes linear time.
00281         inline void operator=(const unsigned int val);
00282 
00283         //- Assignment operator. Takes linear time.
00284         void operator=(const PackedList<nBits>&);
00285 
00286         //- Assignment operator. Takes linear time.
00287         void operator=(const UList<label>&);
00288 
00289     // Ostream operator
00290 
00291 //      // Write PackedList to Ostream.
00292 //      friend Ostream& operator<< <nBits> (Ostream&, const PackedList<nBits>&);
00293 
00294     // Iterators and helpers
00295 
00296         //- The iterator base for PackedList
00297         //  Note: data and functions are protected, to allow reuse by iterator
00298         //  and prevent most external usage.
00299         class iteratorBase
00300         {
00301             friend class PackedList;
00302 
00303         protected:
00304 
00305             // Protected Data
00306 
00307                 //- Pointer to original list
00308                 //  This also lets us use the default bitwise copy/assignment
00309                 PackedList* list_;
00310 
00311                 //- Element index
00312                 label index_;
00313 
00314             // Protected Member Functions
00315 
00316                 //- Get value as unsigned, no range-checking
00317                 inline unsigned int get() const;
00318 
00319                 //- Set value, returning true if changed, no range-checking
00320                 inline bool set(unsigned int);
00321 
00322             // Constructors
00323 
00324                 //- Construct null
00325                 inline iteratorBase();
00326 
00327                 //- Construct from base list and position index
00328                 inline iteratorBase(const PackedList*, const label);
00329 
00330         public:
00331 
00332             // Member Operators
00333 
00334                 //- Compare values (not positions)
00335                 inline bool operator==(const iteratorBase&) const;
00336                 inline bool operator!=(const iteratorBase&) const;
00337 
00338                 //- Assign value, not position.
00339                 //  This allows packed[0] = packed[3] for assigning values
00340                 inline unsigned int operator=(const iteratorBase&);
00341 
00342                 //- Assign value.
00343                 //  A non-existent entry will be auto-vivified.
00344                 inline unsigned int operator=(const unsigned int val);
00345 
00346                 //- Conversion operator
00347                 //  Never auto-vivify entries.
00348                 inline operator unsigned int () const;
00349 
00350             //- Print value and information
00351             Ostream& print(Ostream&) const;
00352         };
00353 
00354 
00355         //- The iterator class used for PackedList
00356         class iterator
00357         :
00358             public iteratorBase
00359         {
00360 
00361             //- Disallow copy constructor from const_iterator - violates const-ness!
00362             iterator(const const_iterator&);
00363 
00364             //- Disallow assignment from const_iterator - violates const-ness!
00365             void operator=(const const_iterator&);
00366 
00367         public:
00368 
00369             // Constructors
00370 
00371                 //- Construct null
00372                 inline iterator();
00373 
00374                 //- Construct from iterator base, eg iter(packedlist[i])
00375                 //  but also  "iterator iter = packedlist[i];"
00376                 //  An out-of-range iterator is assigned end()
00377                 inline iterator(const iteratorBase&);
00378 
00379                 //- Construct from base list and position index
00380                 inline iterator(const PackedList*, const label);
00381 
00382             // Member Operators
00383 
00384                 //- Compare positions (not values)
00385                 inline bool operator==(const iteratorBase&) const;
00386                 inline bool operator!=(const iteratorBase&) const;
00387 
00388                 //- Assign from iteratorBase, eg iter = packedlist[i]
00389                 //  An out-of-range iterator is assigned end()
00390                 inline iterator& operator=(const iteratorBase&);
00391 
00392                 //- Return value
00393                 inline unsigned int operator*() const;
00394 
00395                 //- Return value
00396                 inline unsigned int operator()() const;
00397 
00398                 //- Return iteratorBase for assigning values
00399                 inline iteratorBase& operator*();
00400 
00401                 //- Return iteratorBase for assigning values
00402                 inline iteratorBase& operator()();
00403 
00404                 inline iterator& operator++();
00405                 inline iterator operator++(int);
00406 
00407                 inline iterator& operator--();
00408                 inline iterator operator--(int);
00409 
00410         };
00411 
00412         //- iterator set to the beginning of the PackedList
00413         inline iterator begin();
00414 
00415         //- iterator set to beyond the end of the PackedList
00416         inline iterator end();
00417 
00418 
00419         //- The const_iterator for PackedList
00420         class const_iterator
00421         :
00422             public iteratorBase
00423         {
00424         public:
00425 
00426             // Constructors
00427 
00428                 //- Construct null
00429                 inline const_iterator();
00430 
00431                 //- Construct from iterator base, eg iter(packedlist[i])
00432                 //  but also  "const_iterator iter = packedlist[i];"
00433                 //  An out-of-range iterator is assigned cend()
00434                 inline const_iterator(const iteratorBase&);
00435 
00436                 //- Construct from base list and position index
00437                 inline const_iterator(const PackedList*, const label);
00438 
00439                 //- Construct from iterator
00440                 inline const_iterator(const iterator&);
00441 
00442             // Member operators
00443 
00444                 //- Compare positions (not values)
00445                 inline bool operator==(const iteratorBase&) const;
00446                 inline bool operator!=(const iteratorBase&) const;
00447 
00448                 //- Assign from iteratorBase or derived
00449                 //  eg, iter = packedlist[i] or even iter = list.begin()
00450                 inline const_iterator& operator=(const iteratorBase&);
00451 
00452                 //- Return referenced value directly
00453                 inline unsigned int operator*() const;
00454 
00455                 //- Return referenced value directly
00456                 inline unsigned int operator()() const;
00457 
00458                 inline const_iterator& operator++();
00459                 inline const_iterator operator++(int);
00460 
00461                 inline const_iterator& operator--();
00462                 inline const_iterator operator--(int);
00463 
00464         };
00465 
00466 
00467         //- const_iterator set to the beginning of the PackedList
00468         inline const_iterator cbegin() const;
00469 
00470         //- const_iterator set to beyond the end of the PackedList
00471         inline const_iterator cend() const;
00472 
00473         //- const_iterator set to the beginning of the PackedList
00474         inline const_iterator begin() const;
00475 
00476         //- const_iterator set to beyond the end of the PackedList
00477         inline const_iterator end() const;
00478 
00479 };
00480 
00481 
00482 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00483 
00484 } // End namespace Foam
00485 
00486 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00487 
00488 #include "PackedListI.H"
00489 
00490 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00491 
00492 #ifdef NoRepository
00493 #   include "PackedList.C"
00494 #endif
00495 
00496 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00497 
00498 #endif
00499 
00500 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd