Mesh Oriented datABase  (version 5.4.1)
Array-based unstructured mesh datastructure
TupleList.hpp
Go to the documentation of this file.
00001 /*-----------------------------------------------------------------------------
00002 
00003   Tuple list definition and utilities
00004 
00005   Conceptually, a tuple list is a list of n records or tuples,
00006   each with mi integers, ml longs, mul Ulongs, and mr reals
00007   (these types are defined in "types.h" as sint, slong, moab::EntityHandle, real;
00008   it may be that sint==slong)
00009 
00010   There are four arrays, one for each type (vi,vl,vul,vr),
00011   with records layed out contiguously within each array
00012 
00013   -----------------------------------------------------------------------------*/
00014 
00015 #ifndef TUPLE_LIST_HPP
00016 #define TUPLE_LIST_HPP
00017 
00018 #include 
00019 #include 
00020 
00021 #include "moab/Types.hpp"
00022 #include 
00023 
00024 /* Integral types defined here to ensure variable type sizes are consistent */
00025 /* integer type to use for everything */
00026 #if defined( USE_LONG )
00027 #define INTEGER long
00028 #elif defined( USE_LONG_LONG )
00029 #define INTEGER long long
00030 #elif defined( USE_SHORT )
00031 #define INTEGER short
00032 #else
00033 #define INTEGER int
00034 #endif
00035 
00036 /* when defined, use the given type for global indices instead of INTEGER */
00037 #if defined( USE_GLOBAL_LONG_LONG )
00038 #define GLOBAL_INT long long
00039 #elif defined( USE_GLOBAL_LONG )
00040 #define GLOBAL_INT long
00041 #else
00042 #define GLOBAL_INT long
00043 #endif
00044 
00045 /* floating point type to use for everything */
00046 #if defined( USE_FLOAT )
00047 typedef float realType;
00048 #elif defined( USE_LONG_DOUBLE )
00049 typedef long double realType;
00050 #else
00051 typedef double realType;
00052 #endif
00053 
00054 /* apparently uint and ulong can be defined already in standard headers */
00055 #define uint uint_
00056 //#define ulong ulong_
00057 #define sint  sint_
00058 #define slong slong_
00059 
00060 typedef signed INTEGER sint;
00061 typedef unsigned INTEGER uint;
00062 #undef INTEGER
00063 
00064 #ifdef GLOBAL_INT
00065 typedef signed GLOBAL_INT slong;
00066 // typedef unsigned GLOBAL_INT ulong;
00067 #else
00068 typedef sint slong;
00069 // typedef uint ulong;
00070 #endif
00071 
00072 typedef moab::EntityHandle Ulong;
00073 
00074 namespace moab
00075 {
00076 void fail( const char* fmt, ... );
00077 
00078 class TupleList
00079 {
00080   public:
00081     /*---------------------------------------------------------------------------
00082 
00083       buffer: a simple class which can be used to store data
00084 
00085       The ptr points to the chunk of memory allocated for the buffer's use.  The
00086       size denotes the size of the allocated memory; the user must take care to
00087       note how much of the buffer they are using.
00088 
00089       ---------------------------------------------------------------------------*/
00090     class buffer
00091     {
00092       public:
00093         size_t buffSize;
00094         char* ptr;
00095 
00096         /**Constructor which sets an initial capacity of the buffer
00097          */
00098         buffer( size_t sz );
00099 
00100         /**Default constructor (Note:  buffer must be initialized before use!)
00101          */
00102         buffer();
00103 
00104         ~buffer()
00105         {
00106             this->reset();
00107         };
00108 
00109         /**Initializes the buffer to have a capacity of size
00110          */
00111         void buffer_init_( size_t sz, const char* file );
00112 
00113         /**Ensures that the buffer has at least a capacity of min
00114          */
00115         void buffer_reserve_( size_t min, const char* file );
00116 
00117         /**Frees any allocated memory used by the buffer
00118          */
00119         void reset();
00120 
00121         // Aliases for using the buffer methods
00122 #define buffer_init( sz )     buffer_init_( sz, __FILE__ )
00123 #define buffer_reserve( min ) buffer_reserve_( min, __FILE__ )
00124     };
00125 
00126   public:
00127     /**Constructor that takes all parameters and initializes the TupleList
00128      */
00129     TupleList( uint mi, uint ml, uint mul, uint mr, uint max );
00130 
00131     /**Default constructor (Note:  TupleList must be initialized before use!)
00132      */
00133     TupleList();
00134 
00135     ~TupleList()
00136     {
00137         reset();
00138     };
00139 
00140     /**Initializes the starting memory to be used by the TupleList
00141      * Note:  TupleLists must be initialized before they can be used
00142      *
00143      * param mi   number of signed ints in each tuple
00144      * param ml   number of long ints in each tuple
00145      * param mul  number of unsigned long ints in each tuple
00146      * param mr   number of reals in each tuple
00147      * param max  starting capacity of max tuples in the TupleList
00148      */
00149     void initialize( uint mi, uint ml, uint mul, uint mr, uint max );
00150 
00151     /**Resizes the TupleList to a new max
00152      *
00153      * param max   the new max capacity of the TupleList
00154      */
00155     ErrorCode resize( uint max );
00156 
00157     /**Sorts the TupleList by 'key'
00158      * if key
00317     struct SortData
00318     {
00319         Value v;
00320         Index i;
00321     };
00322 
00323 #define DIGIT_BITS      8
00324 #define DIGIT_VALUES    ( 1 << DIGIT_BITS )
00325 #define DIGIT_MASK      ( (Value)( DIGIT_VALUES - 1 ) )
00326 #define CEILDIV( a, b ) ( ( ( a ) + (b)-1 ) / ( b ) )
00327 #define DIGITS          CEILDIV( CHAR_BIT * sizeof( Value ), DIGIT_BITS )
00328 #define VALUE_BITS      ( DIGIT_BITS * DIGITS )
00329 #define COUNT_SIZE      ( DIGITS * DIGIT_VALUES )
00330 
00331     template < class Value >
00332     static Value radix_count( const Value* A, const Value* end, Index stride, Index count[DIGITS][DIGIT_VALUES] );
00333 
00334     static void radix_offsets( Index* c );
00335 
00336     template < class Value >
00337     static unsigned radix_zeros( Value bitorkey, Index count[DIGITS][DIGIT_VALUES], unsigned* shift, Index** offsets );
00338 
00339     template < class Value >
00340     static void radix_index_pass_b( const Value* A,
00341                                     Index n,
00342                                     Index stride,
00343                                     unsigned sh,
00344                                     Index* off,
00345                                     SortData< Value >* out );
00346 
00347     template < class Value >
00348     static void radix_index_pass_m( const SortData< Value >* src,
00349                                     const SortData< Value >* end,
00350                                     unsigned sh,
00351                                     Index* off,
00352                                     SortData< Value >* out );
00353 
00354     template < class Value >
00355     static void radix_index_pass_e( const SortData< Value >* src,
00356                                     const SortData< Value >* end,
00357                                     unsigned sh,
00358                                     Index* off,
00359                                     Index* out );
00360 
00361     template < class Value >
00362     static void radix_index_pass_be( const Value* A, Index n, Index stride, unsigned sh, Index* off, Index* out );
00363 
00364     /*------------------------------------------------------------------------------
00365 
00366 
00367       Radix Sort
00368 
00369       stable; O(n) time
00370 
00371       ----------------------------------------------------------------------------*/
00372     template < class Value >
00373     static void radix_index_sort( const Value* A, Index n, Index stride, Index* idx, SortData< Value >* work );
00374 
00375     /*------------------------------------------------------------------------------
00376 
00377 
00378       Merge Sort
00379 
00380       stable; O(n log n) time
00381 
00382       ----------------------------------------------------------------------------*/
00383     template < class Value >
00384     static void merge_index_sort( const Value* A, const Index An, Index stride, Index* idx, SortData< Value >* work );
00385 
00386     template < class Value >
00387     static void index_sort( const Value* A, Index n, Index stride, Index* idx, SortData< Value >* work );
00388 
00389 #undef DIGIT_BITS
00390 #undef DIGIT_VALUES
00391 #undef DIGIT_MASK
00392 #undef CEILDIV
00393 #undef DIGITS
00394 #undef VALUE_BITS
00395 #undef COUNT_SIZE
00396 };
00397 
00398 inline uint TupleList::get_max() const
00399 {
00400     return max;
00401 }
00402 inline uint TupleList::get_n() const
00403 {
00404     return n;
00405 }
00406 inline bool TupleList::get_writeEnabled() const
00407 {
00408     return writeEnabled;
00409 }
00410 
00411 }  // namespace moab
00412 #endif
00413 #include 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines