vhacdCircularList.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* Copyright (c) 2011 Khaled Mamou (kmamou at gmail dot com)
  2. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  4. 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  5. 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.
  6. 3. The names of the contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
  7. 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.
  8. */
  9. #pragma once
  10. #ifndef VHACD_CIRCULAR_LIST_H
  11. #define VHACD_CIRCULAR_LIST_H
  12. #include <stdlib.h>
  13. namespace VHACD {
  14. //! CircularListElement class.
  15. template <typename T>
  16. class CircularListElement {
  17. public:
  18. T& GetData() { return m_data; }
  19. const T& GetData() const { return m_data; }
  20. CircularListElement<T>*& GetNext() { return m_next; }
  21. CircularListElement<T>*& GetPrev() { return m_prev; }
  22. const CircularListElement<T>*& GetNext() const { return m_next; }
  23. const CircularListElement<T>*& GetPrev() const { return m_prev; }
  24. //! Constructor
  25. CircularListElement(const T& data) { m_data = data; }
  26. CircularListElement(void) {}
  27. //! Destructor
  28. ~CircularListElement(void) {}
  29. private:
  30. T m_data;
  31. CircularListElement<T>* m_next;
  32. CircularListElement<T>* m_prev;
  33. CircularListElement(const CircularListElement& rhs);
  34. };
  35. //! CircularList class.
  36. template <typename T>
  37. class CircularList {
  38. public:
  39. CircularListElement<T>*& GetHead() { return m_head; }
  40. const CircularListElement<T>* GetHead() const { return m_head; }
  41. bool IsEmpty() const { return (m_size == 0); }
  42. size_t GetSize() const { return m_size; }
  43. const T& GetData() const { return m_head->GetData(); }
  44. T& GetData() { return m_head->GetData(); }
  45. bool Delete();
  46. bool Delete(CircularListElement<T>* element);
  47. CircularListElement<T>* Add(const T* data = 0);
  48. CircularListElement<T>* Add(const T& data);
  49. bool Next();
  50. bool Prev();
  51. void Clear()
  52. {
  53. while (Delete())
  54. ;
  55. };
  56. const CircularList& operator=(const CircularList& rhs);
  57. //! Constructor
  58. CircularList()
  59. {
  60. m_head = 0;
  61. m_size = 0;
  62. }
  63. CircularList(const CircularList& rhs);
  64. //! Destructor
  65. ~CircularList(void) { Clear(); };
  66. private:
  67. CircularListElement<T>* m_head; //!< a pointer to the head of the circular list
  68. size_t m_size; //!< number of element in the circular list
  69. };
  70. }
  71. #include "vhacdCircularList.inl"
  72. #endif // VHACD_CIRCULAR_LIST_H