1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/include/core/SkTDStack.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,110 @@ 1.4 + 1.5 +/* 1.6 + * Copyright 2006 The Android Open Source Project 1.7 + * 1.8 + * Use of this source code is governed by a BSD-style license that can be 1.9 + * found in the LICENSE file. 1.10 + */ 1.11 + 1.12 + 1.13 +#ifndef SkTDStack_DEFINED 1.14 +#define SkTDStack_DEFINED 1.15 + 1.16 +#include "SkTypes.h" 1.17 + 1.18 +template <typename T> class SkTDStack : SkNoncopyable { 1.19 +public: 1.20 + SkTDStack() : fCount(0), fTotalCount(0) { 1.21 + fInitialRec.fNext = NULL; 1.22 + fRec = &fInitialRec; 1.23 + 1.24 + // fCount = kSlotCount; 1.25 + } 1.26 + 1.27 + ~SkTDStack() { 1.28 + Rec* rec = fRec; 1.29 + while (rec != &fInitialRec) { 1.30 + Rec* next = rec->fNext; 1.31 + sk_free(rec); 1.32 + rec = next; 1.33 + } 1.34 + } 1.35 + 1.36 + int count() const { return fTotalCount; } 1.37 + int depth() const { return fTotalCount; } 1.38 + bool empty() const { return fTotalCount == 0; } 1.39 + 1.40 + T* push() { 1.41 + SkASSERT(fCount <= kSlotCount); 1.42 + if (fCount == kSlotCount) { 1.43 + Rec* rec = (Rec*)sk_malloc_throw(sizeof(Rec)); 1.44 + rec->fNext = fRec; 1.45 + fRec = rec; 1.46 + fCount = 0; 1.47 + } 1.48 + ++fTotalCount; 1.49 + return &fRec->fSlots[fCount++]; 1.50 + } 1.51 + 1.52 + void push(const T& elem) { *this->push() = elem; } 1.53 + 1.54 + const T& index(int idx) const { 1.55 + SkASSERT(fRec && fCount > idx); 1.56 + return fRec->fSlots[fCount - idx - 1]; 1.57 + } 1.58 + 1.59 + T& index(int idx) { 1.60 + SkASSERT(fRec && fCount > idx); 1.61 + return fRec->fSlots[fCount - idx - 1]; 1.62 + } 1.63 + 1.64 + const T& top() const { 1.65 + SkASSERT(fRec && fCount > 0); 1.66 + return fRec->fSlots[fCount - 1]; 1.67 + } 1.68 + 1.69 + T& top() { 1.70 + SkASSERT(fRec && fCount > 0); 1.71 + return fRec->fSlots[fCount - 1]; 1.72 + } 1.73 + 1.74 + void pop(T* elem) { 1.75 + if (elem) { 1.76 + *elem = fRec->fSlots[fCount - 1]; 1.77 + } 1.78 + this->pop(); 1.79 + } 1.80 + 1.81 + void pop() { 1.82 + SkASSERT(fCount > 0 && fRec); 1.83 + --fTotalCount; 1.84 + if (--fCount == 0) { 1.85 + if (fRec != &fInitialRec) { 1.86 + Rec* rec = fRec->fNext; 1.87 + sk_free(fRec); 1.88 + fCount = kSlotCount; 1.89 + fRec = rec; 1.90 + } else { 1.91 + SkASSERT(fTotalCount == 0); 1.92 + } 1.93 + } 1.94 + } 1.95 + 1.96 +private: 1.97 + enum { 1.98 + kSlotCount = 8 1.99 + }; 1.100 + 1.101 + struct Rec; 1.102 + friend struct Rec; 1.103 + 1.104 + struct Rec { 1.105 + Rec* fNext; 1.106 + T fSlots[kSlotCount]; 1.107 + }; 1.108 + Rec fInitialRec; 1.109 + Rec* fRec; 1.110 + int fCount, fTotalCount; 1.111 +}; 1.112 + 1.113 +#endif