# array.hpp

Go to the documentation of this file.

    00001 /* $Id: array.hpp 10392 2007-06-29 23:45:13Z KUDr $ */
    00002
    00005 #ifndef  ARRAY_HPP
    00006 #define  ARRAY_HPP
    00007
    00008 #include "fixedsizearray.hpp"
    00009
    00012 template <class Titem_, int Tblock_size_ = 1024, int Tnum_blocks_ = Tblock_size_>
    00013 class CArrayT {
    00014 public:
    00015   typedef Titem_ Titem;
    00016   typedef CFixedSizeArrayT<Titem_, Tblock_size_> CSubArray;
    00017   typedef CFixedSizeArrayT<CSubArray, Tnum_blocks_> CSuperArray;
    00018
    00019 protected:
    00020   CSuperArray     m_a;
    00021
    00022 public:
    00023   static const int Tblock_size = Tblock_size_;
    00024   static const int Tnum_blocks = Tnum_blocks_;
    00025   static const int Tcapacity   = Tblock_size * Tnum_blocks;
    00026
    00028   FORCEINLINE CArrayT() { }
    00030   FORCEINLINE void Clear() {m_a.Clear();}
    00032   FORCEINLINE int Size() const
    00033   {
    00034     int super_size = m_a.Size();
    00035     if (super_size == 0) return 0;
    00036     int sub_size = m_a[super_size - 1].Size();
    00037     return (super_size - 1) * Tblock_size + sub_size;
    00038   }
    00040   FORCEINLINE bool IsEmpty() { return m_a.IsEmpty(); }
    00042   FORCEINLINE bool IsFull() { return m_a.IsFull() && m_a[Tnum_blocks - 1].IsFull(); }
    00044   FORCEINLINE CSubArray& FirstFreeSubArray()
    00045   {
    00046     int super_size = m_a.Size();
    00047     if (super_size > 0) {
    00048       CSubArray& sa = m_a[super_size - 1];
    00049       if (!sa.IsFull()) return sa;
    00050     }
    00051     return m_a.Add();
    00052   }
    00054   FORCEINLINE Titem_& AddNC() { return FirstFreeSubArray().AddNC(); }
    00056   FORCEINLINE Titem_& Add()   { return FirstFreeSubArray().Add(); }
    00058   FORCEINLINE Titem& operator [] (int idx)
    00059   {
    00060     CSubArray& sa = m_a[idx / Tblock_size];
    00061     Titem& item   = sa [idx % Tblock_size];
    00062     return item;
    00063   }
    00065   FORCEINLINE const Titem& operator [] (int idx) const
    00066   {
    00067     const CSubArray& sa = m_a[idx / Tblock_size];
    00068     const Titem& item   = sa [idx % Tblock_size];
    00069     return item;
    00070   }
    00071
    00072   template <typename D> void Dump(D &dmp) const
    00073   {
    00074     dmp.WriteLine("capacity = %d", Tcapacity);
    00075     int num_items = Size();
    00076     dmp.WriteLine("num_items = %d", num_items);
    00077     CStrA name;
    00078     for (int i = 0; i < num_items; i++) {
    00079       const Titem& item = (*this)[i];
    00080       name.Format("item[%d]", i);
    00081       dmp.WriteStructT(name.Data(), &item);
    00082     }
    00083   }
    00084 };
    00085
    00086 #endif /* ARRAY_HPP */