List.h 4.3 KB

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