| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield |
|---|
| 2 | * |
|---|
| 3 | * This library is open source and may be redistributed and/or modified under |
|---|
| 4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
|---|
| 5 | * (at your option) any later version. The full license is in LICENSE file |
|---|
| 6 | * included with this distribution, and on the openscenegraph.org website. |
|---|
| 7 | * |
|---|
| 8 | * This library is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * OpenSceneGraph Public License for more details. |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | #ifndef OSG_FAST_BACK_STACK |
|---|
| 15 | #define OSG_FAST_BACK_STACK 1 |
|---|
| 16 | |
|---|
| 17 | #include <vector> |
|---|
| 18 | |
|---|
| 19 | namespace osg { |
|---|
| 20 | |
|---|
| 21 | /** Simple stack implementation that keeps the back() cached locally for fast access |
|---|
| 22 | * rather than at the back of the vector which is the traditional stack implementation. |
|---|
| 23 | * A conventional std::vector<> stores the rest of the stack. Although fast_back_stack |
|---|
| 24 | * contains a stl container it only implements the back push_back(),pop_back() |
|---|
| 25 | * and back() methods so is not as general purpose as stl stack implementation. |
|---|
| 26 | * The focus of the fast_back_stack is purely to maximize the speed at which the |
|---|
| 27 | * back can be accessed.*/ |
|---|
| 28 | |
|---|
| 29 | template<class T> |
|---|
| 30 | class fast_back_stack |
|---|
| 31 | { |
|---|
| 32 | public: |
|---|
| 33 | |
|---|
| 34 | inline fast_back_stack():_value(),_stack(),_size(0) {} |
|---|
| 35 | |
|---|
| 36 | inline fast_back_stack(const fast_back_stack& fbs):_value(fbs._value),_stack(fbs._stack),_size(fbs._size) {} |
|---|
| 37 | |
|---|
| 38 | inline fast_back_stack(const T& value):_value(value),_stack(),_size(1) {} |
|---|
| 39 | |
|---|
| 40 | fast_back_stack& operator = (const fast_back_stack& fbs) |
|---|
| 41 | { |
|---|
| 42 | _value = fbs._value; |
|---|
| 43 | _stack = fbs._stack; |
|---|
| 44 | _size = fbs._size; |
|---|
| 45 | return *this; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | inline void clear() { _stack.clear(); _size = 0; } |
|---|
| 49 | |
|---|
| 50 | inline bool empty() const { return _size==0; } |
|---|
| 51 | |
|---|
| 52 | inline unsigned int size() const { return _size; } |
|---|
| 53 | |
|---|
| 54 | inline T& back() { return _value; } |
|---|
| 55 | |
|---|
| 56 | inline const T& back() const { return _value; } |
|---|
| 57 | |
|---|
| 58 | inline void push_back() |
|---|
| 59 | { |
|---|
| 60 | if (_size>0) |
|---|
| 61 | { |
|---|
| 62 | _stack.push_back(_value); |
|---|
| 63 | } |
|---|
| 64 | ++_size; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | inline void push_back(const T& value) |
|---|
| 68 | { |
|---|
| 69 | if (_size>0) |
|---|
| 70 | { |
|---|
| 71 | _stack.push_back(_value); |
|---|
| 72 | } |
|---|
| 73 | _value = value; |
|---|
| 74 | ++_size; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | inline void pop_back() |
|---|
| 78 | { |
|---|
| 79 | if (_size>0) |
|---|
| 80 | { |
|---|
| 81 | if (!_stack.empty()) |
|---|
| 82 | { |
|---|
| 83 | _value = _stack.back(); |
|---|
| 84 | _stack.pop_back(); |
|---|
| 85 | } |
|---|
| 86 | --_size; |
|---|
| 87 | } // else error condition. |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | T _value; |
|---|
| 91 | std::vector<T> _stack; |
|---|
| 92 | unsigned int _size; |
|---|
| 93 | }; |
|---|
| 94 | |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | #endif |
|---|