📄 vector
字号:
}
}
// interfaces
private:
property size_type Count_generic
{ // element count
virtual size_type get() sealed
= System::Collections::Generic::ICollection<_Value_t>::Count::get
{ // get element count
return (size());
}
};
property bool IsReadOnly
{ // test if read only
virtual bool get() sealed
= System::Collections::Generic::ICollection<_Value_t>
::IsReadOnly::get
{ // test if read only
return (false);
}
};
virtual void CopyTo(_Myarray_t^ _Dest, int _First) sealed
= System::Collections::Generic::ICollection<_Value_t>::CopyTo
{ // copy to _Dest, beginning at _First
for (int _Idx = size(); 0 <= --_Idx; )
{ // copy back to front
_Dest[_First + _Idx] = _Mymake_t::make_value(at(_Idx));
}
}
virtual System::Collections::Generic::IEnumerator<_Value_t>^
GetEnumerator() sealed
= System::Collections::Generic::IEnumerable<_Value_t>::GetEnumerator
{ // get enumerator for the container
return (gcnew _STLCLR VectorEnumerator<_Value_t>(this, 0));
}
virtual void Add(value_type _Val) sealed
= System::Collections::Generic::ICollection<_Value_t>::Add
{ // add element with value _Val
insert_n(size(), 1, _Val);
}
virtual void Clear() sealed
= System::Collections::Generic::ICollection<_Value_t>::Clear
{ // erase all elements
clear();
}
virtual bool Contains(value_type _Val) sealed
= System::Collections::Generic::ICollection<_Value_t>::Contains
{ // search for element matching value _Val
for (size_type _Idx = 0; _Idx != size(); ++_Idx)
if (((System::Object^)_Val)->Equals(
(System::Object^)at(_Idx)))
return (true);
return (false);
}
virtual bool Remove(value_type _Val) sealed
= System::Collections::Generic::ICollection<_Value_t>::Remove
{ // remove first element matching value _Val
for (size_type _Idx = 0; _Idx != size(); ++_Idx)
if (((System::Object^)_Val)->Equals(
(System::Object^)at(_Idx)))
{ // found a match, remove it
erase_n(_Idx, _Idx + 1);
return (true);
}
return (false);
}
virtual int IndexOf(value_type _Val) sealed
= System::Collections::Generic::IList<_Value_t>::IndexOf
{ // find index of element that matches _Val
int _Idx = 0;
for (; _Idx < size(); ++_Idx)
if (at(_Idx) == _Val)
return (_Idx);
return (-1);
}
virtual void Insert(int _Idx, value_type _Val) sealed
= System::Collections::Generic::IList<_Value_t>::Insert
{ // insert _Val before _Idx
insert_n(_Idx, 1, _Val);
}
virtual void RemoveAt(int _Idx) sealed
= System::Collections::Generic::IList<_Value_t>::RemoveAt
{ // erase element at _Idx
erase_n(_Idx, _Idx + 1);
}
};
//
// TEMPLATE CLASS vector_select
//
template<typename _Value_t,
bool _Is_ref>
ref class vector_select
: public vector_base<_Value_t, _Is_ref>
{ // varying size array of elements
public:
// types
typedef _Value_t _Gvalue_t;
typedef vector_select<_Value_t, _Is_ref> _Mytype_t;
typedef vector_base<_Gvalue_t, _Is_ref> _Mybase_t;
// typedef _STLCLR IVector<_Gvalue_t> _Mycont_it;
// typedef System::Collections::Generic::IEnumerable<_Gvalue_t> _Myenum_it;
// typedef int size_type;
// typedef int difference_type;
typedef _Value_t value_type;
typedef value_type% reference;
typedef value_type% const_reference;
// typedef _Mycont_it generic_container;
// typedef value_type generic_value;
// basics
vector_select()
: _Mybase_t()
{ // construct default
}
vector_select(vector_select% _Right)
: _Mybase_t(_Right)
{ // construct by copying a vector
}
vector_select% operator=(vector_select% _Right)
{ // assign
_Mybase_t::operator=(_Right);
return (*this);
}
// constructors
explicit vector_select(size_type _Count)
: _Mybase_t(_Count)
{ // construct from _Count * value_type()
}
vector_select(size_type _Count, value_type _Val)
: _Mybase_t(_Count, _Val)
{ // construct from _Count * _Val
}
template<typename _InIt_t>
vector_select(_InIt_t _First, _InIt_t _Last)
: _Mybase_t(_First, _Last)
{ // construct from [_First, _Last)
}
vector_select(_Myenum_it^ _Right)
: _Mybase_t(_Right)
{ // initialize with enumeration
}
};
//
// TEMPLATE CLASS vector_select: _Value_t REF SPECIALIZATION
//
template<typename _Value_t>
ref class vector_select<_Value_t, true>
: public vector_base<_Value_t^, true>
{ // varying size array of elements
public:
// types
typedef _Value_t^ _Gvalue_t;
typedef vector_select<_Value_t, true> _Mytype_t;
typedef vector_base<_Gvalue_t, true> _Mybase_t;
// typedef _STLCLR IVector<_Gvalue_t> _Mycont_it;
// typedef System::Collections::Generic::IEnumerable<_Gvalue_t> _Myenum_it;
// typedef int size_type;
// typedef int difference_type;
typedef _Value_t value_type;
typedef value_type% reference;
typedef value_type% const_reference;
// typedef _Mycont_it generic_container;
// typedef value_type generic_value;
// basics
vector_select()
: _Mybase_t()
{ // construct default
}
vector_select(vector_select% _Right)
: _Mybase_t(_Right)
{ // construct by copying a vector
}
vector_select% operator=(vector_select% _Right)
{ // assign
_Mybase_t::operator=(_Right);
return (*this);
}
// constructors
explicit vector_select(size_type _Count)
{ // construct from _Count * value_type()
resize(_Count);
}
vector_select(size_type _Count, value_type _Val)
{ // construct from _Count * _Val
resize(_Count, _Val);
}
template<typename _InIt_t>
vector_select(_InIt_t _First, _InIt_t _Last)
: _Mybase_t(_First, _Last)
{ // construct from [_First, _Last)
}
vector_select(_Myenum_it^ _Right)
: _Mybase_t(_Right)
{ // initialize with enumeration
}
// size controllers
virtual void resize(size_type _Newsize) override
{ // determine new length, padding with value_type elements
value_type _Val;
_Mybase_t::resize(_Newsize, %_Val);
}
void resize(size_type _Newsize, value_type _Val)
{ // determine new length, padding with _Val elements
_Mybase_t::resize(_Newsize, %_Val);
}
// accessors
reference at(size_type _Pos) new
{ // subscript mutable sequence with checking
return (*_Mybase_t::at(_Pos));
}
property value_type default[size_type]
{ // get or set subscripted element
value_type get(size_type _Pos)
{ // get _Pos element
return (*_Mybase_t::at(_Pos));
}
void set(size_type _Pos, value_type _Val)
{ // set _Pos element
_Mybase_t::at(_Pos) = gcnew value_type(_Val);
}
};
property value_type front_item
{ // get or set first element
value_type get()
{ // get first element
return (*_Mybase_t::front_item);
}
void set(value_type _Val)
{ // set first element
_Mybase_t::front_item = gcnew value_type(_Val);
}
};
property value_type back_item
{ // get or set last element
value_type get()
{ // get last element
return (*_Mybase_t::back_item);
}
void set(value_type _Val)
{ // set last element
_Mybase_t::back_item = gcnew value_type(_Val);
}
};
reference front() new
{ // get first element of mutable sequence
return (*_Mybase_t::front());
}
reference back() new
{ // get last element of mutable sequence
return (*_Mybase_t::back());
}
// mutators
// void push_front(value_type _Val)
void push_back(value_type _Val)
{ // insert element at end
_Mybase_t::push_back(%_Val);
}
void assign(size_type _Count, value_type _Val)
{ // assign _Count * _Val
_Mybase_t::assign(_Count, %_Val);
}
iterator insert(iterator _Where, value_type _Val)
{ // insert _Val at _Where
return (_Mybase_t::insert(_Where, %_Val));
}
void insert(iterator _Where,
size_type _Count, value_type _Val)
{ // insert _Count * _Val at _Where
return (_Mybase_t::insert(_Where, _Count, %_Val));
}
};
} // namespace cliext::impl
//
// TEMPLATE CLASS vector
//
template<typename _Value_t>
ref class vector
: public impl::vector_select<_Value_t,
__is_ref_class(typename _Dehandle<_Value_t>::type)
&& !is_handle<_Value_t>::value>
{ // varying size array of elements
public:
// types
typedef vector<_Value_t> _Mytype_t;
typedef impl::vector_select<_Value_t,
__is_ref_class(typename _Dehandle<_Value_t>::type)
&& !is_handle<_Value_t>::value> _Mybase_t;
// typedef int size_type;
// typedef int difference_type;
// typedef _Value_t value_type;
// typedef value_type% reference;
// typedef value_type% const_reference;
// typedef _Mycont_it generic_container;
// typedef value_type generic_value;
// basics
vector()
: _Mybase_t()
{ // construct default
}
vector(vector% _Right)
: _Mybase_t((_Mybase_t%)_Right)
{ // construct by copying a vector
}
vector(vector^ _Right)
: _Mybase_t((_Mybase_t%)*_Right)
{ // construct by copying a vector
}
vector% operator=(vector% _Right)
{ // assign
_Mybase_t::operator=(_Right);
return (*this);
}
vector% operator=(vector^ _Right)
{ // assign
_Mybase_t::operator=(*_Right);
return (*this);
}
// constructors
explicit vector(size_type _Count)
: _Mybase_t(_Count)
{ // construct from _Count * value_type()
}
vector(size_type _Count, value_type _Val)
: _Mybase_t(_Count, _Val)
{ // construct from _Count * _Val
}
template<typename _InIt_t>
vector(_InIt_t _First, _InIt_t _Last)
: _Mybase_t(_First, _Last)
{ // construct from [_First, _Last)
}
vector(_Myenum_it^ _Right)
: _Mybase_t(_Right)
{ // initialize with enumeration
}
// converters
virtual System::Object^ Clone() override
{ // clone the vector
return (gcnew _Mytype_t(*this));
}
// mutators
void swap(vector% _Right)
{ // exchange contents with _Right
_Mybase_t::swap(_Right);
}
};
//
// TEMPLATE COMPARISONS
//
template<typename _Value_t> inline
bool operator==(vector<_Value_t>% _Left,
vector<_Value_t>% _Right)
{ // test if _Left == _Right
vector<_Value_t>::size_type _Size = _Left.size();
if (_Size != _Right.size())
return (false);
else
{ // same length, compare elements
for (int _Idx = 0; _Idx != _Size; ++_Idx)
if (_Left.at(_Idx) != _Right.at(_Idx))
return (false);
return (true);
}
}
template<typename _Value_t> inline
bool operator!=(vector<_Value_t>% _Left,
vector<_Value_t>% _Right)
{ // test if _Left != _Right
return (!(_Left == _Right));
}
template<typename _Value_t> inline
bool operator<(vector<_Value_t>% _Left,
vector<_Value_t>% _Right)
{ // test if _Left < _Right
vector<_Value_t>::size_type _Idx = 0;
for (; _Idx != _Left.size() && _Idx != _Right.size(); ++_Idx)
if (_Left.at(_Idx) < _Right.at(_Idx))
return (true);
else if (_Right.at(_Idx) < _Left.at(_Idx))
return (false);
return (_Idx == _Left.size() && _Idx != _Right.size());
}
template<typename _Value_t> inline
bool operator>=(vector<_Value_t>% _Left,
vector<_Value_t>% _Right)
{ // test if _Left >= _Right
return (!(_Left < _Right));
}
template<typename _Value_t> inline
bool operator>(vector<_Value_t>% _Left,
vector<_Value_t>% _Right)
{ // test if _Left > _Right
return (_Right < _Left);
}
template<typename _Value_t> inline
bool operator<=(vector<_Value_t>% _Left,
vector<_Value_t>% _Right)
{ // test if _Left <= _Right
return (!(_Right < _Left));
}
//
// TEMPLATE FUNCTION std::swap
//
template<typename _Value_t> inline
void swap(vector<_Value_t>% _Left,
vector<_Value_t>% _Right)
{ // swap two vectors
_Left.swap(_Right);
}
} // namespace cliext
#endif // _CLI_VECTOR_
/*
* Copyright (c) 2004-2007 by Dinkumware, Ltd. ALL RIGHTS RESERVED.
* Consult your license regarding permissions and restrictions.
V5.03:0009 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -