Vc STL's List Iterator P.j implementation

xiaoxiao2021-03-06  65

Implementation of VC p.j STL List

<< Programmer-13-Memory-Pool >>

Houjie View - Technical Philosophy and Painless Application of Memory Pool

In the article, the VC STL PJ implemented this version of the allocator. (Memory pool and memory distributor). At the same time, in order to write a large amount of data buffer pool in the database, something similar to memory pool management and its stack Iterator Just learn from the implementation code of the Iterator in the VC List, but lack the List's code, confirmed some "Hou Jie point - the philosophical and painless use of Spring and Autumn in the pool", and there is a point of PJ. The code readability is almost, and it is very unpalad. It is not very poor. It may not feel that this may be related to habits. It feels that the PJ's code is simple. The code does not have a GCC's implementation STL, so it is not such a twice.

It mainly looks at its Iterator implementation. There are many books of other lists, or directly look at the code implementation of the list, so understanding is relatively profound. Clear Iterator implementation, after direct COPY, change

In STL, for the type of TYPEDEF, don't look dazzling .j

VC's List template declaration, it uses the default Allocator memory distributor. There is a code of its implementation below.

Document: stddef.h

#ifndef _PTRDIFF_T_DEFINED

TYPEDEF INT PTRDIFF_T;

#define _PTRDIFF_T_DEFINED

#ENDIF

Document: UTILITY

Empty TAG class, the main purpose is to make iterator trains

Struct Input_iterator_tag {};

Struct output_iterator_tag {};

STRUCT Forward_Iterator_tag

: public input_iterator_tag {};

STRUCT BIDIRECTIONAL_ITERATOR_TAG

: public forward_iterator_tag {};

Struct random_access_iterator_tag

: public bidirectional_iterator_tag {};

// The most basic Iterator statement, essence is an empty class, providing type definitions

Template

Struct itrator {

TYPEDEF _C ITERATOR_CATEGORY;

TYPEDEF _TY value_type;

TYPEDEF_D DISTANCE_TYPE;

}

// The most basic layer of _BIDIT declaration, essential is an empty class

Template

Struct _bidit: public iterator

_Ty, _d> {};

File: list

For example: how the Iterator is used.

X :: Iterator IT;

For (it = x.begin (); it! = x.end (); IT)

? = * IT;

Here, the List function becom (), end (), iterator! = Operator, * operator, operator.

Const_ITerator's statement in List, it doesn't matter if this _bidit base class can be seen as not.

Const_iterator has a member variable _nodeptr_ptr; saves the current chain table node pointer. The mobile transformation of the pointer is implemented by the _ACC this middle class, defined in the list.

