You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
2.3 KiB

  1. /*
  2. * This program source code file is part of KICAD, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2017 CERN
  5. * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #ifndef INTRUSIVE_LIST_H
  25. #define INTRUSIVE_LIST_H
  26. ///> A lightweight intrusive list container
  27. template <class T>
  28. class INTRUSIVE_LIST
  29. {
  30. public:
  31. INTRUSIVE_LIST<T>()
  32. {
  33. ListClear();
  34. }
  35. void ListClear()
  36. {
  37. m_prev = nullptr;
  38. m_next = nullptr;
  39. m_root = (T*) this;
  40. m_count = 1;
  41. }
  42. T* ListRemove()
  43. {
  44. if( m_prev )
  45. m_prev->m_next = m_next;
  46. if( m_next )
  47. m_next->m_prev = m_prev;
  48. m_root->m_count--;
  49. T* rv = nullptr;
  50. if( m_prev )
  51. rv = m_prev;
  52. else if( m_next )
  53. rv = m_next;
  54. m_root = nullptr;
  55. m_prev = nullptr;
  56. m_next = nullptr;
  57. return rv;
  58. }
  59. int ListSize() const
  60. {
  61. return m_root ? m_root->m_count : 0;
  62. }
  63. void ListInsert( T* item )
  64. {
  65. if( !m_root )
  66. m_root = item;
  67. if( m_next )
  68. m_next->m_prev = item;
  69. item->m_prev = (T*) this;
  70. item->m_next = m_next;
  71. item->m_root = m_root;
  72. m_root->m_count++;
  73. m_next = item;
  74. }
  75. T* ListNext() const { return m_next; };
  76. T* ListPrev() const { return m_prev; };
  77. private:
  78. int m_count;
  79. T* m_prev;
  80. T* m_next;
  81. T* m_root;
  82. };
  83. #endif /* INTRUSIVE_LIST_H */