📄 stl_slist.h
字号:
} before_first->next = last_node; return last_node; }public: iterator insert_after(iterator pos, const value_type& x) { return iterator(_insert_after(pos.node, x)); } iterator insert_after(iterator pos) { return insert_after(pos, value_type()); } void insert_after(iterator pos, size_type n, const value_type& x) { _insert_after_fill(pos.node, n, x); } void insert_after(iterator pos, int n, const value_type& x) { _insert_after_fill(pos.node, (size_type) n, x); } void insert_after(iterator pos, long n, const value_type& x) { _insert_after_fill(pos.node, (size_type) n, x); }#ifdef __STL_MEMBER_TEMPLATES template <class InIter> void insert_after(iterator pos, InIter first, InIter last) { _insert_after_range(pos.node, first, last); }#else /* __STL_MEMBER_TEMPLATES */ void insert_after(iterator pos, const_iterator first, const_iterator last) { _insert_after_range(pos.node, first, last); } void insert_after(iterator pos, const value_type* first, const value_type* last) { _insert_after_range(pos.node, first, last); }#endif /* __STL_MEMBER_TEMPLATES */ iterator insert(iterator pos, const value_type& x) { return iterator(_insert_after(__slist_previous(&head, pos.node), x)); } iterator insert(iterator pos) { return iterator(_insert_after(__slist_previous(&head, pos.node), value_type())); } void insert(iterator pos, size_type n, const value_type& x) { _insert_after_fill(__slist_previous(&head, pos.node), n, x); } void insert(iterator pos, int n, const value_type& x) { _insert_after_fill(__slist_previous(&head, pos.node), (size_type) n, x); } void insert(iterator pos, long n, const value_type& x) { _insert_after_fill(__slist_previous(&head, pos.node), (size_type) n, x); } #ifdef __STL_MEMBER_TEMPLATES template <class InIter> void insert(iterator pos, InIter first, InIter last) { _insert_after_range(__slist_previous(&head, pos.node), first, last); }#else /* __STL_MEMBER_TEMPLATES */ void insert(iterator pos, const_iterator first, const_iterator last) { _insert_after_range(__slist_previous(&head, pos.node), first, last); } void insert(iterator pos, const value_type* first, const value_type* last) { _insert_after_range(__slist_previous(&head, pos.node), first, last); }#endif /* __STL_MEMBER_TEMPLATES */public: iterator erase_after(iterator pos) { return iterator((list_node*)erase_after(pos.node)); } iterator erase_after(iterator before_first, iterator last) { return iterator((list_node*)erase_after(before_first.node, last.node)); } iterator erase(iterator pos) { return (list_node*) erase_after(__slist_previous(&head, pos.node)); } iterator erase(iterator first, iterator last) { return (list_node*) erase_after(__slist_previous(&head, first.node), last.node); } void resize(size_type new_size, const T& x); void resize(size_type new_size) { resize(new_size, T()); } void clear() { erase_after(&head, 0); }public: // Moves the range [before_first + 1, before_last + 1) to *this, // inserting it immediately after pos. This is constant time. void splice_after(iterator pos, iterator before_first, iterator before_last) { if (before_first != before_last) __slist_splice_after(pos.node, before_first.node, before_last.node); } // Moves the element that follows prev to *this, inserting it immediately // after pos. This is constant time. void splice_after(iterator pos, iterator prev) { __slist_splice_after(pos.node, prev.node, prev.node->next); } // Linear in distance(begin(), pos), and linear in L.size(). void splice(iterator pos, slist& L) { if (L.head.next) __slist_splice_after(__slist_previous(&head, pos.node), &L.head, __slist_previous(&L.head, 0)); } // Linear in distance(begin(), pos), and in distance(L.begin(), i). void splice(iterator pos, slist& L, iterator i) { __slist_splice_after(__slist_previous(&head, pos.node), __slist_previous(&L.head, i.node), i.node); } // Linear in distance(begin(), pos), in distance(L.begin(), first), // and in distance(first, last). void splice(iterator pos, slist& L, iterator first, iterator last) { if (first != last) __slist_splice_after(__slist_previous(&head, pos.node), __slist_previous(&L.head, first.node), __slist_previous(first.node, last.node)); }public: void reverse() { if (head.next) head.next = __slist_reverse(head.next); } void remove(const T& val); void unique(); void merge(slist& L); void sort(); #ifdef __STL_MEMBER_TEMPLATES template <class Predicate> void remove_if(Predicate pred); template <class BinaryPredicate> void unique(BinaryPredicate pred); template <class StrictWeakOrdering> void merge(slist&, StrictWeakOrdering); template <class StrictWeakOrdering> void sort(StrictWeakOrdering comp); #endif /* __STL_MEMBER_TEMPLATES */};template <class T, class Alloc>slist<T, Alloc>& slist<T,Alloc>::operator=(const slist<T, Alloc>& L){ if (&L != this) { list_node_base* p1 = &head; list_node* n1 = (list_node*) head.next; const list_node* n2 = (const list_node*) L.head.next; while (n1 && n2) { n1->data = n2->data; p1 = n1; n1 = (list_node*) n1->next; n2 = (const list_node*) n2->next; } if (n2 == 0) erase_after(p1, 0); else _insert_after_range(p1, const_iterator((list_node*)n2), const_iterator(0)); } return *this;} template <class T, class Alloc>bool operator==(const slist<T, Alloc>& L1, const slist<T, Alloc>& L2){ typedef typename slist<T,Alloc>::list_node list_node; list_node* n1 = (list_node*) L1.head.next; list_node* n2 = (list_node*) L2.head.next; while (n1 && n2 && n1->data == n2->data) { n1 = (list_node*) n1->next; n2 = (list_node*) n2->next; } return n1 == 0 && n2 == 0;}template <class T, class Alloc>inline bool operator<(const slist<T, Alloc>& L1, const slist<T, Alloc>& L2){ return lexicographical_compare(L1.begin(), L1.end(), L2.begin(), L2.end());}#ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDERtemplate <class T, class Alloc>inline void swap(slist<T, Alloc>& x, slist<T, Alloc>& y) { x.swap(y);}#endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */template <class T, class Alloc>void slist<T, Alloc>::resize(size_type len, const T& x){ list_node_base* cur = &head; while (cur->next != 0 && len > 0) { --len; cur = cur->next; } if (cur->next) erase_after(cur, 0); else _insert_after_fill(cur, len, x);}template <class T, class Alloc>void slist<T,Alloc>::remove(const T& val){ list_node_base* cur = &head; while (cur && cur->next) { if (((list_node*) cur->next)->data == val) erase_after(cur); else cur = cur->next; }}template <class T, class Alloc> void slist<T,Alloc>::unique(){ list_node_base* cur = head.next; if (cur) { while (cur->next) { if (((list_node*)cur)->data == ((list_node*)(cur->next))->data) erase_after(cur); else cur = cur->next; } }}template <class T, class Alloc>void slist<T,Alloc>::merge(slist<T,Alloc>& L){ list_node_base* n1 = &head; while (n1->next && L.head.next) { if (((list_node*) L.head.next)->data < ((list_node*) n1->next)->data) __slist_splice_after(n1, &L.head, L.head.next); n1 = n1->next; } if (L.head.next) { n1->next = L.head.next; L.head.next = 0; }}template <class T, class Alloc>void slist<T,Alloc>::sort(){ if (head.next && head.next->next) { slist carry; slist counter[64]; int fill = 0; while (!empty()) { __slist_splice_after(&carry.head, &head, head.next); int i = 0; while (i < fill && !counter[i].empty()) { counter[i].merge(carry); carry.swap(counter[i]); ++i; } carry.swap(counter[i]); if (i == fill) ++fill; } for (int i = 1; i < fill; ++i) counter[i].merge(counter[i-1]); this->swap(counter[fill-1]); }}#ifdef __STL_MEMBER_TEMPLATEStemplate <class T, class Alloc> template <class Predicate> void slist<T,Alloc>::remove_if(Predicate pred){ list_node_base* cur = &head; while (cur->next) { if (pred(((list_node*) cur->next)->data)) erase_after(cur); else cur = cur->next; }}template <class T, class Alloc> template <class BinaryPredicate> void slist<T,Alloc>::unique(BinaryPredicate pred){ list_node* cur = (list_node*) head.next; if (cur) { while (cur->next) { if (pred(((list_node*)cur)->data, ((list_node*)(cur->next))->data)) erase_after(cur); else cur = (list_node*) cur->next; } }}template <class T, class Alloc> template <class StrictWeakOrdering>void slist<T,Alloc>::merge(slist<T,Alloc>& L, StrictWeakOrdering comp){ list_node_base* n1 = &head; while (n1->next && L.head.next) { if (comp(((list_node*) L.head.next)->data, ((list_node*) n1->next)->data)) __slist_splice_after(n1, &L.head, L.head.next); n1 = n1->next; } if (L.head.next) { n1->next = L.head.next; L.head.next = 0; }}template <class T, class Alloc> template <class StrictWeakOrdering> void slist<T,Alloc>::sort(StrictWeakOrdering comp){ if (head.next && head.next->next) { slist carry; slist counter[64]; int fill = 0; while (!empty()) { __slist_splice_after(&carry.head, &head, head.next); int i = 0; while (i < fill && !counter[i].empty()) { counter[i].merge(carry, comp); carry.swap(counter[i]); ++i; } carry.swap(counter[i]); if (i == fill) ++fill; } for (int i = 1; i < fill; ++i) counter[i].merge(counter[i-1], comp); this->swap(counter[fill-1]); }}#endif /* __STL_MEMBER_TEMPLATES */#if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)#pragma reset woff 1174#endif__STL_END_NAMESPACE #endif /* __SGI_STL_INTERNAL_SLIST_H */// Local Variables:// mode:C++// End:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -