List.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright (c) 2016 The ZLToolKit project authors. All Rights Reserved.
  3. *
  4. * This file is part of ZLToolKit(https://github.com/ZLMediaKit/ZLToolKit).
  5. *
  6. * Use of this source code is governed by MIT license that can be found in the
  7. * LICENSE file in the root of the source tree. All contributing project authors
  8. * may be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef ZLTOOLKIT_LIST_H
  11. #define ZLTOOLKIT_LIST_H
  12. #include <list>
  13. #include <type_traits>
  14. namespace toolkit {
  15. #if 0
  16. template<typename T>
  17. class List;
  18. template<typename T>
  19. class ListNode
  20. {
  21. public:
  22. friend class List<T>;
  23. ~ListNode(){}
  24. template <class... Args>
  25. ListNode(Args&&... args):_data(std::forward<Args>(args)...){}
  26. private:
  27. T _data;
  28. ListNode *next = nullptr;
  29. };
  30. template<typename T>
  31. class List {
  32. public:
  33. using NodeType = ListNode<T>;
  34. List(){}
  35. List(List &&that){
  36. swap(that);
  37. }
  38. ~List(){
  39. clear();
  40. }
  41. void clear(){
  42. auto ptr = _front;
  43. auto last = ptr;
  44. while(ptr){
  45. last = ptr;
  46. ptr = ptr->next;
  47. delete last;
  48. }
  49. _size = 0;
  50. _front = nullptr;
  51. _back = nullptr;
  52. }
  53. template<typename FUN>
  54. void for_each(FUN &&fun) const {
  55. auto ptr = _front;
  56. while (ptr) {
  57. fun(ptr->_data);
  58. ptr = ptr->next;
  59. }
  60. }
  61. size_t size() const{
  62. return _size;
  63. }
  64. bool empty() const{
  65. return _size == 0;
  66. }
  67. template <class... Args>
  68. void emplace_front(Args&&... args){
  69. NodeType *node = new NodeType(std::forward<Args>(args)...);
  70. if(!_front){
  71. _front = node;
  72. _back = node;
  73. _size = 1;
  74. }else{
  75. node->next = _front;
  76. _front = node;
  77. ++_size;
  78. }
  79. }
  80. template <class...Args>
  81. void emplace_back(Args&&... args){
  82. NodeType *node = new NodeType(std::forward<Args>(args)...);
  83. if(!_back){
  84. _back = node;
  85. _front = node;
  86. _size = 1;
  87. }else{
  88. _back->next = node;
  89. _back = node;
  90. ++_size;
  91. }
  92. }
  93. T &front() const{
  94. return _front->_data;
  95. }
  96. T &back() const{
  97. return _back->_data;
  98. }
  99. T &operator[](size_t pos){
  100. NodeType *front = _front ;
  101. while(pos--){
  102. front = front->next;
  103. }
  104. return front->_data;
  105. }
  106. void pop_front(){
  107. if(!_front){
  108. return;
  109. }
  110. auto ptr = _front;
  111. _front = _front->next;
  112. delete ptr;
  113. if(!_front){
  114. _back = nullptr;
  115. }
  116. --_size;
  117. }
  118. void swap(List &other){
  119. NodeType *tmp_node;
  120. tmp_node = _front;
  121. _front = other._front;
  122. other._front = tmp_node;
  123. tmp_node = _back;
  124. _back = other._back;
  125. other._back = tmp_node;
  126. size_t tmp_size = _size;
  127. _size = other._size;
  128. other._size = tmp_size;
  129. }
  130. void append(List<T> &other){
  131. if(other.empty()){
  132. return;
  133. }
  134. if(_back){
  135. _back->next = other._front;
  136. _back = other._back;
  137. }else{
  138. _front = other._front;
  139. _back = other._back;
  140. }
  141. _size += other._size;
  142. other._front = other._back = nullptr;
  143. other._size = 0;
  144. }
  145. List &operator=(const List &that) {
  146. that.for_each([&](const T &t) {
  147. emplace_back(t);
  148. });
  149. return *this;
  150. }
  151. private:
  152. NodeType *_front = nullptr;
  153. NodeType *_back = nullptr;
  154. size_t _size = 0;
  155. };
  156. #else
  157. template<typename T>
  158. class List : public std::list<T> {
  159. public:
  160. template<typename ... ARGS>
  161. List(ARGS &&...args) : std::list<T>(std::forward<ARGS>(args)...) {};
  162. ~List() = default;
  163. void append(List<T> &other) {
  164. if (other.empty()) {
  165. return;
  166. }
  167. this->insert(this->end(), other.begin(), other.end());
  168. other.clear();
  169. }
  170. template<typename FUNC>
  171. void for_each(FUNC &&func) {
  172. for (auto &t : *this) {
  173. func(t);
  174. }
  175. }
  176. template<typename FUNC>
  177. void for_each(FUNC &&func) const {
  178. for (auto &t : *this) {
  179. func(t);
  180. }
  181. }
  182. };
  183. #endif
  184. } /* namespace toolkit */
  185. #endif //ZLTOOLKIT_LIST_H