Class const_iterator: public _bidit <_ty, Difference_Type> {

PUBLIC:

Const_Iterator ()

{}

const_iterator (_NodePtr_P)

: _PTR (_P) {}

Const_Iterator (Const Iterator & _x)

: _Ptr (_x._ptr) {}

Const_reference Operator * () Const

{return (_Acc :: _ value (_ptr));

_Ctptr operator -> () const

{Return (& ** this);

Const_Iterator & Operator ()

{_PTR = _Acc :: _ next (_ptr);

Return (* this);

Const_Iterator Operator (INT)

{const_iterator_tmp = * this;

* THIS;

Return (_TMP);

Const_Iterator & Operator - ()

{_PTR = _Acc :: _ prev (_ptr);

Return (* this);

Const_Iterator Operator - (INT)

{const_iterator_tmp = * this;

- * this;

Return (_TMP);

Bool Operator == (const const_iterator & _x) Const

{RETURN (_ptr == _x._ptr);

BOOL Operator! = (const const_iterator & _x) Const

{Return (! (* this == _x));

_Nodeptr_mynode () const

{Return (_PTR);

protected:

_Nodeptr_ptr;

}

// iTerator in the statement of List, it inherits const_iterator

Friend class iterator;

Class Iterator: public const_iterator {

PUBLIC:

Iterator ()

{}

Iterator (_NodePtr_P)

: const_iterator (_p) {}

Reference Operator * () const

{return (_Acc :: _ value (_ptr));

_Tptr operation -> () const

{Return (& ** this);

Iterator & Operator ()

{_PTR = _Acc :: _ next (_ptr);

Return (* this);

Iterator Operator (int)

{item_tmp = * this;

* THIS;

Return (_TMP);

Iterator & Operator - ()

{_PTR = _Acc :: _ prev (_ptr);

Return (* this);

Iterator Operator - (int)

{item_tmp = * this;

- * this;

Return (_TMP);

Bool Operator == (Const Iterator & _x) Const

{RETURN (_ptr == _x._ptr);

Bool Operator! = (Const Iterator & _x) Const

{Return (! (* this == _x));

}

Template >

Class List {protected:

Struct _node;

Friend struct _node;

Typedef _pointer_x (_Node, _A) _NodePtr;

/ / Data structure J of the list

Struct _node {

_Nodeptr_next, _prev;

_Ty _value;

}

Struct_Acc;

Friend struct_acc;

// This structure is only used in Iterator, designing it is to separate the mobile phone of Iterator and pointers. This makes the // iTerator's code is very simple and clean, notice that they are static functions.

Struct_acc {

Typedef _reference_x (_nodeptr, _A) _nodepref;

Typedef _a :: refreste _vref;

Static _NodePref _next (_NodePtr_P)

{RETURN (_NodePref) (* _P) ._ next);

Static _nodepref _prev (_NodePtr_P)

{RETURN ((_nodepref) (* _ p) ._ prev);

Static _VREF _VALUE (_NodePtr _P)

{RETURN ((_VREF) (* _ p) ._ value);

}

PUBLIC:

// Types secure TypeDef definition

Typedef List <_ty, _a> _myt;

Typedef _a allocator_type;

Typedef _a :: size_type size_type;

Typedef _a :: Difference_Type Difference_type;

Typedef _a :: Pointer_tptr

Typedef _a :: const_pointer _ctptr

TYPEDEF _A:: Reference Reference;

Typedef _a :: const_reference.

Typedef _a :: value_type value_type;

// Iterator's forward declaration

Class itrator;

Class const_iterator;

Friend Class Const_IstRessional;

//Constructor

Explicit List (const _a & _al = _A ())

: Allocator (_AL),

_Head (_buynode ()), _size (0) {}

Explicit List (size_type _n, const_ty & _v = _ty (),

Const _a & _al = _a ())

: Allocator (_AL),

_Head (_buynode ()), _size (0)

{INSERT (begin (), _n, _v);

List (const_myt & _x)

: Allocator (_X.allocator),

_Head (_buynode ()), _size (0)

{INSERT (Begin (), _x.begin (), _x.end ());

List (const_ty * _f, const _ty * _l, const _a & _al = _A ())

: Allocator (_AL),

_Head (_buynode ()), _size (0)

{INSERT (Begin (), _f, _l);

Typedef const_iterator_it;

List (_IT _F, _IT _L, const _a & _al = _A ())

: Allocator (_AL),

_Head (_buynode ()), _size (0)

{INSERT (Begin (), _f, _l);

~ List () {ERASE (Begin (), END ());

_Freenode (_HEAD);

_HEAD = 0, _size = 0;

_MYT & Operator = (const _myt & _x)

{IF (this! = & _x)

{Iterator_f1 = begin ();

Iterator _l1 = end ();

Const_iterator _f2 = _x.begin ();

const_iterator _l2 = _x.end ();

For (; _f1! = _l1 && _f2! = _l2; _ f1, _ f2)

* _F1 = * _f2;

ERASE (_f1, _l1);

INSERT (_L1, _f2, _l2);

Return (* this);

// below is some common methods involved in Iterator

// begin (), End () is returning an Iterator object, other similar

Iterator begin ()

{return (item (_ACC :: _ next (_head));}

Const_Iterator Begin () Const

{RETURN (const_iterator (_acc :: _ next (_head)));

Iterator end ()

{Return (iTerator (_Head);}

const_iterator End () Const

{return (const_iterator (_head));

Reverse_iterator rbegin ()

{RETURN (REVERSE_ITERATOR (End ()));

const_reverse_iterator rbegin () const

{return (const_reverse_iterator (end ()));}

Reverse_iterator rend ()

{RETURN (Reverse_iterator (begin ()));}

Const_reverse_iterator rend () const

{RETURN (const_reverse_iterator (begin ()));}

// below is some common methods

void resize (size_type _n, _ty _x = _ty ())

{IF (size () <_n)

INSERT (end (), _n - size (), _x);

Else

While (_n

POP_BACK ();

SIZE_TYPE SIZE () Const

{return (_size);

SIZE_TYPE MAX_SIZE () Const

{return (allocator.max_size ());

BOOL EMPTY () Const

{RETURN (size () == 0);

_A get_allocator () const

{return (allocator);

Reference Front ()

{RETURN (* begin ());

Const_reference front () const () const

{RETURN (* begin ());

Reference back ()

{RETURN (* ()));}

Const_reference back () const

{RETURN (* ()));}

Void push_front (const_ty & _x)

{INSERT (Begin (), _x);

Void pop_front ()

{Erase (begin ());

Void Push_Back (const_ty & _x) {INSERT (end (), _x);

Void pop_back ()

{ERASE (- end ());}

Void Assign (_IT _F, _IT _L)

{Erase (Begin (), END ());

INSERT (begin (), _f, _l);

Void Assign (size_type _n, const _ty & _x = _ty ())

{Erase (Begin (), END ());

INSERT (Begin (), _n, _x);

............ .....

}

Memory distributor implementation

Obviously, the ALLOCATE Template function uses the Operator New implementation, DEAllocate uses the delete implementation, so Allocate is a shallow packaging class for C New and Delete.

File: xmemory

#define _farq

#define _pdft ptrdiff_t

#define _sizt size_t

Template inline

_Ty _farq * _allocate (_PDFT _N, _TY _FARQ *)

{IF (_n <0)

_N = 0;

Return (_ty _farq *) Operator New

(_Sizt) _N * sizeof (_ty)));

Template

Class allocator {

PUBLIC:

Typedef _sizt size_type;

TYPEDEF _PDFT DIFCERENCE_TYPE;

Typedef _ty _farq * Pointer;

Typedef const_ty _farq * const_point;

TYPEDEF _TY _FARQ & REFERENCE

Typedef const_ty;

TYPEDEF _TY value_type;

Pointer Address (Reference_x) Const

{Return (& _x);

const_pointer address (const_reference _x) Const

{Return (& _x);

Pointer Allocate (size_type _n, const void *)

{RETURN (_allocate) _n, (pointer));}

Char _farq * _charalloc (size_type _n)

{RETURN (_allocate ((Difference_Type) _n,

(char _farq *) 0);

Void Deallocate (void _farq * _p, size_type)

{Operator delete (_P);

Void Construct (Pointer_P, Const_ty & _V)

{_P, _V);

Void Destroy (Pointer_P)

{_DESTROY (_P);

_Sizt max_size () const

{_Sizt _n = (_sizt) (- 1) / sizeof (_TY);

Return (0 <_n? _n: 1);

}

appendix:

SGI STL implementation

List header file

#ifndef __sgi_stl_list

#define __sgi_stl_list

#include

#include

#include #include

#include

#ENDIF / * __SGI_STL_LIST * /

STL_List.h

#ifndef __sgi_stl_internal_list_h

#define __sgi_stl_internal_list_h

#include

__STL_BEGIN_NAMESPACE

#if Defined (__ sgi) &&! defined (__ gnuc__) && (_MIPS_SIM! = _MIPS_SIM_ABI32)

#pragma set WOFF 1174

#pragma set WOFF 1375

#ENDIF

Struct _list_node_base {

_LIST_NODE_BASE * _M_NEXT;

_LIST_NODE_BASE * _M_PREV;

}

Template

Struct _List_node: public _list_node_base {

_TP _M_DATA;

}

Struct _list_iterator_base {

TYPEDEF SIZE_T SIZE_TYPE;

TYPEDEF PTRDIFF_T DIFCERENCE_TYPE;

TYPEDEF BIDIRECTIONAL_ITERATOR_TAG ITERATOR_CATEGORY

_LIST_NODE_BASE * _M_NODE;

_List_iterator_base (_list_node_base * __x): _m_node (__) {}

_List_iterator_base () {}

Void _m_incr () {_m_node = _m_node -> _ m_next;}

Void _m_decr () {_m_node = _m_node -> _ m_prev;}

BOOL Operator == (const _list_iterator_base & __x) const {

Return_m_node == __x._m_node;

}

BOOL Operator! = (const _list_iterator_base & __x) const {

Return_M_Node! = __X._M_Node;

}

}

Template

Struct _list_iterator: public _list_iterator_base {

TYPEDEF _LIST_ITERATOR <_TP, _TP &, _ TP *> Iterator;

Typedef _list_iterator <_tp, const _tp &, const _tp *> const_iterator)

TYPEDEF _LIST_ITERATOR <_TP, _ref, _ptr> _self;

Typedef _tp value_type;

Typedef _ptr pointer;

Typedef _ref reason;

TYPEDEF _LIST_NODE <_tp> _node;

_List_iterator (_Node * __x): _List_iterator_base (__ x) {}

_List_iterator () {}

_List_iterator (const itrator & __x): _list_iterator_base (__ x._m_node) {} Reference Operator * () const {return ((_node *) _m_node) -> _ m_data;}

#ifndef __sgi_stl_no_arrow_operator

Pointer Operator -> () const {return (operator * ());

#ENDIF / * __SGI_STL_NO_ARROW_OPERATOR * /

_Self & Operator () {

THIS -> _ m_incr ();

RETURN * THIS;

}

_Self Operator (int) {

_SELF __TMP = * THIS;

THIS -> _ m_incr ();

Return__tmp;

}

_Self & Operator - () {

THIS -> _ m_decr ();

RETURN * THIS;

}

_Self Operator - (int) {

_SELF __TMP = * THIS;

THIS -> _ m_decr ();

Return__tmp;

}

}

#ifndef __stl_class_partial_specialization

Inline Bidirectional_Istrator_tag

Iterator_category (const _list_iterator_base)

{

Return Bidirectional_Item_tag ();

}

Template

Inline_tp *

Value_type (const _list_iterator <_tp, _ref, _ptr>)

{

Return 0;

}

Inline PTRDIFF_T *

Distance_type (const _list_iterator_base)

{

Return 0;

}

#ENDIF / * __STL_CLASS_PARTIAL_SPECIALIZATION * /

// Base Class That Encapsulates Details of Allocators. Three Cases:

// An Ordinary Standard-Conforming Allocator, A Standard-Conforming

// Allocator with no Non-static data, and an sgi-style allocator.

// this Complexity Is Necessary Only Because We're Worrying About Backward

// compatibility and because we want to avoid Wasting Storage on AN AN

// allocator instance if it isn't Necessary.

#ifDef __stl_use_std_allocators

// base for General Standard-Conforming Allocators.

Template

Class _List_alloc_base {

PUBLIC:

TypeDef Typename _alloc_traits <_tp, _allocator> :: allocator_type

Allocator_Type;

Allocator_type get_allocator () const {return_node_allocator;} _list_alloc_base (const allocator_type& __a): _node_allocator (__ a) {}

protected:

_List_node <_tp> * _m_get_node ()

{RETURN _NODE_ALLOCATOR.Allocate (1);

Void _m_put_node (_list_node <_tp> * __p)

{_Node_Allocator.deallocate (__ p, 1);}

protected:

Typename _alloc_traits <_list_node <_tp>, _allocator> :: allocator_type

_Node_allocator;

_List_node <_tp> * _m_node;

}

// Specialization for Instanceless Allocators.

Template

Class _List_alloc_base <_tp, _allocator, true> {

PUBLIC:

TypeDef Typename _alloc_traits <_tp, _allocator> :: allocator_type

Allocator_Type;

Allocator_type get_allocator () const {return allocator_type ();}

_List_alloc_base (const allocator_type&) {}

protected:

TypedEf TypeName _alloc_traits <_list_node <_tp>, _allocator> :: _ alloc_type

_Alloc_type;

_List_node <_tp> * _m_get_node () {return_alloc_type :: allocate (1);

Void _m_put_node (_list_node <_tp> * __p) {_alloc_type :: deallocate (__ p, 1);}

protected:

_List_node <_tp> * _m_node;

}

Template

Class _List_Base

: public _List_alloc_base <_tp, _alloc,

_Alloc_traits <_tp, _alloc> :: _ s_instanceless>

{

PUBLIC:

TYPEDEF _LIST_ALLOC_BASE <_tp, _alloc,

_Alloc_traits <_tp, _alloc> :: _ s_instanceless>

_Base;

TypedEf TypeName_base :: allocator_type allocator_type;

_List_base (const allocator_type & __a): _base (__ a) {

_M_node = _m_get_node ();

_M_node -> _ m_next = _m_node;

_M_node -> _ m_prev = _m_node;

}

~ _List_base () {

Clear ();

_M_put_node (_m_node);

}

Void clear ();

}

#ELSE / * __STL_USE_STD_ALLOCATORS * /

Template class _list_base

{

PUBLIC:

Typedef _alloc allocator_type;

Allocator_type get_allocator () const {return allocator_type ();}

_List_base (const allocator_type&) {

_M_node = _m_get_node ();

_M_node -> _ m_next = _m_node;

_M_node -> _ m_prev = _m_node;

}

~ _List_base () {

Clear ();

_M_put_node (_m_node);

}

Void clear ();

protected:

TYPEDEF SIMPLE_ALLOC <_List_Node <_tp>, _alloc> _alloc_type;

_List_node <_tp> * _m_get_node () {return_alloc_type :: allocate (1);

Void _m_put_node (_list_node <_tp> * __p) {_alloc_type :: deallocate (__ p, 1);}

protected:

_List_node <_tp> * _m_node;

}

#ENDIF / * __STL_USE_STD_ALLOCATORS * /

Template

Void

_List_base <_tp, _alloc> :: clear ()

{

_List_node <_tp> * __cur = (_list_node <_tp> *) _M_Node -> _ M_Next;

While (__cur! = _m_node) {

_List_node <_tp> * __tmp = __cur;

__cur = (_list_node <_tp> *) __cur -> _ m_next;

_Destroy (& __ tmp -> _ m_data);

_M_PUT_NODE (__ TMP);

}

_M_node -> _ m_next = _m_node;

_M_node -> _ m_prev = _m_node;

}

Template

Class List: protected _list_base <_tp, _alloc> {

// Requirements:

__STL_CLASS_REQUIRES (_TP, _Assignable);

Typedef _List_base <_tp, _alloc> _base;

protected:

Typedef void * _void_point;

PUBLIC:

Typedef _tp value_type;

TypeDef value_type * pointer;

Typedef const value_type * const_point;

TypeDef value_type & reason;

TypedEf const_type & const_reference;

TYPEDEF _LIST_NODE <_tp> _node;

TYPEDEF SIZE_T SIZE_TYPE;

TYPEDEF PTRDIFF_T DIFCERENCE_TYPE;

TypedEf TypeName _base :: allocator_type allocator_type; allocator_type get_allocator () const {return_base:: get_allocator ();

PUBLIC:

TYPEDEF _LIST_ITERATOR <_TP, _TP &, _ TP *> Iterator;

Typedef _list_iterator <_tp, const _tp &, const _tp *> const_iterator)

#ifdef __stl_class_partial_specialization

TypedEf reverse_iterator const_reverse_iterator;

TypedEf Reverse_Iterator Reverse_iterator;

#ELSE / * __STL_CLASS_PARTIAL_SPECIALIZATION * /

TypedEf Reverse_Bidirectional_Iterator

Const_reference, Difference_Type>

Const_reverse_iterator;

Typedef reverse_bidirectional_iterator

Difference_type>

REVERSE_ITERATOR;

#ENDIF / * __STL_CLASS_PARTIAL_SPECIALIZATION * /

protected:

#ifdef __stl_has_namespaces

Using _BASE :: _ m_node;

Using _BASE :: _ m_put_node;

Using _BASE :: _ m_get_node;

#ENDIF / * __STL_HAS_NAMESPACES * /

protected:

_Node * _m_create_node (const _tp & __x)

{

_Node * __P = _m_get_node ();

__Stl_try {

_Construct (& __P -> _ m_data, __x);

}

__Stl_unwind (_M_PUT_NODE (__ P));

Return__p;

}

_Node * _M_create_node ()

{

_Node * __P = _m_get_node ();

__Stl_try {

_Construct (& __P -> _ m_data);

}

__Stl_unwind (_M_PUT_NODE (__ P));

Return__p;

}

PUBLIC:

Explicit List (const allocator_type& __a = allocator_type ()): _base (__ a) {}

Iterator begin () {return (_node *) (_ m_node -> _ m_next);

const_iterator begin () const {return (_node *) (_ m_node -> _ m_next);

Iterator end () {return_m_node;}

Const_iterator End () const {return _m_node;}

Reverse_iterator rbegin ()

{RETURN REVERSE_ITERATOR (End ());

Const_reverse_iterator rbegin () const {return const_reverse_iterator (end ());

Reverse_iterator rend ()

{RETURN REVERSE_ITERATOR (Begin ());

Const_reverse_iterator rend () const

{return const_reverse_iterator (begin ());}

Bool Empty () const {return _m_node -> _ m_next == _m_node;}

SIZE_TYPE SIZE () Const {

SIZE_TYPE __RESULT = 0;

Distance (Begin (), End (), __RESULT

Return __Result;

}

SIZE_TYPE MAX_SIZE () Const {Return Size_Type (-1);

Reference front () {return * begin ();

Const_reference front () const {return * begin ();

Reference back () {return * (- end ());

Const_reference back () const {return * (- end ());}

Void swap (list <_tp, _alloc> & __x) {__std :: swap (_m_node, __x._m_node);

Iterator ITERT (item __position, const _tp & __x) {

_Node * __TMP = _m_create_node (__ x);

__tmp -> _ m_next = __position._m_node;

__tmp -> _ m_prev = __position._m_node -> _ m_prev;

__position._m_node -> _ m_prev -> _ m_next = __TMP;

__position._m_node -> _ m_prev = __tmp;

Return__tmp;

}

Iterator INSERT (orerator __position) {returnin insert (__ position, _tp ());

#ifDef __stl_member_templates

// Check WHETHER IT'S AN INTEGRAL TYPE. If SO, IT'S Not An Iterator.

Template

Void _m_insert_dispatch (item __pos, _integer __n, _integer __x,

__true_type) {

_M_fill_insert (__ pOS, (size_type) __n, (_tp) __x);

}

Template

Void _m_insert_dispatch (item __pos,

_INPUTITERATOR __FIRST, _INPUTITERATOR __LAST,

__false_type);

Template

Void insert (item __pos, _inputiterator __first, _inputiterator __last) {

TypedEf Typename_IS_INTEGER <_InputIterator> :: _ integral_integral

_M_INSERT_DISPATCH (__ pos, __first, __last, _integral ());

}

#ELSE / * __STL_MEMBER_TEMPLATES * /

Void insert (item __position, const _tp * __first, const _tp * __last);

Void insert (item __position,

const_iterator __first, const_iterator __last;

#ENDIF / * __STL_MEMBER_TEMPLATES * /

Void insert (item __pos, size_type __n, const _tp & __x)

{_M_FILL_INSERT (__ pOS, __, __x);

Void _m_fill_insert (item __pos, size_type __n, const _tp & __x);

Void push_front (const_tp & __x) {INSERT (begin (), __x);

Void push_front () {INSERT (begin ());

Void Push_Back (const_tp & __x) {INSERT (end (), __x);}

Void push_back () {INSERT (end ());

Iterator erase (iterator __position) {

_List_node_base * __next_node = __position._m_node -> _ m_next;

_List_node_base * __prev_node = __position._m_node -> _ m_prev;

_Node * __n = (_node *) __position._m_node;

__prev_node -> _ m_next = __next_node;

__next_node -> _ m_prev = __prev_node;

_DESTROY (& __ n -> _ m_data);

_M_PUT_NODE (__ N);

Return Iterator ((_ node *) __next_node);

}

Iterator Erase (item __first, iterator __last);

Void clear () {_base :: clear ();

Void Resize (size_type __new_size, const _tp & __x);

Void resize (size_type __new_size) {this-> resize (__ new_size, _tp ());

Void pop_front () {Erase (begin ());}

Void pop_back () {

Iterator __tmp = end ();

ERASE (--__ TMP);

}

List (size_type __n, const _tp& __value,

const allocator_type& __a = allocator_type ())

: _BASE (__ a)

{INSERT (begin (), __n, __value);

Explicit List (size_type __n)

: _BASE (allocator_type ())

{INSERT (Begin (), __n, _tp ());

#ifDef __stl_member_templates

// We don't need any dispatch tricks here, Because Insert Does All of

// That Anyway.Template

List (_InputIterator __first, _inputiterator __last,

const allocator_type& __a = allocator_type ())

: _BASE (__ a)

{INSERT (begin (), __first, __last);

#ELSE / * __STL_MEMBER_TEMPLATES * /

List (const _tp * __first, const _tp * __last,

const allocator_type& __a = allocator_type ())

: _BASE (__ a)

{this-> INSERT (begin (), __first, __last);

List (const_iterator __first, const_iterator __last,

const allocator_type& __a = allocator_type ())

: _BASE (__ a)

{this-> INSERT (begin (), __first, __last);

#ENDIF / * __STL_MEMBER_TEMPLATES * /

List (const list <_tp, _alloc> & __x): _base (__ x.get_allocator ())

{INSERT (Begin (), __x.begin (), __x.end ());

~ List () {}

List <_tp, _alloc> & operator = (const list <_tp, _alloc> & __x);

PUBLIC:

// Assign (), a generalized assignment member function. Two

// Versions: One Takes a count, and one what takes a ing.

// The Range Version Is A Member Template, So We dispatch on WHETHER

// or not the Type is an integer.

Void Assign (SIZE_TYPE __N, ConST _TP & __VAL) {_m_fill_assign (__ n, __val);

Void _m_fill_assign (size_type __n, const _tp & __val);

#ifDef __stl_member_templates

Template

Void Assign (_inputiterator __first, _inputiterator __last) {

TypedEf Typename_IS_INTEGER <_InputIterator> :: _ integral_integral

_M_assign_dispatch (__ first, __last, _integral ());

}

Template

Void _m_assign_dispatch (_integer __n, _integer __val, __true_type)

{_M_FILL_ASSIGN ((size_type) __n, (_tp) __val);

Template

Void _m_assign_dispatch (_inputiterator __first, _inputiterator __last,

__false_type); # Endif / * __stl_member_templates * /

protected:

Void Transfer (item __position, item __first, itrator __last) {

IF (__position! = __last) {

// Remove [first, last) from its old position.

__last._m_node -> _ m_prev -> _ m_next = __position._m_node;

__first._m_node -> _ m_prev -> _ m_next = __last._m_node;

__position._m_node -> _ m_prev -> _ m_next = __first._m_node;

// splice [first, last) INTO ITS New Position.

_List_node_base * __tmp = __position._m_node -> _ m_prev;

__position._m_node -> _ m_prev = __last._m_node -> _ m_prev;

__last._m_node -> _ m_prev = __first._m_node -> _ m_prev;

__first._m_node -> _ m_prev = __tmp;

}

}

PUBLIC:

Void splice (item __position, list & __x) {

IF (! __ x.empty ())

this-> transfer (__ position, __x.begin (), __x.end ());

}

Void splice (item __position, list &, iterator __i) {

Iterator __j = __i;

__ j;

IF (__position == __i || __position == __j) return;

this-> Transfer (__ position, __i, __j);

}

Void splice (item __position, list &, itrator __first, iterator __last) {

IF (__first! = __last)

This-> Transfer (__ position, __first, __last);

}

Void Remove; Const_TP & __Value;

void unique ();

Void Merge (List & __x);

Void Reverse ();

Void sort ();

#ifDef __stl_member_templates

Template void remove_if (_predicate);

Template void unique (_BINARYPREDICATE);

Template void merge (list &, _strictweakorder);

Template void sort (_strictweakordering);

#ENDIF / * __STL_MEMBER_TEMPLATES * /

}

Template inline bool

Operator == (const list <_tp, _alloc> & __x, const list <_tp, _alloc> & __y)

{

TypedEf TypeName List <_tp, _alloc> :: const_iterator const_iterator;

const_iterator __end1 = __x.end ();

const_iterator __end2 = __y.end ();

const_iterator __i1 = __x.begin ();

const_iterator __i2 = __y.begin ();

While (__i1! = __end1 && __i2! = __END2 && * __ i1 == * __ i2) {

__ i1;

__ i2;

}

Return __i1 == __END1 && __I2 == __END2;

}

Template

Inline Bool Operator <(const list <_tp, _alloc> & __x,

Const List <_tp, _alloc> & __y)

{

Return lexicographical_compare (__ x.begin (), __x.end (),

__y.begin (), __y.end ());

}

#ifdef __stl_function_tmpl_partial_order

Template

Inline Bool Operator! = (const list <_tp, _alloc> & __x,

Const list <_tp, _alloc> & __y) {

Return! (__ x == __y);

}

Template

Inline Bool Operator> (Const List <_tp, _alloc> & __x,

Const list <_tp, _alloc> & __y) {

Return __y <__x;

}

Template

Inline Bool Operator <= (const list <_tp, _alloc> & __x,

Const list <_tp, _alloc> & __y) {

Return! (__ y <__x);

}

Template

Inline Bool Operator> = (const list <_tp, _alloc> & __x,

Const list <_tp, _alloc> & __y) {

Return! (__ x <__y);

}

Template

Inline void

SWAP (List <_tp, _alloc> & __x, list <_tp, _alloc> & __y)

{

__x.swap (__ y);

}

#ENDIF / * __STL_FUNCTION_TMPL_PARTIAL_ORDER * / # ifdef __stl_member_templates

Template template

Void

List <_tp, _alloc> :: _ m_insert_dispatch (item __position,

_Inputiter __first, _inputiter __last,

__false_type)

{

For (; __first! = __last; __first)

INSERT (__ position, * __first);

}

#ELSE / * __STL_MEMBER_TEMPLATES * /

Template

Void

List <_tp, _alloc> :: insert (item __position,

const _tp * __first, const _tp * __last)

{

For (; __first! = __last; __first)

INSERT (__ position, * __first);

}

Template

Void

List <_tp, _alloc> :: insert (item __position,

const_iterator __first, const_iterator __last)

{

For (; __first! = __last; __first)

INSERT (__ position, * __first);

}

#ENDIF / * __STL_MEMBER_TEMPLATES * /

Template

Void

List <_tp, _alloc> :: _ m_fill_insert (item __position,

SIZE_TYPE __N, ConST _TP & __X)

{

For (; __n> 0; --__ n)

INSERT (__ position, __x);

}

Template

TypeName List <_tp, _alloc> :: item list <_tp, _alloc> :: Erase (item __first,

Iterator __last)

{

While (__first! = __last)

ERASE (__first );

Return __last;

}

Template

Void List <_tp, _alloc> :: resize (size_type __new_size, const _tp& __x)

{

Iterator __i = begin ();

SIZE_TYPE __LEN = 0;

For (; __i! = end () && __len <__new_size; __ i, __ len)

;

IF (__len == __new_size)

ERASE (__ i, end ());

Else // __i == End ()

INSERT (end (), __new_size - __len, __x);

Template

List <_tp, _alloc> & list <_tp, _alloc> :: operator = (const list <_tp, _alloc> & __x)

{

IF (this! = & __ x) {

Iterator __first1 = begin ();

Iterator __last1 = end ();

const_iterator __first2 = __x.begin ();

const_iterator __last2 = __x.end ();

While (__first1! = __last1 && __first2! = __last2)

* __first1 = * __ first2 ;

IF (__first2 == __last2)

ERASE (__ numbert1, __last1);

Else

INSERT (__ last1, __first2, __last2);

}

RETURN * THIS;

}

Template

Void List <_tp, _alloc> :: _ m_fill_assign (size_type __n, const _tp& __val) {

Iterator __i = begin ();

For (; __i! = end () && __n> 0; __ i, --__)

* __ i = __val;

IF (__n> 0)

INSERT (end (), __n, __val);

Else

ERASE (__ i, end ());

}

#ifDef __stl_member_templates

Template template

Void

List <_tp, _alloc> :: _ m_assign_dispatch (_Inputiter __first2, _inputiter __last2,

__false_type)

{

Iterator __first1 = begin ();

Iterator __last1 = end ();

For (; __first1! = __last1 && __first2! = __last2; __first1, __first2)

* __first1 = * __ number2;

IF (__first2 == __last2)

ERASE (__ numbert1, __last1);

Else

INSERT (__ last1, __first2, __last2);

}

#ENDIF / * __STL_MEMBER_TEMPLATES * /

Template

Void List <_tp, _alloc> :: remove (const_tp & __value)

{

Iterator __first = begin ();

Iterator __last = End ();

While (__first! = __last) {

Iterator __next = __first;

__ next; if (* __first == __value) ERASE (__ first);

__first = __Next;

}

}

Template

Void List <_tp, _alloc> :: unique ()

{

Iterator __first = begin ();

Iterator __last = End ();

IF (__first == __last) return;

Iterator __next = __first;

While ( __ next! = __last) {

IF (* __ first == * __ next)

ERASE (__ next);

Else

__first = __Next;

__Next = __first;

}

}

Template

Void List <_tp, _alloc> :: merge (list <_tp, _alloc> & __x)

{

Iterator __first1 = begin ();

Iterator __last1 = end ();

Iterator __first2 = __x.begin ();

Iterator __last2 = __x.end ();

While (__first1! = __last1 && __first2! = __last2)

IF (* __ first2 <* __first1) {

Iterator __next = __first2;

Transfer (__ numbert1, __first2, __ next);

__first2 = __Next;

}

Else

__first1;

IF (__first2! = __last2) Transfer (__ last1, __first2, __last2);

}

Inline void __list_base_reverse (_list_node_base * __p)

{

_List_node_base * __tmp = __p;

Do {

__Std :: swap (__ tmp -> _ m_next, __tmp -> _ m_prev);

__tmp = __tmp -> _ m_prev; // old next node is now prev.

} while (__tmp! = __p);

}

Template

Inline void list <_tp, _alloc> :: reverse ()

{

__List_base_reverse (this -> _ m_node);

}

Template

Void List <_tp, _alloc> :: sort ()

{

// do Nothing if the list has length 0 or 1.

IF (_m_node -> _ m_next! = _m_node && _m_node -> _ m_next -> _ m_next! = _m_node) {

List <_tp, _alloc> __carry;

List <_tp, _alloc> __counter [64];

INT __FILL = 0;

While (! Empty ()) {__carry.splice (__ carry.begin (), * this, begin ());

INT __I = 0;

While (__ i <__fill &&! __ counter [__ i] .empty ()) {

__counter [__ i] .merge (__ carry);

__carry.swap (__ counter [__ i ]);

}

__carry.swap (__ counter [__ i]);

IF (__i == __fill) __ fill;

}

For (int __i = 1; __i <__fill; __ i)

__counter [__ i] .merge (__ counter [__ i-1]);

SWAP (__ counter [__ fill-1]);

}

}

#ifDef __stl_member_templates

Template template

Void List <_tp, _alloc> :: remove_if (_predicate __pred)

{

Iterator __first = begin ();

Iterator __last = End ();

While (__first! = __last) {

Iterator __next = __first;

__ next;

IF (__pred (* __first)) ERASE (__ first);

__first = __Next;

}

}

Template template

Void List <_tp, _alloc> :: unique (_binarypredicate __binary_pred)

{

Iterator __first = begin ();

Iterator __last = End ();

IF (__first == __last) return;

Iterator __next = __first;

While ( __ next! = __last) {

IF (__binary_pred (* __ number, * __ next))

ERASE (__ next);

Else

__first = __Next;

__Next = __first;

}

}

Template template

Void List <_tp, _alloc> :: merge (list <_tp, _alloc> & __x,

_Strictweakordering __comp)

{

Iterator __first1 = begin ();

Iterator __last1 = end ();

Iterator __first2 = __x.begin ();

Iterator __last2 = __x.end ();

While (__first1! = __last1 && __first2! = __last2)

IF (__comp (* __first2, * __first1)) {

Iterator __next = __first2;

Transfer (__ first1, __first2, __ next); __first2 = __Next;

}

Else

__first1;

IF (__first2! = __last2) Transfer (__ last1, __first2, __last2);

}

Template template

Void List <_tp, _alloc> :: sort (_strictweakordering __comp)

{

// do Nothing if the list has length 0 or 1.

IF (_m_node -> _ m_next! = _m_node && _m_node -> _ m_next -> _ m_next! = _m_node) {

List <_tp, _alloc> __carry;

List <_tp, _alloc> __counter [64];

INT __FILL = 0;

While (! Empty ()) {

__carry.splice (__ carry.begin (), * this, begin ());

INT __I = 0;

While (__ i <__fill &&! __ counter [__ i] .empty ()) {

__counter [__] .merge (__ carry, __comp);

__carry.swap (__ counter [__ i ]);

}

__carry.swap (__ counter [__ i]);

IF (__i == __fill) __ fill;

}

For (int __i = 1; __i <__fill; __ i)

__counter [__] .merge (__ counter [__ i-1], __comp);

SWAP (__ counter [__ fill-1]);

}

}

#ENDIF / * __STL_MEMBER_TEMPLATES * /

#if Defined (__ sgi) &&! defined (__ gnuc__) && (_MIPS_SIM! = _MIPS_SIM_ABI32)

#pragma reset Woff 1174

#pragma reset woff 1375

#ENDIF

__Stl_end_namespace

#ENDIF / * __SGI_STL_INTERNAL_LIST_H * /

转载请注明原文地址:https://www.9cbs.com/read-86586.html

New Post(0)