Stxxl  1.4.0
include/stxxl/bits/mng/bid.h
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  include/stxxl/bits/mng/bid.h
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2002-2004 Roman Dementiev <dementiev@mpi-sb.mpg.de>
00007  *  Copyright (C) 2009, 2010 Andreas Beckmann <beckmann@cs.uni-frankfurt.de>
00008  *  Copyright (C) 2009 Johannes Singler <singler@ira.uka.de>
00009  *
00010  *  Distributed under the Boost Software License, Version 1.0.
00011  *  (See accompanying file LICENSE_1_0.txt or copy at
00012  *  http://www.boost.org/LICENSE_1_0.txt)
00013  **************************************************************************/
00014 
00015 #ifndef STXXL_BID_HEADER
00016 #define STXXL_BID_HEADER
00017 
00018 #include <cstring>
00019 #include <ostream>
00020 #include <iomanip>
00021 
00022 #include <stxxl/bits/io/file.h>
00023 #include <stxxl/bits/common/utils.h>
00024 
00025 #ifndef STXXL_VERBOSE_BLOCK_LIFE_CYCLE
00026 #define STXXL_VERBOSE_BLOCK_LIFE_CYCLE STXXL_VERBOSE2
00027 #endif
00028 #define FMT_BID(_bid_) "[" << (_bid_).storage->get_allocator_id() << "]0x" << std::hex << std::setfill('0') << std::setw(8) << (_bid_).offset << "/0x" << std::setw(8) << (_bid_).size
00029 
00030 
00031 __STXXL_BEGIN_NAMESPACE
00032 
00033 //! \ingroup mnglayer
00034 //! \{
00035 
00036 //! \brief Block identifier class
00037 
00038 //! Stores block identity, given by file and offset within the file
00039 template <unsigned SIZE>
00040 struct BID
00041 {
00042     enum
00043     {
00044         size = SIZE,         //!< Block size
00045         t_size = SIZE        //!< Blocks size, given by the parameter
00046     };
00047 
00048     file * storage;          //!< pointer to the file of the block
00049     stxxl::int64 offset;     //!< offset within the file of the block
00050 
00051     BID() : storage(NULL), offset(0)
00052     { }
00053 
00054     bool valid() const
00055     {
00056         return storage != NULL;
00057     }
00058 
00059     BID(file * s, stxxl::int64 o) : storage(s), offset(o)
00060     { }
00061 
00062     BID(const BID & obj) : storage(obj.storage), offset(obj.offset)
00063     { }
00064 
00065     template <unsigned BlockSize>
00066     explicit BID(const BID<BlockSize> & obj) : storage(obj.storage), offset(obj.offset)
00067     { }
00068 
00069     template <unsigned BlockSize>
00070     BID & operator = (const BID<BlockSize> & obj)
00071     {
00072         storage = obj.storage;
00073         offset = obj.offset;
00074         return *this;
00075     }
00076 
00077     bool is_managed() const
00078     {
00079         return storage->get_allocator_id() != file::NO_ALLOCATOR;
00080     }
00081 };
00082 
00083 
00084 //! \brief Specialization of block identifier class (BID) for variable size block size
00085 
00086 //! Stores block identity, given by file, offset within the file, and size of the block
00087 template <>
00088 struct BID<0>
00089 {
00090     file * storage;          //!< pointer to the file of the block
00091     stxxl::int64 offset;     //!< offset within the file of the block
00092     unsigned size;           //!< size of the block in bytes
00093 
00094     enum
00095     {
00096         t_size = 0           //!< Blocks size, given by the parameter
00097     };
00098 
00099     BID() : storage(NULL), offset(0), size(0)
00100     { }
00101 
00102     BID(file * f, stxxl::int64 o, unsigned s) : storage(f), offset(o), size(s)
00103     { }
00104 
00105     bool valid() const
00106     {
00107         return storage;
00108     }
00109 };
00110 
00111 template <unsigned blk_sz>
00112 bool operator == (const BID<blk_sz> & a, const BID<blk_sz> & b)
00113 {
00114     return (a.storage == b.storage) && (a.offset == b.offset) && (a.size == b.size);
00115 }
00116 
00117 template <unsigned blk_sz>
00118 bool operator != (const BID<blk_sz> & a, const BID<blk_sz> & b)
00119 {
00120     return (a.storage != b.storage) || (a.offset != b.offset) || (a.size != b.size);
00121 }
00122 
00123 template <unsigned blk_sz>
00124 std::ostream & operator << (std::ostream & s, const BID<blk_sz> & bid)
00125 {
00126     // [0x12345678|0]0x00100000/0x00010000
00127     // [file ptr|file id]offset/size
00128 
00129     s << "[" << bid.storage << "|";
00130     if (bid.storage)
00131         s << bid.storage->get_allocator_id();
00132     else
00133         s << "?";
00134     s << "]0x" << std::hex << std::setfill('0') << std::setw(8) << bid.offset << "/0x" << std::setw(8) << bid.size << std::dec;
00135     return s;
00136 }
00137 
00138 
00139 #if 0
00140 template <unsigned BLK_SIZE>
00141 class BIDArray : public std::vector<BID<BLK_SIZE> >
00142 {
00143 public:
00144     BIDArray(std::vector<BID<BLK_SIZE> >::size_type size = 0) : std::vector<BID<BLK_SIZE> >(size) { }
00145 };
00146 #endif
00147 
00148 template <unsigned BLK_SIZE>
00149 class BIDArray : private noncopyable
00150 {
00151 protected:
00152     unsigned_type _size;
00153     BID<BLK_SIZE> * array;
00154 
00155 public:
00156     typedef BID<BLK_SIZE> & reference;
00157     typedef BID<BLK_SIZE> * iterator;
00158     typedef const BID<BLK_SIZE> * const_iterator;
00159 
00160     BIDArray() : _size(0), array(NULL)
00161     { }
00162 
00163     iterator begin()
00164     {
00165         return array;
00166     }
00167 
00168     iterator end()
00169     {
00170         return array + _size;
00171     }
00172 
00173     BIDArray(unsigned_type size) : _size(size)
00174     {
00175         array = new BID<BLK_SIZE>[size];
00176     }
00177 
00178     unsigned_type size() const
00179     {
00180         return _size;
00181     }
00182 
00183     reference operator [] (int_type i)
00184     {
00185         return array[i];
00186     }
00187 
00188     void resize(unsigned_type newsize)
00189     {
00190         if (array)
00191         {
00192             STXXL_MSG("Warning: resizing nonempty BIDArray");
00193             BID<BLK_SIZE> * tmp = array;
00194             array = new BID<BLK_SIZE>[newsize];
00195             memcpy((void *)array, (void *)tmp,
00196                    sizeof(BID<BLK_SIZE>) * (STXXL_MIN(_size, newsize)));
00197             delete[] tmp;
00198             _size = newsize;
00199         }
00200         else
00201         {
00202             array = new BID<BLK_SIZE>[newsize];
00203             _size = newsize;
00204         }
00205     }
00206 
00207     ~BIDArray()
00208     {
00209         if (array)
00210             delete[] array;
00211     }
00212 };
00213 
00214 //! \}
00215 
00216 __STXXL_END_NAMESPACE
00217 
00218 #endif // !STXXL_BID_HEADER
00219 // vim: et:ts=4:sw=4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines