(svn r18822) -Codechange: Rename YAPF-related container classes and their members to better fit other container classes. (skidd13)

This commit is contained in:
frosch 2010-01-16 13:52:24 +00:00
parent 21fff6407d
commit c7dafb9a26
4 changed files with 69 additions and 69 deletions

View File

@ -17,70 +17,70 @@
/** Flexible array with size limit. Implemented as fixed size /** Flexible array with size limit. Implemented as fixed size
* array of fixed size arrays */ * array of fixed size arrays */
template <class Titem_, int Tblock_size_ = 1024, int Tnum_blocks_ = Tblock_size_> template <class T, int B = 1024, int N = B>
class CArrayT { class SmallArray {
public: public:
typedef Titem_ Titem; ///< Titem is now visible from outside typedef T Titem; ///< Titem is now visible from outside
typedef CFixedSizeArrayT<Titem_, Tblock_size_> CSubArray; ///< inner array typedef FixedSizeArray<T, B> SubArray; ///< inner array
typedef CFixedSizeArrayT<CSubArray, Tnum_blocks_> CSuperArray; ///< outer array typedef FixedSizeArray<SubArray, N> SuperArray; ///< outer array
protected: protected:
CSuperArray m_a; ///< array of arrays of items SuperArray data; ///< array of arrays of items
public: public:
static const int Tblock_size = Tblock_size_; ///< block size is now visible from outside static const int Tblock_size = B; ///< block size is now visible from outside
static const int Tnum_blocks = Tnum_blocks_; ///< number of blocks is now visible from outside static const int Tnum_blocks = N; ///< number of blocks is now visible from outside
static const int Tcapacity = Tblock_size * Tnum_blocks; ///< total max number of items static const int Tcapacity = B * N; ///< total max number of items
/** implicit constructor */ /** implicit constructor */
FORCEINLINE CArrayT() { } FORCEINLINE SmallArray() { }
/** Clear (destroy) all items */ /** Clear (destroy) all items */
FORCEINLINE void Clear() {m_a.Clear();} FORCEINLINE void Clear() {data.Clear();}
/** Return actual number of items */ /** Return actual number of items */
FORCEINLINE int Size() const FORCEINLINE int Length() const
{ {
int super_size = m_a.Size(); int super_size = data.Length();
if (super_size == 0) return 0; if (super_size == 0) return 0;
int sub_size = m_a[super_size - 1].Size(); int sub_size = data[super_size - 1].Length();
return (super_size - 1) * Tblock_size + sub_size; return (super_size - 1) * Tblock_size + sub_size;
} }
/** return true if array is empty */ /** return true if array is empty */
FORCEINLINE bool IsEmpty() { return m_a.IsEmpty(); } FORCEINLINE bool IsEmpty() { return data.IsEmpty(); }
/** return true if array is full */ /** return true if array is full */
FORCEINLINE bool IsFull() { return m_a.IsFull() && m_a[Tnum_blocks - 1].IsFull(); } FORCEINLINE bool IsFull() { return data.IsFull() && data[Tnum_blocks - 1].IsFull(); }
/** return first sub-array with free space for new item */ /** return first sub-array with free space for new item */
FORCEINLINE CSubArray& FirstFreeSubArray() FORCEINLINE SubArray& FirstFreeSubArray()
{ {
int super_size = m_a.Size(); int super_size = data.Length();
if (super_size > 0) { if (super_size > 0) {
CSubArray& sa = m_a[super_size - 1]; SubArray& s = data[super_size - 1];
if (!sa.IsFull()) return sa; if (!s.IsFull()) return s;
} }
return m_a.Add(); return data.AppendC();
} }
/** allocate but not construct new item */ /** allocate but not construct new item */
FORCEINLINE Titem_& AddNC() { return FirstFreeSubArray().AddNC(); } FORCEINLINE T& Append() { return FirstFreeSubArray().Append(); }
/** allocate and construct new item */ /** allocate and construct new item */
FORCEINLINE Titem_& Add() { return FirstFreeSubArray().Add(); } FORCEINLINE T& AppendC() { return FirstFreeSubArray().AppendC(); }
/** indexed access (non-const) */ /** indexed access (non-const) */
FORCEINLINE Titem& operator [] (int idx) FORCEINLINE Titem& operator [] (int index)
{ {
CSubArray& sa = m_a[idx / Tblock_size]; SubArray& s = data[index / Tblock_size];
Titem& item = sa [idx % Tblock_size]; Titem& item = s[index % Tblock_size];
return item; return item;
} }
/** indexed access (const) */ /** indexed access (const) */
FORCEINLINE const Titem& operator [] (int idx) const FORCEINLINE const Titem& operator [] (int index) const
{ {
const CSubArray& sa = m_a[idx / Tblock_size]; const SubArray& s = data[index / Tblock_size];
const Titem& item = sa [idx % Tblock_size]; const Titem& item = s[index % Tblock_size];
return item; return item;
} }
template <typename D> void Dump(D &dmp) const template <typename D> void Dump(D &dmp) const
{ {
dmp.WriteLine("capacity = %d", Tcapacity); dmp.WriteLine("capacity = %d", Tcapacity);
int num_items = Size(); int num_items = Length();
dmp.WriteLine("num_items = %d", num_items); dmp.WriteLine("num_items = %d", num_items);
CStrA name; CStrA name;
for (int i = 0; i < num_items; i++) { for (int i = 0; i < num_items; i++) {

View File

@ -18,61 +18,61 @@
* Upon construction it preallocates fixed size block of memory * Upon construction it preallocates fixed size block of memory
* for all items, but doesn't construct them. Item's construction * for all items, but doesn't construct them. Item's construction
* is delayed. */ * is delayed. */
template <class Titem_, int Tcapacity_> template <class T, int C>
struct CFixedSizeArrayT { struct FixedSizeArray {
/** the only member of fixed size array is pointer to the block /** the only member of fixed size array is pointer to the block
* of C array of items. Header can be found on the offset -sizeof(CHdr). */ * of C array of items. Header can be found on the offset -sizeof(ArrayHeader). */
Titem_ *m_items; T *data;
/** header for fixed size array */ /** header for fixed size array */
struct CHdr struct ArrayHeader
{ {
int m_num_items; ///< number of items in the array int items; ///< number of items in the array
int m_ref_cnt; ///< block reference counter (used by copy constructor and by destructor) int reference_count; ///< block reference counter (used by copy constructor and by destructor)
}; };
/* make types and constants visible from outside */ /* make types and constants visible from outside */
typedef Titem_ Titem; // type of array item typedef T Titem; // type of array item
static const int Tcapacity = Tcapacity_; // the array capacity (maximum size) static const int Tcapacity = C; // the array capacity (maximum size)
static const int TitemSize = sizeof(Titem_); // size of item static const int Tsize = sizeof(T); // size of item
static const int ThdrSize = sizeof(CHdr); // size of header static const int HeaderSize = sizeof(ArrayHeader); // size of header
/** Default constructor. Preallocate space for items and header, then initialize header. */ /** Default constructor. Preallocate space for items and header, then initialize header. */
CFixedSizeArrayT() FixedSizeArray()
{ {
/* allocate block for header + items (don't construct items) */ /* allocate block for header + items (don't construct items) */
m_items = (Titem*)((MallocT<int8>(ThdrSize + Tcapacity * sizeof(Titem))) + ThdrSize); data = (Titem*)((MallocT<int8>(HeaderSize + Tcapacity * Tsize)) + HeaderSize);
SizeRef() = 0; // initial number of items SizeRef() = 0; // initial number of items
RefCnt() = 1; // initial reference counter RefCnt() = 1; // initial reference counter
} }
/** Copy constructor. Preallocate space for items and header, then initialize header. */ /** Copy constructor. Preallocate space for items and header, then initialize header. */
CFixedSizeArrayT(const CFixedSizeArrayT<Titem_, Tcapacity_>& src) FixedSizeArray(const FixedSizeArray<T, C>& src)
{ {
/* share block (header + items) with the source array */ /* share block (header + items) with the source array */
m_items = src.m_items; data = src.data;
RefCnt()++; // now we share block with the source RefCnt()++; // now we share block with the source
} }
/** destroy remaining items and free the memory block */ /** destroy remaining items and free the memory block */
~CFixedSizeArrayT() ~FixedSizeArray()
{ {
/* release one reference to the shared block */ /* release one reference to the shared block */
if ((--RefCnt()) > 0) return; // and return if there is still some owner if ((--RefCnt()) > 0) return; // and return if there is still some owner
Clear(); Clear();
/* free the memory block occupied by items */ /* free the memory block occupied by items */
free(((int8*)m_items) - ThdrSize); free(((int8*)data) - HeaderSize);
m_items = NULL; data = NULL;
} }
/** Clear (destroy) all items */ /** Clear (destroy) all items */
FORCEINLINE void Clear() FORCEINLINE void Clear()
{ {
/* walk through all allocated items backward and destroy them */ /* walk through all allocated items backward and destroy them */
for (Titem *pItem = &m_items[Size() - 1]; pItem >= m_items; pItem--) { for (Titem *pItem = &data[Length() - 1]; pItem >= data; pItem--) {
pItem->~Titem_(); pItem->~T();
} }
/* number of items become zero */ /* number of items become zero */
SizeRef() = 0; SizeRef() = 0;
@ -80,30 +80,30 @@ struct CFixedSizeArrayT {
protected: protected:
/** return reference to the array header (non-const) */ /** return reference to the array header (non-const) */
FORCEINLINE CHdr& Hdr() { return *(CHdr*)(((int8*)m_items) - ThdrSize); } FORCEINLINE ArrayHeader& Hdr() { return *(ArrayHeader*)(((int8*)data) - HeaderSize); }
/** return reference to the array header (const) */ /** return reference to the array header (const) */
FORCEINLINE const CHdr& Hdr() const { return *(CHdr*)(((int8*)m_items) - ThdrSize); } FORCEINLINE const ArrayHeader& Hdr() const { return *(ArrayHeader*)(((int8*)data) - HeaderSize); }
/** return reference to the block reference counter */ /** return reference to the block reference counter */
FORCEINLINE int& RefCnt() { return Hdr().m_ref_cnt; } FORCEINLINE int& RefCnt() { return Hdr().reference_count; }
/** return reference to number of used items */ /** return reference to number of used items */
FORCEINLINE int& SizeRef() { return Hdr().m_num_items; } FORCEINLINE int& SizeRef() { return Hdr().items; }
public: public:
/** return number of used items */ /** return number of used items */
FORCEINLINE int Size() const { return Hdr().m_num_items; } FORCEINLINE int Length() const { return Hdr().items; }
/** return true if array is full */ /** return true if array is full */
FORCEINLINE bool IsFull() const { return Size() >= Tcapacity; }; FORCEINLINE bool IsFull() const { return Length() >= Tcapacity; };
/** return true if array is empty */ /** return true if array is empty */
FORCEINLINE bool IsEmpty() const { return Size() <= 0; }; FORCEINLINE bool IsEmpty() const { return Length() <= 0; };
/** index validation */ /** index validation */
FORCEINLINE void CheckIdx(int idx) const { assert(idx >= 0); assert(idx < Size()); } FORCEINLINE void CheckIdx(int index) const { assert(index >= 0); assert(index < Length()); }
/** add (allocate), but don't construct item */ /** add (allocate), but don't construct item */
FORCEINLINE Titem& AddNC() { assert(!IsFull()); return m_items[SizeRef()++]; } FORCEINLINE Titem& Append() { assert(!IsFull()); return data[SizeRef()++]; }
/** add and construct item using default constructor */ /** add and construct item using default constructor */
FORCEINLINE Titem& Add() { Titem& item = AddNC(); new(&item)Titem; return item; } FORCEINLINE Titem& AppendC() { Titem& item = Append(); new(&item)Titem; return item; }
/** return item by index (non-const version) */ /** return item by index (non-const version) */
FORCEINLINE Titem& operator [] (int idx) { CheckIdx(idx); return m_items[idx]; } FORCEINLINE Titem& operator [] (int index) { CheckIdx(index); return data[index]; }
/** return item by index (const version) */ /** return item by index (const version) */
FORCEINLINE const Titem& operator [] (int idx) const { CheckIdx(idx); return m_items[idx]; } FORCEINLINE const Titem& operator [] (int index) const { CheckIdx(index); return data[index]; }
}; };
#endif /* FIXEDSIZEARRAY_HPP */ #endif /* FIXEDSIZEARRAY_HPP */

View File

@ -27,7 +27,7 @@ public:
/** make Titem_::Key a property of HashTable */ /** make Titem_::Key a property of HashTable */
typedef typename Titem_::Key Key; typedef typename Titem_::Key Key;
/** type that we will use as item container */ /** type that we will use as item container */
typedef CArrayT<Titem_, 65536, 256> CItemArray; typedef SmallArray<Titem_, 65536, 256> CItemArray;
/** how pointers to open nodes will be stored */ /** how pointers to open nodes will be stored */
typedef CHashTableT<Titem_, Thash_bits_open_ > COpenList; typedef CHashTableT<Titem_, Thash_bits_open_ > COpenList;
/** how pointers to closed nodes will be stored */ /** how pointers to closed nodes will be stored */
@ -74,7 +74,7 @@ public:
/** allocate new data item from m_arr */ /** allocate new data item from m_arr */
FORCEINLINE Titem_ *CreateNewNode() FORCEINLINE Titem_ *CreateNewNode()
{ {
if (m_new_node == NULL) m_new_node = &m_arr.Add(); if (m_new_node == NULL) m_new_node = &m_arr.AppendC();
return m_new_node; return m_new_node;
} }
@ -152,7 +152,7 @@ public:
return item; return item;
} }
FORCEINLINE int TotalCount() {return m_arr.Size();} FORCEINLINE int TotalCount() {return m_arr.Length();}
FORCEINLINE Titem_& ItemAt(int idx) {return m_arr[idx];} FORCEINLINE Titem_& ItemAt(int idx) {return m_arr[idx];}
template <class D> void Dump(D &dmp) const template <class D> void Dump(D &dmp) const

View File

@ -53,7 +53,7 @@ public:
typedef typename Node::Key Key; ///< key to hash tables typedef typename Node::Key Key; ///< key to hash tables
typedef typename Node::CachedData CachedData; typedef typename Node::CachedData CachedData;
typedef typename CachedData::Key CacheKey; typedef typename CachedData::Key CacheKey;
typedef CArrayT<CachedData> LocalCache; typedef SmallArray<CachedData> LocalCache;
protected: protected:
LocalCache m_local_cache; LocalCache m_local_cache;
@ -70,7 +70,7 @@ public:
FORCEINLINE bool PfNodeCacheFetch(Node& n) FORCEINLINE bool PfNodeCacheFetch(Node& n)
{ {
CacheKey key(n.GetKey()); CacheKey key(n.GetKey());
Yapf().ConnectNodeToCachedData(n, *new (&m_local_cache.AddNC()) CachedData(key)); Yapf().ConnectNodeToCachedData(n, *new (&m_local_cache.Append()) CachedData(key));
return false; return false;
} }
@ -113,7 +113,7 @@ struct CSegmentCostCacheT
enum {c_hash_bits = 14}; enum {c_hash_bits = 14};
typedef CHashTableT<Tsegment, c_hash_bits> HashTable; typedef CHashTableT<Tsegment, c_hash_bits> HashTable;
typedef CArrayT<Tsegment> Heap; typedef SmallArray<Tsegment> Heap;
typedef typename Tsegment::Key Key; ///< key to hash table typedef typename Tsegment::Key Key; ///< key to hash table
HashTable m_map; HashTable m_map;
@ -133,7 +133,7 @@ struct CSegmentCostCacheT
Tsegment *item = m_map.Find(key); Tsegment *item = m_map.Find(key);
if (item == NULL) { if (item == NULL) {
*found = false; *found = false;
item = new (&m_heap.AddNC()) Tsegment(key); item = new (&m_heap.Append()) Tsegment(key);
m_map.Push(*item); m_map.Push(*item);
} else { } else {
*found = true; *found = true;