当前位置:   article > 正文

《STL源码剖析》-- stl_list.h_list.h stl

list.h stl
  1. // Filename: stl_list.h
  2. // Comment By: 凝霜
  3. // E-mail: mdl2009@vip.qq.com
  4. // Blog: http://blog.csdn.net/mdl13412
  5. /*
  6. *
  7. * Copyright (c) 1994
  8. * Hewlett-Packard Company
  9. *
  10. * Permission to use, copy, modify, distribute and sell this software
  11. * and its documentation for any purpose is hereby granted without fee,
  12. * provided that the above copyright notice appear in all copies and
  13. * that both that copyright notice and this permission notice appear
  14. * in supporting documentation. Hewlett-Packard Company makes no
  15. * representations about the suitability of this software for any
  16. * purpose. It is provided "as is" without express or implied warranty.
  17. *
  18. *
  19. * Copyright (c) 1996,1997
  20. * Silicon Graphics Computer Systems, Inc.
  21. *
  22. * Permission to use, copy, modify, distribute and sell this software
  23. * and its documentation for any purpose is hereby granted without fee,
  24. * provided that the above copyright notice appear in all copies and
  25. * that both that copyright notice and this permission notice appear
  26. * in supporting documentation. Silicon Graphics makes no
  27. * representations about the suitability of this software for any
  28. * purpose. It is provided "as is" without express or implied warranty.
  29. */
  30. /* NOTE: This is an internal header file, included by other STL headers.
  31. * You should not attempt to use it directly.
  32. */
  33. #ifndef __SGI_STL_INTERNAL_LIST_H
  34. #define __SGI_STL_INTERNAL_LIST_H
  35. __STL_BEGIN_NAMESPACE
  36. #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
  37. #pragma set woff 1174
  38. #endif
  39. // list结点, 提供双向访问能力
  40. // -------- -------- -------- --------
  41. // | next |---------->| next |---------->| next |---------->| next |
  42. // -------- -------- -------- --------
  43. // | prev |<----------| prev |<----------| prev |<----------| prev |
  44. // -------- -------- -------- --------
  45. // | data | | data | | data | | data |
  46. // -------- -------- -------- --------
  47. template <class T>
  48. struct __list_node
  49. {
  50. typedef void* void_pointer;
  51. void_pointer next;
  52. void_pointer prev;
  53. T data;
  54. };
  55. // 至于为什么不使用默认参数, 这个是因为有一些编译器不能提供推导能力,
  56. // 而作者又不想维护两份代码, 故不使用默认参数
  57. template<class T, class Ref, class Ptr>
  58. struct __list_iterator
  59. {
  60. // 标记为'STL标准强制要求'的typedefs用于提供iterator_traits<I>支持
  61. typedef __list_iterator<T, T&, T*> iterator; // STL标准强制要求
  62. typedef __list_iterator<T, const T&, const T*> const_iterator;
  63. typedef __list_iterator<T, Ref, Ptr> self;
  64. typedef bidirectional_iterator_tag iterator_category;
  65. typedef T value_type; // STL标准强制要求
  66. typedef Ptr pointer; // STL标准强制要求
  67. typedef Ref reference; // STL标准强制要求
  68. typedef __list_node<T>* link_type;
  69. typedef size_t size_type;
  70. typedef ptrdiff_t difference_type; // STL标准强制要求
  71. // 这个是迭代器实际管理的资源指针
  72. link_type node;
  73. __list_iterator(link_type x) : node(x) {}
  74. __list_iterator() {}
  75. __list_iterator(const iterator& x) : node(x.node) {}
  76. // 在STL算法中需要迭代器提供支持
  77. bool operator==(const self& x) const { return node == x.node; }
  78. bool operator!=(const self& x) const { return node != x.node; }
  79. // 重载operator *, 返回实际维护的数据
  80. reference operator*() const { return (*node).data; }
  81. #ifndef __SGI_STL_NO_ARROW_OPERATOR
  82. // 如果支持'->'则重载之
  83. // 解释一下为什么要返回地址
  84. // class A
  85. // {
  86. // public:
  87. // // ...
  88. // void fun();
  89. // // ...
  90. // }
  91. // __list_iterator<A, A&, A*> iter(new A)
  92. // iter->fun();
  93. // 这就相当于调用(iter.operator())->fun();
  94. // 经过重载使其行为和原生指针一致
  95. pointer operator->() const { return &(operator*()); }
  96. #endif /* __SGI_STL_NO_ARROW_OPERATOR */
  97. // 前缀自加
  98. self& operator++()
  99. {
  100. node = (link_type)((*node).next);
  101. return *this;
  102. }
  103. // 后缀自加, 需要先产生自身的一个副本, 然会再对自身操作, 最后返回副本
  104. self operator++(int)
  105. {
  106. self tmp = *this;
  107. ++*this;
  108. return tmp;
  109. }
  110. self& operator--()
  111. {
  112. node = (link_type)((*node).prev);
  113. return *this;
  114. }
  115. self operator--(int)
  116. {
  117. self tmp = *this;
  118. --*this;
  119. return tmp;
  120. }
  121. };
  122. // 如果编译器支持模板类偏特化那么就不需要提供以下traits函数
  123. // 直接使用<stl_iterator.h>中的
  124. // template <class Iterator>
  125. // struct iterator_traits
  126. #ifndef __STL_CLASS_PARTIAL_SPECIALIZATION
  127. template <class T, class Ref, class Ptr>
  128. inline bidirectional_iterator_tag
  129. iterator_category(const __list_iterator<T, Ref, Ptr>&) {
  130. return bidirectional_iterator_tag();
  131. }
  132. template <class T, class Ref, class Ptr>
  133. inline T*
  134. value_type(const __list_iterator<T, Ref, Ptr>&) {
  135. return 0;
  136. }
  137. template <class T, class Ref, class Ptr>
  138. inline ptrdiff_t*
  139. distance_type(const __list_iterator<T, Ref, Ptr>&) {
  140. return 0;
  141. }
  142. #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
  143. // 链表本身成环, 且是双向链表, 这样计算begin()和end()是常数时间
  144. // end() 头结点 begin()
  145. // ↓ ↓ ↓
  146. // -------- -------- -------- --------
  147. // ---->| next |---------->| next |---------->| next |---------->| next |------
  148. // | -------- -------- -------- -------- |
  149. // | --| prev |<----------| prev |<----------| prev |<----------| prev |<--| |
  150. // | | -------- -------- -------- -------- | |
  151. // | | | data | | data | | data | | data | | |
  152. // | | -------- -------- -------- -------- | |
  153. // | | | |
  154. // | | -------- -------- -------- -------- | |
  155. // ---|-| next |<----------| next |<----------| next |<----------| next |<--|--
  156. // | -------- -------- -------- -------- |
  157. // ->| prev |---------->| prev |---------->| prev |---------->| prev |----
  158. // -------- -------- -------- --------
  159. // | data | | data | | data | | data |
  160. // -------- -------- -------- --------
  161. // 默认allocator为alloc, 其具体使用版本请参照<stl_alloc.h>
  162. template <class T, class Alloc = alloc>
  163. class list
  164. {
  165. protected:
  166. typedef void* void_pointer;
  167. typedef __list_node<T> list_node;
  168. // 这个提供STL标准的allocator接口
  169. typedef simple_alloc<list_node, Alloc> list_node_allocator;
  170. public:
  171. typedef T value_type;
  172. typedef value_type* pointer;
  173. typedef const value_type* const_pointer;
  174. typedef value_type& reference;
  175. typedef const value_type& const_reference;
  176. typedef list_node* link_type;
  177. typedef size_t size_type;
  178. typedef ptrdiff_t difference_type;
  179. public:
  180. typedef __list_iterator<T, T&, T*> iterator;
  181. typedef __list_iterator<T, const T&, const T*> const_iterator;
  182. #ifdef __STL_CLASS_PARTIAL_SPECIALIZATION
  183. typedef reverse_iterator<const_iterator> const_reverse_iterator;
  184. typedef reverse_iterator<iterator> reverse_iterator;
  185. #else /* __STL_CLASS_PARTIAL_SPECIALIZATION */
  186. typedef reverse_bidirectional_iterator<const_iterator, value_type,
  187. const_reference, difference_type>
  188. const_reverse_iterator;
  189. typedef reverse_bidirectional_iterator<iterator, value_type, reference,
  190. difference_type>
  191. reverse_iterator;
  192. #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
  193. protected:
  194. // 分配一个新结点, 注意这里并不进行构造,
  195. // 构造交给全局的construct, 见<stl_stl_uninitialized.h>
  196. link_type get_node() { return list_node_allocator::allocate(); }
  197. // 释放指定结点, 不进行析构, 析构交给全局的destroy,
  198. // 见<stl_stl_uninitialized.h>
  199. void put_node(link_type p) { list_node_allocator::deallocate(p); }
  200. // 创建结点, 首先分配内存, 然后进行构造
  201. // 注: commit or rollback
  202. link_type create_node(const T& x)
  203. {
  204. link_type p = get_node();
  205. __STL_TRY {
  206. construct(&p->data, x);
  207. }
  208. __STL_UNWIND(put_node(p));
  209. return p;
  210. }
  211. // 析构结点元素, 并释放内存
  212. void destroy_node(link_type p)
  213. {
  214. destroy(&p->data);
  215. put_node(p);
  216. }
  217. protected:
  218. // 用于空链表的建立
  219. void empty_initialize()
  220. {
  221. node = get_node();
  222. node->next = node;
  223. node->prev = node;
  224. }
  225. // 创建值为value共n个结点的链表
  226. // 注: commit or rollback
  227. void fill_initialize(size_type n, const T& value)
  228. {
  229. empty_initialize();
  230. __STL_TRY {
  231. // 此处插入操作时间复杂度O(1)
  232. insert(begin(), n, value);
  233. }
  234. __STL_UNWIND(clear(); put_node(node));
  235. }
  236. // 以一个区间初始化链表
  237. // 注: commit or rollback
  238. #ifdef __STL_MEMBER_TEMPLATES
  239. template <class InputIterator>
  240. void range_initialize(InputIterator first, InputIterator last)
  241. {
  242. empty_initialize();
  243. __STL_TRY {
  244. insert(begin(), first, last);
  245. }
  246. __STL_UNWIND(clear(); put_node(node));
  247. }
  248. #else /* __STL_MEMBER_TEMPLATES */
  249. void range_initialize(const T* first, const T* last) {
  250. empty_initialize();
  251. __STL_TRY {
  252. insert(begin(), first, last);
  253. }
  254. __STL_UNWIND(clear(); put_node(node));
  255. }
  256. void range_initialize(const_iterator first, const_iterator last) {
  257. empty_initialize();
  258. __STL_TRY {
  259. insert(begin(), first, last);
  260. }
  261. __STL_UNWIND(clear(); put_node(node));
  262. }
  263. #endif /* __STL_MEMBER_TEMPLATES */
  264. protected:
  265. // 好吧, 这个是链表头结点, 其本身不保存数据
  266. link_type node;
  267. public:
  268. list() { empty_initialize(); }
  269. iterator begin() { return (link_type)((*node).next); }
  270. const_iterator begin() const { return (link_type)((*node).next); }
  271. // 链表成环, 当指所以头节点也就是end
  272. iterator end() { return node; }
  273. const_iterator end() const { return node; }
  274. reverse_iterator rbegin() { return reverse_iterator(end()); }
  275. const_reverse_iterator rbegin() const {
  276. return const_reverse_iterator(end());
  277. }
  278. reverse_iterator rend() { return reverse_iterator(begin()); }
  279. const_reverse_iterator rend() const {
  280. return const_reverse_iterator(begin());
  281. }
  282. // 头结点指向自身说明链表中无元素
  283. bool empty() const { return node->next == node; }
  284. // 使用全局函数distance()进行计算, 时间复杂度O(n)
  285. size_type size() const
  286. {
  287. size_type result = 0;
  288. distance(begin(), end(), result);
  289. return result;
  290. }
  291. size_type max_size() const { return size_type(-1); }
  292. reference front() { return *begin(); }
  293. const_reference front() const { return *begin(); }
  294. reference back() { return *(--end()); }
  295. const_reference back() const { return *(--end()); }
  296. void swap(list<T, Alloc>& x) { __STD::swap(node, x.node); }
  297. // 在指定位置插入元素
  298. // insert(iterator position, const T& x)
  299. // ↓
  300. // create_node(x)
  301. // p = get_node();-------->list_node_allocator::allocate();
  302. // construct(&p->data, x);
  303. // ↓
  304. // tmp->next = position.node;
  305. // tmp->prev = position.node->prev;
  306. // (link_type(position.node->prev))->next = tmp;
  307. // position.node->prev = tmp;
  308. iterator insert(iterator position, const T& x)
  309. {
  310. link_type tmp = create_node(x);
  311. tmp->next = position.node;
  312. tmp->prev = position.node->prev;
  313. (link_type(position.node->prev))->next = tmp;
  314. position.node->prev = tmp;
  315. return tmp;
  316. }
  317. iterator insert(iterator position) { return insert(position, T()); }
  318. #ifdef __STL_MEMBER_TEMPLATES
  319. template <class InputIterator>
  320. void insert(iterator position, InputIterator first, InputIterator last);
  321. #else /* __STL_MEMBER_TEMPLATES */
  322. void insert(iterator position, const T* first, const T* last);
  323. void insert(iterator position,
  324. const_iterator first, const_iterator last);
  325. #endif /* __STL_MEMBER_TEMPLATES */
  326. // 指定位置插入n个值为x的元素, 详细解析见实现部分
  327. void insert(iterator pos, size_type n, const T& x);
  328. void insert(iterator pos, int n, const T& x)
  329. {
  330. insert(pos, (size_type)n, x);
  331. }
  332. void insert(iterator pos, long n, const T& x)
  333. {
  334. insert(pos, (size_type)n, x);
  335. }
  336. // 在链表前端插入结点
  337. void push_front(const T& x) { insert(begin(), x); }
  338. // 在链表最后插入结点
  339. void push_back(const T& x) { insert(end(), x); }
  340. // 擦除指定结点
  341. iterator erase(iterator position)
  342. {
  343. link_type next_node = link_type(position.node->next);
  344. link_type prev_node = link_type(position.node->prev);
  345. prev_node->next = next_node;
  346. next_node->prev = prev_node;
  347. destroy_node(position.node);
  348. return iterator(next_node);
  349. }
  350. // 擦除一个区间的结点, 详细解析见实现部分
  351. iterator erase(iterator first, iterator last);
  352. void resize(size_type new_size, const T& x);
  353. void resize(size_type new_size) { resize(new_size, T()); }
  354. void clear();
  355. // 删除链表第一个结点
  356. void pop_front() { erase(begin()); }
  357. // 删除链表最后一个结点
  358. void pop_back()
  359. {
  360. iterator tmp = end();
  361. erase(--tmp);
  362. }
  363. list(size_type n, const T& value) { fill_initialize(n, value); }
  364. list(int n, const T& value) { fill_initialize(n, value); }
  365. list(long n, const T& value) { fill_initialize(n, value); }
  366. explicit list(size_type n) { fill_initialize(n, T()); }
  367. // 以一个区间元素为蓝本创建链表
  368. #ifdef __STL_MEMBER_TEMPLATES
  369. template <class InputIterator>
  370. list(InputIterator first, InputIterator last)
  371. {
  372. range_initialize(first, last);
  373. }
  374. #else /* __STL_MEMBER_TEMPLATES */
  375. list(const T* first, const T* last) { range_initialize(first, last); }
  376. list(const_iterator first, const_iterator last) {
  377. range_initialize(first, last);
  378. }
  379. #endif /* __STL_MEMBER_TEMPLATES */
  380. // 复制构造
  381. list(const list<T, Alloc>& x)
  382. {
  383. range_initialize(x.begin(), x.end());
  384. }
  385. ~list()
  386. {
  387. // 释放所有结点 // 使用全局函数distance()进行计算, 时间复杂度O(n)
  388. size_type size() const
  389. {
  390. size_type result = 0;
  391. distance(begin(), end(), result);
  392. return result;
  393. }
  394. clear();
  395. // 释放头结点
  396. put_node(node);
  397. }
  398. list<T, Alloc>& operator=(const list<T, Alloc>& x);
  399. protected:
  400. // 将[first, last)区间插入到position
  401. // 如果last == position, 则相当于链表不变化, 不进行操作
  402. // 初始状态
  403. // first last
  404. // ↓ ↓
  405. // -------- -------- -------- -------- -------- --------
  406. // | next |-->| next |-->| next | | next |-->| next |-->| next |
  407. // ... -------- -------- -------- ... -------- -------- -------- ...
  408. // | prev |<--| prev |<--| prev | | prev |<--| prev |<--| prev |
  409. // -------- -------- -------- -------- -------- --------
  410. //
  411. // position
  412. // ↓
  413. // -------- -------- -------- -------- -------- --------
  414. // | next |-->| next |-->| next |-->| next |-->| next |-->| next |
  415. // ... -------- -------- -------- -------- -------- -------- ...
  416. // | prev |<--| prev |<--| prev |<--| prev |<--| prev |<--| prev |
  417. // -------- -------- -------- -------- -------- --------
  418. //
  419. // 操作完成后状态
  420. // first
  421. // |
  422. // --------------|--------------------------------------
  423. // | ------------|------------------------------------ | last
  424. // | | ↓ | | ↓
  425. // -------- | | -------- -------- -------- | | -------- --------
  426. // | next |-- | ----->| next |-->| next | | next |----- | -->| next |-->| next |
  427. // ... -------- | | -------- -------- ... -------- | | -------- -------- ...
  428. // | prev |<--- | ---| prev |<--| prev | | prev |<-- | -----| prev |<--| prev |
  429. // -------- | | -------- -------- -------- | | -------- --------
  430. // | | | |
  431. // | ------ | |
  432. // ------- | ------------------------------ |
  433. // | | | |
  434. // | | | -----------------------------
  435. // | | | |
  436. // | | | | position
  437. // | | | | ↓
  438. // -------- -------- | | | | -------- -------- -------- --------
  439. // | next |-->| next |-- | | -->| next |-->| next |-->| next |-->| next |
  440. // ... -------- -------- | | -------- -------- -------- -------- ...
  441. // | prev |<--| prev |<--- ------| prev |<--| prev |<--| prev |<--| prev |
  442. // -------- -------- -------- -------- -------- --------
  443. void transfer(iterator position, iterator first, iterator last)
  444. {
  445. if (position != last)
  446. {
  447. (*(link_type((*last.node).prev))).next = position.node;
  448. (*(link_type((*first.node).prev))).next = last.node;
  449. (*(link_type((*position.node).prev))).next = first.node;
  450. link_type tmp = link_type((*position.node).prev);
  451. (*position.node).prev = (*last.node).prev;
  452. (*last.node).prev = (*first.node).prev;
  453. (*first.node).prev = tmp;
  454. }
  455. }
  456. public:
  457. // 将链表x移动到position之前
  458. void splice(iterator position, list& x)
  459. {
  460. if (!x.empty())
  461. transfer(position, x.begin(), x.end());
  462. }
  463. // 将链表中i指向的内容移动到position之前
  464. void splice(iterator position, list&, iterator i)
  465. {
  466. iterator j = i;
  467. ++j;
  468. if (position == i || position == j) return;
  469. transfer(position, i, j);
  470. }
  471. // 将[first, last}元素移动到position之前
  472. void splice(iterator position, list&, iterator first, iterator last)
  473. {
  474. if (first != last)
  475. transfer(position, first, last);
  476. }
  477. void remove(const T& value);
  478. void unique();
  479. void merge(list& x);
  480. void reverse();
  481. void sort();
  482. #ifdef __STL_MEMBER_TEMPLATES
  483. template <class Predicate> void remove_if(Predicate);
  484. template <class BinaryPredicate> void unique(BinaryPredicate);
  485. template <class StrictWeakOrdering> void merge(list&, StrictWeakOrdering);
  486. template <class StrictWeakOrdering> void sort(StrictWeakOrdering);
  487. #endif /* __STL_MEMBER_TEMPLATES */
  488. friend bool operator== __STL_NULL_TMPL_ARGS (const list& x, const list& y);
  489. };
  490. // 判断两个链表是否相等
  491. template <class T, class Alloc>
  492. inline bool operator==(const list<T,Alloc>& x, const list<T,Alloc>& y)
  493. {
  494. typedef typename list<T,Alloc>::link_type link_type;
  495. link_type e1 = x.node;
  496. link_type e2 = y.node;
  497. link_type n1 = (link_type) e1->next;
  498. link_type n2 = (link_type) e2->next;
  499. for ( ; n1 != e1 && n2 != e2 ;
  500. n1 = (link_type) n1->next, n2 = (link_type) n2->next)
  501. if (n1->data != n2->data)
  502. return false;
  503. return n1 == e1 && n2 == e2;
  504. }
  505. // 链表比较大小使用的是字典顺序
  506. template <class T, class Alloc>
  507. inline bool operator<(const list<T, Alloc>& x, const list<T, Alloc>& y)
  508. {
  509. return lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
  510. }
  511. // 如果编译器支持模板函数特化优先级
  512. // 那么将全局的swap实现为使用list私有的swap以提高效率
  513. #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
  514. template <class T, class Alloc>
  515. inline void swap(list<T, Alloc>& x, list<T, Alloc>& y)
  516. {
  517. x.swap(y);
  518. }
  519. #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
  520. // 将[first, last)区间插入到position之前
  521. #ifdef __STL_MEMBER_TEMPLATES
  522. template <class T, class Alloc> template <class InputIterator>
  523. void list<T, Alloc>::insert(iterator position,
  524. InputIterator first, InputIterator last)
  525. {
  526. for ( ; first != last; ++first)
  527. insert(position, *first);
  528. }
  529. #else /* __STL_MEMBER_TEMPLATES */
  530. template <class T, class Alloc>
  531. void list<T, Alloc>::insert(iterator position, const T* first, const T* last) {
  532. for ( ; first != last; ++first)
  533. insert(position, *first);
  534. }
  535. template <class T, class Alloc>
  536. void list<T, Alloc>::insert(iterator position,
  537. const_iterator first, const_iterator last) {
  538. for ( ; first != last; ++first)
  539. insert(position, *first);
  540. }
  541. #endif /* __STL_MEMBER_TEMPLATES */
  542. // 在position前插入n个值为x的元素
  543. template <class T, class Alloc>
  544. void list<T, Alloc>::insert(iterator position, size_type n, const T& x)
  545. {
  546. for ( ; n > 0; --n)
  547. insert(position, x);
  548. }
  549. // 擦除[first, last)间的结点
  550. template <class T, class Alloc>
  551. list<T,Alloc>::iterator list<T, Alloc>::erase(iterator first, iterator last)
  552. {
  553. while (first != last) erase(first++);
  554. return last;
  555. }
  556. // 重新设置容量大小
  557. // 如果当前容量小于新容量, 则新增加值为x的元素, 使容量增加至新指定大小
  558. // 如果当前容量大于新容量, 则析构出来的元素
  559. template <class T, class Alloc>
  560. void list<T, Alloc>::resize(size_type new_size, const T& x)
  561. {
  562. iterator i = begin();
  563. size_type len = 0;
  564. for ( ; i != end() && len < new_size; ++i, ++len)
  565. ;
  566. if (len == new_size)
  567. erase(i, end());
  568. else // i == end()
  569. insert(end(), new_size - len, x);
  570. }
  571. // 销毁所有结点, 将链表置空
  572. template <class T, class Alloc>
  573. void list<T, Alloc>::clear()
  574. {
  575. link_type cur = (link_type) node->next;
  576. while (cur != node) {
  577. link_type tmp = cur;
  578. cur = (link_type) cur->next;
  579. destroy_node(tmp);
  580. }
  581. node->next = node;
  582. node->prev = node;
  583. }
  584. // 链表赋值操作
  585. // 如果当前容器元素少于x容器, 则析构多余元素,
  586. // 否则将调用insert插入x中剩余的元素
  587. template <class T, class Alloc>
  588. list<T, Alloc>& list<T, Alloc>::operator=(const list<T, Alloc>& x)
  589. {
  590. if (this != &x) {
  591. iterator first1 = begin();
  592. iterator last1 = end();
  593. const_iterator first2 = x.begin();
  594. const_iterator last2 = x.end();
  595. while (first1 != last1 && first2 != last2) *first1++ = *first2++;
  596. if (first2 == last2)
  597. erase(first1, last1);
  598. else
  599. insert(last1, first2, last2);
  600. }
  601. return *this;
  602. }
  603. // 移除特定值的所有结点
  604. // 时间复杂度O(n)
  605. template <class T, class Alloc>
  606. void list<T, Alloc>::remove(const T& value)
  607. {
  608. iterator first = begin();
  609. iterator last = end();
  610. while (first != last) {
  611. iterator next = first;
  612. ++next;
  613. if (*first == value) erase(first);
  614. first = next;
  615. }
  616. }
  617. // 移除容器内所有的相邻的重复结点
  618. // 时间复杂度O(n)
  619. // 用户自定义数据类型需要提供operator ==()重载
  620. template <class T, class Alloc>
  621. void list<T, Alloc>::unique()
  622. {
  623. iterator first = begin();
  624. iterator last = end();
  625. if (first == last) return;
  626. iterator next = first;
  627. while (++next != last) {
  628. if (*first == *next)
  629. erase(next);
  630. else
  631. first = next;
  632. next = first;
  633. }
  634. }
  635. // 假设当前容器和x都已序, 保证两容器合并后仍然有序
  636. template <class T, class Alloc>
  637. void list<T, Alloc>::merge(list<T, Alloc>& x)
  638. {
  639. iterator first1 = begin();
  640. iterator last1 = end();
  641. iterator first2 = x.begin();
  642. iterator last2 = x.end();
  643. while (first1 != last1 && first2 != last2)
  644. if (*first2 < *first1) {
  645. iterator next = first2;
  646. transfer(first1, first2, ++next);
  647. first2 = next;
  648. }
  649. else
  650. ++first1;
  651. if (first2 != last2) transfer(last1, first2, last2);
  652. }
  653. // 将链表倒置
  654. // 其算法核心是历遍链表, 每次取出一个结点, 并插入到链表起始点
  655. // 历遍完成后链表满足倒置
  656. template <class T, class Alloc>
  657. void list<T, Alloc>::reverse()
  658. {
  659. if (node->next == node || link_type(node->next)->next == node) return;
  660. iterator first = begin();
  661. ++first;
  662. while (first != end()) {
  663. iterator old = first;
  664. ++first;
  665. transfer(begin(), old, first);
  666. }
  667. }
  668. // 按照升序排序
  669. template <class T, class Alloc>
  670. void list<T, Alloc>::sort()
  671. {
  672. if (node->next == node || link_type(node->next)->next == node) return;
  673. list<T, Alloc> carry;
  674. list<T, Alloc> counter[64];
  675. int fill = 0;
  676. while (!empty()) {
  677. carry.splice(carry.begin(), *this, begin());
  678. int i = 0;
  679. while(i < fill && !counter[i].empty()) {
  680. counter[i].merge(carry);
  681. carry.swap(counter[i++]);
  682. }
  683. carry.swap(counter[i]);
  684. if (i == fill) ++fill;
  685. }
  686. for (int i = 1; i < fill; ++i) counter[i].merge(counter[i-1]);
  687. swap(counter[fill-1]);
  688. }
  689. #ifdef __STL_MEMBER_TEMPLATES
  690. // 给定一个仿函数, 如果仿函数值为真则进行相应元素的移除
  691. template <class T, class Alloc> template <class Predicate>
  692. void list<T, Alloc>::remove_if(Predicate pred)
  693. {
  694. iterator first = begin();
  695. iterator last = end();
  696. while (first != last) {
  697. iterator next = first;
  698. ++next;
  699. if (pred(*first)) erase(first);
  700. first = next;
  701. }
  702. }
  703. // 根据仿函数, 决定如何移除相邻的重复结点
  704. template <class T, class Alloc> template <class BinaryPredicate>
  705. void list<T, Alloc>::unique(BinaryPredicate binary_pred)
  706. {
  707. iterator first = begin();
  708. iterator last = end();
  709. if (first == last) return;
  710. iterator next = first;
  711. while (++next != last) {
  712. if (binary_pred(*first, *next))
  713. erase(next);
  714. else
  715. first = next;
  716. next = first;
  717. }
  718. }
  719. // 假设当前容器和x均已序, 将x合并到当前容器中, 并保证在comp仿函数
  720. // 判定下仍然有序
  721. template <class T, class Alloc> template <class StrictWeakOrdering>
  722. void list<T, Alloc>::merge(list<T, Alloc>& x, StrictWeakOrdering comp)
  723. {
  724. iterator first1 = begin();
  725. iterator last1 = end();
  726. iterator first2 = x.begin();
  727. iterator last2 = x.end();
  728. while (first1 != last1 && first2 != last2)
  729. if (comp(*first2, *first1)) {
  730. iterator next = first2;
  731. transfer(first1, first2, ++next);
  732. first2 = next;
  733. }
  734. else
  735. ++first1;
  736. if (first2 != last2) transfer(last1, first2, last2);
  737. }
  738. // 根据仿函数comp据定如何排序
  739. template <class T, class Alloc> template <class StrictWeakOrdering>
  740. void list<T, Alloc>::sort(StrictWeakOrdering comp)
  741. {
  742. if (node->next == node || link_type(node->next)->next == node) return;
  743. list<T, Alloc> carry;
  744. list<T, Alloc> counter[64];
  745. int fill = 0;
  746. while (!empty()) {
  747. carry.splice(carry.begin(), *this, begin());
  748. int i = 0;
  749. while(i < fill && !counter[i].empty()) {
  750. counter[i].merge(carry, comp);
  751. carry.swap(counter[i++]);
  752. }
  753. carry.swap(counter[i]);
  754. if (i == fill) ++fill;
  755. }
  756. for (int i = 1; i < fill; ++i) counter[i].merge(counter[i-1], comp);
  757. swap(counter[fill-1]);
  758. }
  759. #endif /* __STL_MEMBER_TEMPLATES */
  760. #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
  761. #pragma reset woff 1174
  762. #endif
  763. __STL_END_NAMESPACE
  764. #endif /* __SGI_STL_INTERNAL_LIST_H */
  765. // Local Variables:
  766. // mode:C++
  767. // End:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/128140
推荐阅读
相关标签
  

闽ICP备14008679号