1 | /* Copyright (c) 2011 Khaled Mamou (kmamou at gmail dot com) |
2 | All rights reserved. |
3 | |
4 | |
5 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: |
6 | |
7 | 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. |
8 | |
9 | 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. |
10 | |
11 | 3. The names of the contributors may not be used to endorse or promote products derived from this software without specific prior written permission. |
12 | |
13 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14 | */ |
15 | #pragma once |
16 | #ifndef VHACD_CIRCULAR_LIST_H |
17 | #define VHACD_CIRCULAR_LIST_H |
18 | #include <stdlib.h> |
19 | namespace VHACD { |
20 | //! CircularListElement class. |
21 | template <typename T> |
22 | class CircularListElement { |
23 | public: |
24 | T& GetData() { return m_data; } |
25 | const T& GetData() const { return m_data; } |
26 | CircularListElement<T>*& GetNext() { return m_next; } |
27 | CircularListElement<T>*& GetPrev() { return m_prev; } |
28 | const CircularListElement<T>*& GetNext() const { return m_next; } |
29 | const CircularListElement<T>*& GetPrev() const { return m_prev; } |
30 | //! Constructor |
31 | CircularListElement(const T& data) { m_data = data; } |
32 | CircularListElement(void) {} |
33 | //! Destructor |
34 | ~CircularListElement(void) {} |
35 | private: |
36 | T m_data; |
37 | CircularListElement<T>* m_next; |
38 | CircularListElement<T>* m_prev; |
39 | |
40 | CircularListElement(const CircularListElement& rhs); |
41 | }; |
42 | //! CircularList class. |
43 | template <typename T> |
44 | class CircularList { |
45 | public: |
46 | CircularListElement<T>*& GetHead() { return m_head; } |
47 | const CircularListElement<T>* GetHead() const { return m_head; } |
48 | bool IsEmpty() const { return (m_size == 0); } |
49 | size_t GetSize() const { return m_size; } |
50 | const T& GetData() const { return m_head->GetData(); } |
51 | T& GetData() { return m_head->GetData(); } |
52 | bool Delete(); |
53 | bool Delete(CircularListElement<T>* element); |
54 | CircularListElement<T>* Add(const T* data = 0); |
55 | CircularListElement<T>* Add(const T& data); |
56 | bool Next(); |
57 | bool Prev(); |
58 | void Clear() |
59 | { |
60 | while (Delete()) |
61 | ; |
62 | }; |
63 | const CircularList& operator=(const CircularList& rhs); |
64 | //! Constructor |
65 | CircularList() |
66 | { |
67 | m_head = 0; |
68 | m_size = 0; |
69 | } |
70 | CircularList(const CircularList& rhs); |
71 | //! Destructor |
72 | ~CircularList(void) { Clear(); }; |
73 | private: |
74 | CircularListElement<T>* m_head; //!< a pointer to the head of the circular list |
75 | size_t m_size; //!< number of element in the circular list |
76 | }; |
77 | } |
78 | #include "vhacdCircularList.inl" |
79 | #endif // VHACD_CIRCULAR_LIST_H |