64,663
社区成员
发帖
与我相关
我的任务
分享
template<class InputIterator>
String(InputIterator start, InputIterator end);
~String()
{
//std::cout << buffer << std::endl;
delete [] buffer;
}
// String::begin();
iterator begin()
{
return buffer;
}
const_iterator begin() const
{
return buffer;
}
// 返回C风格的字符串;
const char * c_str() const
{
return (NULL == buffer ? "" : buffer);
}
// String::end();
iterator end()
{
return buffer + strLength;
}
const_iterator end() const
{
return buffer + strLength;
}
// String::append
String& append ( const String& str );
String& append ( const String& str, size_t pos, size_t n );
String& append ( const char* s, size_t n );
String& append ( const char* s );
String& append ( size_t n, char c );
template<class InputIterator>
String& append ( InputIterator first, InputIterator last );
// String::operator=
String& operator= ( const String& str);
String& operator= ( const char* s);
String& operator= ( char c );
// String::operatro+=
String& operator+= ( const String& str);
String& operator+= ( const char* s);
String& operator+= ( char c);
// String::assign
String& assign ( const String& str );
String& assign ( const String& str, size_t pos, size_t n );
String& assign ( const char* s, size_t n );
String& assign ( const char* s );
String& assign ( size_t n, char c );
template<class InputIterator>
String& assign ( InputIterator first, InputIterator last );
// String::operator[]
const char& operator[] ( size_t pos ) const
{
return buffer[pos];
}
char& operator[] ( size_t pos )
{
return buffer[pos];
}
//String::at
const char& at (size_t pos) const;
char& at (size_t pos);
// Stirng::lenght()
size_t length() const { return strLength; }
// String::size
size_t size() const { return strLength; }
// String::capacity
size_t capacity () const { return bufLength; }
// String::max_size
size_t max_size() const { return npos; }
// String::clear
void clear();
// String::compare
int compare ( const String& str ) const;
int compare ( const char* s ) const;
int compare ( size_t pos1, size_t n1, const String& str ) const;
int compare ( size_t pos1, size_t n1, const char* s) const;
int compare ( size_t pos1, size_t n1, const String& str, size_t pos2, size_t n2 ) const;
int compare ( size_t pos1, size_t n1, const char* s, size_t n2) const;
// String::copy
size_t copy ( char* s, size_t n, size_t pos = 0) const;
// String::data
const char* data() const { return buffer; }
// String::empty
bool empty ( ) const { return (size_t)0 == strLength; }
// String::erase
String& erase ( size_t pos = 0, size_t n = npos );
iterator erase ( iterator position );
iterator erase ( iterator first, iterator last );
// String::find
size_t find ( const String& str, size_t pos = 0 ) const;
size_t find ( const char* s, size_t pos, size_t n ) const;
size_t find ( const char* s, size_t pos = 0 ) const;
size_t find ( char c, size_t pos = 0 ) const;
//String::find_first_not_of
size_t find_first_not_of ( const String& str, size_t pos = 0 ) const;
size_t find_first_not_of ( const char* s, size_t pos, size_t n ) const;
size_t find_first_not_of ( const char* s, size_t pos = 0 ) const;
size_t find_first_not_of ( char c, size_t pos = 0 ) const;
//String::find_first_of
size_t find_first_of ( const String& str, size_t pos = 0 ) const;
size_t find_first_of ( const char* s, size_t pos, size_t n ) const;
size_t find_first_of ( const char* s, size_t pos = 0 ) const;
size_t find_first_of ( char c, size_t pos = 0 ) const;
//String::find_last_not_of
size_t find_last_not_of ( const String& str, size_t pos = npos ) const;
size_t find_last_not_of ( const char* s, size_t pos, size_t n ) const;
size_t find_last_not_of ( const char* s, size_t pos = npos ) const;
size_t find_last_not_of ( char c, size_t pos = npos ) const;
//String::find_last_of
size_t find_last_of ( const String& str, size_t pos = npos ) const;
size_t find_last_of ( const char* s, size_t pos, size_t n ) const;
size_t find_last_of ( const char* s, size_t pos = npos ) const;
size_t find_last_of ( char c, size_t pos = npos ) const;
//String::insert
String& insert ( size_t pos1, const String& str );
String& insert ( size_t pos1, const String& str, size_t pos2, size_t n );
String& insert ( size_t pos1, const char* s, size_t n);
String& insert ( size_t pos1, const char* s );
String& insert ( size_t pos1, size_t n, char c );
iterator insert ( iterator p, char c );
void insert ( iterator p, size_t n, char c );
//template<class InputIterator>
template<class InputIterator>
void insert ( iterator p, InputIterator first, InputIterator last );
// String::push_back
void push_back ( char c );
//String::replace
String& replace ( size_t pos1, size_t n1, const String& str );
String& replace ( iterator i1, iterator i2, const String& str );
String& replace ( size_t pos1, size_t n1, const String& str, size_t pos2, size_t n2 );
String& replace ( size_t pos1, size_t n1, const char* s, size_t n2 );
String& replace ( iterator i1, iterator i2, const char* s, size_t n2 );
String& replace ( size_t pos1, size_t n1, const char* s );
String& replace ( iterator i1, iterator i2, const char* s );
String& replace ( size_t pos1, size_t n1, size_t n2, char c );
String& replace ( iterator i1, iterator i2, size_t n2, char c );
//template<class InputIterator>
template<class InputIterator>
String& replace ( iterator i1, iterator i2, InputIterator j1, InputIterator j2 );
// String::reserve
void reserve ( size_t res_arg=0 );
// String::swap
void swap ( String& str );
//String::substr
String substr ( size_t pos = 0, size_t n = npos ) const;
// String::rbegin
reverse_iterator rbegin();
const_reverse_iterator rbegin() const;
// String::rend
reverse_iterator rend();
const_reverse_iterator rend() const;
// friend function
friend std::ostream & operator << (std::ostream & os,const String &str);
private:
// 这种设计参考:
// http://users.cis.fiu.edu/~weiss/dsaa_c++/code/
char *buffer; // 指向存储数据区域的指针;
size_t strLength; // String长度;
size_t bufLength; // 存储区域的内存的大小;
size_t doFind(const char * s1, const char * s2, size_t pos) const;
size_t doFindFirstNotOf(const char * s1, const char * s2, size_t pos ) const;
size_t doFindFirstOf(const char * s1, const char * s2, size_t pos ) const;
size_t doFindLastNotOf(const char * s1, const char * s2, size_t pos ) const;
size_t doFindLastOf(const char * s1, const char * s2, size_t pos ) const;
size_t strlen(const char * str) const
{
size_t ret(0);
while(*str++)
ret++;
return ret;
}
char * strcpy(char *dest, const char * src) const
{
char * p = dest;
while((*p++ = *src++))
;
return dest;
}
char * strncpy(char *dest, const char * src, size_t n) const
{
char *p = dest;
size_t cnt = 0;
while((cnt++ < n) && (*p++ = *src++))
;
*p = '\0';
return dest;
}
int strcmp(const char * s1, const char * s2) const
{
while (*s1 && *s2 && *s1 == *s2)
{
s1++;
s2++;
}
return (*s1 - *s2);
}
};
std::istream& getline ( std::istream& is, String& str, char delim );
std::istream& getline ( std::istream& is, String& str );
String operator+ (const String& lhs, const String& rhs);
String operator+ (const char* lhs, const String& rhs);
String operator+ (char lhs, const String& rhs);
String operator+ (const String& lhs, const char* rhs);
String operator+ (const String& lhs, char rhs);
// comparison operators function
bool operator== ( const String& lhs, const String& rhs );
bool operator== ( const char* lhs, const String& rhs );
bool operator== ( const String& lhs, const char* rhs );
bool operator!= ( const String& lhs, const String& rhs );
bool operator!= ( const char* lhs, const String& rhs );
bool operator!= ( const String& lhs, const char* rhs );
bool operator< ( const String& lhs, const String& rhs );
bool operator< ( const char* lhs, const String& rhs );
bool operator< ( const String& lhs, const char* rhs );
bool operator> ( const String& lhs, const String& rhs );
bool operator> ( const char* lhs, const String& rhs );
bool operator> ( const String& lhs, const char* rhs );
bool operator<= ( const String& lhs, const String& rhs );
bool operator<= ( const char* lhs, const String& rhs );
bool operator<= ( const String& lhs, const char* rhs );
bool operator>= ( const String& lhs, const String& rhs );
bool operator>= ( const char* lhs, const String& rhs );
bool operator>= ( const String& lhs, const char* rhs );
#endif /* STRING_H */
template <typename T, typename Alloc = Allocator<T> >
class List {
public:
typedef typename Alloc::reference reference;
typedef typename Alloc::const_reference const_reference;
typedef typename Alloc::size_type size_type;
typedef typename Alloc::difference_type difference_type;
typedef typename Alloc::value_type value_type;
typedef typename Alloc::pointer pointer;
typedef typename Alloc::const_pointer const_pointer;
typedef Alloc allocator_type;
typedef ListNode<T> Node;
typedef Node * nodePtr;
typedef ListIterator<T> iterator;
typedef ConstListIterator<T> const_iterator;
typedef ListReverseIterator<T> reverse_iterator;
typedef const ListReverseIterator<T> const_reverse_iterator;
explicit List ( const Alloc& = Alloc() ) : head(0), len(0) { initList();}
explicit List ( size_type n, const T& value = T(), const Alloc& = Alloc() );
template < class InputIterator >
List ( InputIterator first, InputIterator last, const Alloc& = Alloc() );
List ( const List<T,Alloc>& x );
~List () {
freeList(begin(), end());
destroyList();
}
void swap ( List<T,Alloc>& lst)
{
nodePtr tmp = lst.head;
size_type l = lst.len;
lst.head = head;
lst.len = len;
head = tmp;
len = l;
}
template <class InputIterator>
void assign ( InputIterator first, InputIterator last );
void assign ( size_type n, const T& u );
bool empty ( ) const
{
return (len == 0);
}
size_type size() const
{
return len;
}
size_type max_size () const
{
return nodeAlloc.max_size();
}
void resize ( size_type sz, T c = T() );
iterator begin ()
{
return (head->next);
}
const_iterator begin () const
{
return head->next;
}
iterator end ()
{
return head;
}
const_iterator end () const
{
return head;
}
reference front ()
{
if (empty())
throw OutOfRange();
return *begin();
}
const_reference front () const
{
if (empty())
throw OutOfRange();
return *begin();
}
reference back ( )
{
if (empty())
throw OutOfRange();
return *(--end() );
}
const_reference back ( ) const
{
if (empty())
throw OutOfRange();
return *(--end() );
}
void push_front ( const T& x )
{
insertNode(begin(), x);
}
void pop_front ()
{
if (empty())
throw OutOfRange();
freeNode(begin());
}
void push_back ( const T& x )
{
insertNode(end(), x);
}
void pop_back ()
{
if (empty())
throw OutOfRange();
freeNode(--end());
}
iterator insert ( iterator position, const T& x )
{
return insertNode(position, x);
}
void insert ( iterator position, size_type n, const T& x );
template <class InputIterator>
void insert ( iterator position, InputIterator first, InputIterator last );
iterator erase ( iterator position )
{
return freeNode(position);
}
iterator erase ( iterator first, iterator last )
{
for (; first != last;)
first = freeNode(first);
return first;
}
void clear()
{
freeList(begin(), end());
}
void splice ( iterator position, List<T,Alloc>& x );
void splice ( iterator position, List<T,Alloc>& x, iterator i );
void splice ( iterator position, List<T,Alloc>& x, iterator first, iterator last );
void remove ( const T& value );
template <class Predicate>
void remove_if ( Predicate pred );
void reverse ( );
void sort ( );
template <class Compare>
void sort ( Compare comp );
void unique ( );
template <class BinaryPredicate>
void unique ( BinaryPredicate binary_pred );
void merge ( List<T,Alloc>& x );
template <class Compare>
void merge ( List<T,Alloc>& x, Compare comp );
allocator_type get_allocator() const
{
return alloc;
}
private:
allocator_type alloc;
nodePtr head;
size_type len;
Allocator<Node> nodeAlloc;
iterator newNode(const T & x = T())
{
nodePtr cur = nodeAlloc.allocate(1);
alloc.construct(&cur->elem, x);
return cur;
}
void destroyNode(iterator pos)
{
alloc.destroy(&(pos.p->elem));
nodeAlloc.deallocate(pos.p, 1);
}
void initList()
{
head = newNode().p;
head->prev = head->next = head;
}
void destroyList()
{
destroyNode(head);
}
iterator insertNode(iterator pos, const T & x)
{
iterator it = newNode(x);
if (pos == end()) {
it.p->prev = head->prev;
head->prev->next = it.p;
head->prev = it.p;
it.p->next = head;
}
else {
it.p->prev = pos.p->prev;
pos.p->prev->next = it.p;
it.p->next = pos.p;
pos.p->prev = it.p;
}
++len;
return it;
}
iterator insertNode(iterator pos, iterator it )
{
if (pos == end()) {
it.p->prev = head->prev;
head->prev->next = it.p;
head->prev = it.p;
it.p->next = head;
}
else {
it.p->prev = pos.p->prev;
pos.p->prev->next = it.p;
it.p->next = pos.p;
pos.p->prev = it.p;
}
++len;
return it;
}
iterator appendNode(iterator pos, const T & x)
{
iterator it = newNode(x);
it.p->prev = pos.p;
it.p->next = pos.p->next;
pos.p->next->prev = it.p;
pos.p->next = it.p;
++len;
return it;
}
iterator removeNode(iterator pos)
{
if (pos == end())
return pos;
pos.p->prev->next = pos.p->next;
pos.p->next->prev = pos.p->prev;
--len;
return ++pos;
}
iterator freeNode(iterator pos)
{
iterator ret = removeNode(pos);
destroyNode(pos);
return ret;
}
void freeList(iterator first, iterator last)
{
for (; first != last; )
first = freeNode(first);
}
};
template <typename T, typename Alloc >
inline List<T, Alloc>::List ( size_type n, const T& value, const Alloc& ): head(0), len(0)
{
initList();
assign(n, value);
}
template <typename T, typename Alloc >
template < class InputIterator >
inline List<T, Alloc>::List ( InputIterator first, InputIterator last, const Alloc& ) : head(0), len(0)
{
initList();
assign(first, last);
}
template <typename T, typename Alloc >
inline List<T, Alloc>::List ( const List<T,Alloc>& x ) : head(0), len(0)
{
initList();
assign(x.begin(), x.end());
}
template <typename T, typename Alloc >
template <class InputIterator>
void List<T, Alloc>::assign ( InputIterator first, InputIterator last )
{
List<T, Alloc> lst;
iterator pos = lst.begin();
for (; first != last; ++first)
pos = lst.appendNode(pos, *first);
swap(lst);
}
template <typename T, typename Alloc >
void List<T, Alloc>::assign ( size_type n, const T& u )
{
List<T, Alloc> lst;
iterator pos = lst.begin();
for (size_type i = 0; i < n; ++i)
pos = lst.appendNode(pos, u);
swap(lst);
}
template <typename T, typename Alloc >
void List<T, Alloc>::resize ( size_type sz, T c)
{
size_type diff = 0;
if (sz == size())
return;
if (sz > size())
{
diff = sz - size();
while (diff > 0)
{
insertNode(end(), c);
--diff;
}
}
else
{
diff = size() - sz;
while (diff > 0)
{
freeNode(--end());
--diff;
}
}
}
template <typename T, typename Alloc = Allocator<T> >
class Vector {
public:
typedef typename Alloc::reference reference;
typedef typename Alloc::const_reference const_reference;
typedef typename Alloc::size_type size_type;
typedef typename Alloc::difference_type difference_type;
typedef typename Alloc::value_type value_type;
typedef typename Alloc::pointer pointer;
typedef typename Alloc::const_pointer const_pointer;
typedef Alloc allocator_type;
typedef VectorIterator<T> iterator;
typedef const VectorIterator<T> const_iterator;
typedef VectorReverseIterator<T> reverse_iterator;
typedef const VectorReverseIterator<T> const_reverse_iterator;
explicit Vector ( const Alloc& = Alloc() ): head(0), first_free(0), tail(0) {}
explicit Vector ( size_type n, const T& value= T());
template <class InputIterator>
Vector ( InputIterator first, InputIterator last);
Vector ( const Vector<T,Alloc>& x );
~Vector ()
{
T * p = head;
for (; p != first_free; ++p)
alloc.destroy(p);
alloc.deallocate(head, tail - head);
}
Vector<T, Alloc>& operator= (const Vector<T, Alloc>& x);
size_type size() const
{
difference_type diff = first_free - head;
return ((size_type) diff);
}
template <typename InputIterator>
void assign (InputIterator first, InputIterator last);
void assign (size_type n, const T& u);
iterator begin () { return head;}
const_iterator begin () const { return head; }
iterator end () { return first_free;}
const_iterator end () const { return first_free; }
reverse_iterator rbegin() { return first_free - 1;}
const_reverse_iterator rbegin() const { return first_free - 1;}
reverse_iterator rend() { return head - 1;}
const_reverse_iterator rend() const { return head - 1; }
reference operator[] ( size_type n )
{
return *(begin() + n);
}
const_reference operator[] ( size_type n ) const
{
return *(begin() + n);
}
const_reference at ( size_type n ) const
{
if (n < size())
return *(begin() + n);
throw OutOfRange();
}
reference at ( size_type n )
{
if (n < size())
return *(begin() + n);
throw OutOfRange();
}
reference back ( )
{
return (*--end());
}
const_reference back ( ) const
{
return (*--end());
}
reference front ( )
{
return (*begin());
}
const_reference front ( ) const
{
return (*begin());
}
void push_back ( const T& x )
{
if (first_free == tail)
reallocate();
alloc.construct(first_free++, x);
}
size_type capacity () const
{
return (size_type)(tail - head);
}
size_type max_size () const
{
return alloc.max_size();
}
void resize ( size_type sz, T c = T() );
void reserve ( size_type n );
void clear();
bool empty () const
{
return (head == first_free);
}
void pop_back()
{
alloc.destroy(--first_free);
}
iterator insert ( iterator position, const T& x );
void insert ( iterator position, size_type n, const T& x );
template <class InputIterator>
void insert ( iterator position, InputIterator first, InputIterator last );
iterator erase ( iterator position );
iterator erase ( iterator first, iterator last );
void swap ( vector<T,Alloc>& vec );
allocator_type get_allocator() const
{
return alloc;
}
private:
Alloc alloc; //!< 内存分配器
T * head; //!< 指向数组的第一个元素
T * first_free; //!< 指向第一个空闲的元素
T * tail; //!< 指向数组的最后元素的下一个元素
void reallocate(size_type n = 1);
//! @brief 内存拷贝函数
//! @param[out] dest 目标内存
//! @param[in] src 源内存
//! @return void * 返回目标内存末地址
void * memcpy(void * dest, void const * src, size_type n)
{
char * p = (char *) dest;
char const * q = (char *) src;
for (size_type i = 0; i < n; ++i)
*p++ = *q++;
return p;
}
//! @brief 逆向内存拷贝函数
//! @param[out] dest 目标内存
//!@param[in] src 源内存
//! @return void * 返回目标内存逆向末地址
void * resMemCpy(void * dest, void const * src, size_type n)
{
char * p = (char *) dest;
char const * q = (char *) src;
for (size_type i = 0; i < n; ++i)
*p-- = *q--;
return p;
}
void swap(T * & s1, T * & s2)
{
T * tmp = s1;
s1 = s2;
s2 = tmp;
}
};
//! @file Vector.h
//! @brief 实现stl中vector类的所有接口\n参考:
//! - <c++ primer> 第四版 18.1.2 allocator类
//! - <泛型编程与stl> 侯捷译 16.1.1 vector
//! - http://www.cplusplus.com/reference/stl/vector/
//! - <c++ Templates: The Complete Guide>, Davide Vandevoorde, Nicolai M. Josuttis
#ifndef VECTOR_H
#define VECTOR_H
#include "Allocator.h"
#include "Iterator.h"
class OutOfRange {
};
class LengthError {
};
template <class T>
class VectorIterator : public Iterator<RandomIteratorTag, T>
{
T * p;
public:
typedef typename Iterator<RandomIteratorTag, T>::difference_type difference_type;
VectorIterator() : p(0) {}
VectorIterator( T * x) : p(x) {}
VectorIterator(const VectorIterator & b) : p(b.p) {}
VectorIterator & operator=(const VectorIterator & b) { p = b.p; return *this;}
bool operator== (const VectorIterator & b) { return (p == b.p); }
bool operator!= (const VectorIterator & b) { return !operator==(b); }
T& operator* ()
{
if (p)
return *p;
throw OutOfRange();
}
T * operator->()
{
if (p)
return p;
throw OutOfRange();
}
VectorIterator& operator++()
{
++p;
return *this;
}
VectorIterator& operator--()
{
--p;
return *this;
}
VectorIterator operator++(int)
{
VectorIterator tmp(*this);
operator++();
return tmp;
}
VectorIterator operator--(int)
{
VectorIterator tmp(*this);
operator--();
return tmp;
}
VectorIterator operator+(int d) const {return p + d;}
VectorIterator operator-(int d) const {return p - d;}
difference_type operator-(const VectorIterator & it)
{ return p - it.p; }
bool operator>(const VectorIterator & rhs) { return p > rhs.p; }
bool operator<=(const VectorIterator & rhs) { return !operator>(rhs);}
bool operator<(const VectorIterator & rhs) { return p < rhs.p; }
bool operator>=(const VectorIterator & rhs) { return !operator<(rhs); }
};
template <typename T>
class VectorReverseIterator : public Iterator<RandomIteratorTag, T>
{
T * p;
public:
typedef typename Iterator<RandomIteratorTag, T>::difference_type difference_type;
VectorReverseIterator() : p(0) {}
VectorReverseIterator( T * x) : p(x) {}
VectorReverseIterator(const VectorReverseIterator & b) : p(b.p) {}
VectorReverseIterator & operator=(const VectorReverseIterator & b) { p = b.p; return *this;}
bool operator== (const VectorReverseIterator & b) { return (p == b.p); }
bool operator!= (const VectorReverseIterator & b) { return !operator==(b); }
T& operator* ()
{
if (p)
return *p;
throw OutOfRange();
}
T * operator->()
{
if (p)
return p;
throw OutOfRange();
}
VectorReverseIterator& operator++()
{
--p;
return *this;
}
VectorReverseIterator& operator--()
{
++p;
return *this;
}
VectorReverseIterator operator++(int)
{
VectorReverseIterator tmp(*this);
operator++();
return tmp;
}
VectorReverseIterator operator--(int)
{
VectorReverseIterator tmp(*this);
operator--();
return tmp;
}
VectorReverseIterator operator+(int d) const {return p - d;}
VectorReverseIterator operator-(int d) const {return p + d;}
difference_type operator-(const VectorReverseIterator & it)
{ return it.p - p; }
bool operator>(const VectorReverseIterator & rhs) { return p < rhs.p; }
bool operator<=(const VectorReverseIterator & rhs) { return !operator>(rhs);}
bool operator<(const VectorReverseIterator & rhs) { return p > rhs.p; }
bool operator>=(const VectorReverseIterator & rhs) { return !operator<(rhs); }
};
// reallocate()
// 下面代码来自<c++ primer>第四版 18.1.2 allocator类
template <typename T, typename Alloc>
void Vector<T, Alloc>::reallocate(size_type n)
{
size_type size = first_free - head;
size_type newcapacity = 2 * (size > 0 ? size : 1);
if (newcapacity < n)
newcapacity = n;
pointer newhead = alloc.allocate(newcapacity);
alloc.uninitialized_copy(head, first_free, newhead);
for (pointer p = first_free; p != head; /* empty */)
alloc.destroy(--p);
if (head)
alloc.deallocate(head, tail - head);
head = newhead;
first_free = head + size;
tail = head + newcapacity;
}
template <typename T, typename Alloc>
template <typename InputIterator>
void Vector<T, Alloc>::assign (InputIterator first, InputIterator last)
{
size_type size = last - first;
size_type capacity = tail - head;
for (pointer p = first_free; p != head; /* empty */)
alloc.destroy(--p);
if (size > capacity) {
size_type newcapacity = 2 * (size > 1 ? size : 1);
if (head)
alloc.deallocate(head, capacity);
head = alloc.allocate(newcapacity);
tail = head + newcapacity;
}
alloc.uninitialized_copy(first, last, head);
first_free = head + size;
}
template<typename T, typename Alloc>
void Vector<T, Alloc>::assign (size_type n, const T& u)
{
size_type capacity = tail - head;
for (pointer p = first_free; p != head; /* empty */)
alloc.destroy(--p);
if (n > capacity) {
size_type newcapacity = 2 * (n > 1 ? n : 1);
if (head)
alloc.deallocate(head, tail - head);
head = alloc.allocate(newcapacity);
tail = head + newcapacity;
}
alloc.uninitialized_fill(head, head + n, u);
first_free = head + n;
}
template<typename T, typename Alloc>
Vector<T, Alloc>::Vector( size_type n, const T& value) : head(0), first_free(0), tail(0)
{
assign(n , value);
}
template<typename T, typename Alloc>
template <class InputIterator>
Vector<T, Alloc>::Vector ( InputIterator first, InputIterator last): head(0), first_free(0), tail(0)
{
assign(first, last);
}
template<typename T, typename Alloc>
Vector<T, Alloc>::Vector ( const Vector<T,Alloc>& x ) : head(0), first_free(0), tail(0)
{
assign(x.begin(), x.end());
}
template<typename T, typename Alloc>
void Vector<T, Alloc>::clear()
{
// assign((size_type)0, T());
assign(pointer(0), pointer(0));
}
template<typename T, typename Alloc>
void Vector<T, Alloc>::resize ( size_type sz, T c )
{
if (sz < size())
{
for(; first_free != head + sz; )
alloc.destroy(--first_free);
} else if (sz > size())
{
reallocate();
for(; first_free != head + sz; )
alloc.construct(first_free++, c);
}
}
template<typename T, typename Alloc>
void Vector<T, Alloc>::reserve ( size_type n )
{
if (n > max_size())
throw LengthError();
else if (n > capacity())
reallocate(n);
}
template<typename T, typename Alloc>
Vector<T, Alloc>& Vector<T, Alloc>::operator= (const Vector<T, Alloc>& x)
{
assign(x.begin(), x.end());
return *this;
}
template<typename T, typename Alloc>
VectorIterator<T> Vector<T,Alloc>::insert ( iterator position, const T& x )
{
size_type len = position - head;
if (size() + 1 > capacity())
reallocate();
resMemCpy(first_free, first_free - 1, (first_free - (head + len)) * sizeof(T));
alloc.construct(head + len, x);
first_free++;
return (head + len);
}
template<typename T, typename Alloc>
void Vector<T,Alloc>::insert ( iterator position, size_type n, const T& x )
{
size_type len = position - head;
if (size() + n > capacity())
reallocate();
resMemCpy(first_free + (n - 1), first_free - 1, (first_free - (head + len)) * sizeof(T));
for (size_type i = 0; i < n; ++i)
alloc.construct(head + len + i, x);
first_free += n;
}
template<typename T, typename Alloc>
template <class InputIterator>
void Vector<T,Alloc>::insert ( iterator position, InputIterator first, InputIterator last )
{
size_type n = last - first;
size_type len = position -head;
if (size() + n > capacity())
reallocate();
resMemCpy(first_free + (n - 1), first_free - 1, (first_free - (head + len)) * sizeof(T));
for (size_type i = 0; i < n; ++i)
alloc.construct(head + len + i, *first++);
first_free += n;
}
template<typename T, typename Alloc>
typename Vector<T, Alloc>::iterator Vector<T, Alloc>::erase ( iterator position )
{
alloc.destroy(&*position);
size_type num = (end() - position) - 1;
num = num > 0 ? (num * sizeof(T)) : 0;
if (num > 0)
{
memcpy(&*position, &*(position + 1), num );
first_free--;
}
return position;
}
template<typename T, typename Alloc>
typename Vector<T, Alloc>::iterator Vector<T, Alloc>::erase ( iterator first, iterator last )
{
alloc.destroy(first, last);
size_type num = (end() - last);
if (num > 0)
{
memcpy(&*first, &*last, num * sizeof(T) );
first_free = &*first + num;
}
return first;
}
template<typename T, typename Alloc>
void Vector<T, Alloc>::swap ( Vector<T,Alloc>& vec )
{
swap(head, vec.head);
swap(first_free, vec.first_free);
swap(tail, vec.tail);
}
// Equal函数模板代码来源: http://www.cplusplus.com/reference/algorithm/equal/
template <class InputIterator1, class InputIterator2>
bool Equal ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 )
{
while ( first1!=last1 )
{
if (*first1 != *first2) // or: if (!pred(*first1,*first2)), for pred version
return false;
++first1; ++first2;
}
return true;
}
// Lexicographical_compare函数模板的代码来源于: http://www.cplusplus.com/reference/algorithm/lexicographical_compare/
template <class InputIterator1, class InputIterator2>
bool Lexicographical_compare ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2 )
{
while (first1!=last1)
{
if (first2==last2 || *first2<*first1) return false;
else if (*first1<*first2) return true;
first1++; first2++;
}
return (first2!=last2);
}
template <class T, class Alloc>
bool operator== ( const vector<T,Alloc>& x, const vector<T,Alloc>& y )
{
if (x.size() != y.size())
return false;
return ( Equal(x.begin(), x.end(), y.begin() ) );
}
template <class T, class Alloc>
bool operator< ( const vector<T,Alloc>& x, const vector<T,Alloc>& y )
{
return Lexicographical_compare( x.begin(), x.end(), y.begin(), y.end() );
}
template <class T, class Alloc>
bool operator!= ( const vector<T,Alloc>& x, const vector<T,Alloc>& y )
{
return !operator==(x, y);
}
template <class T, class Alloc>
bool operator> ( const vector<T,Alloc>& x, const vector<T,Alloc>& y )
{
return operator<(y, x);
}
template <class T, class Alloc>
bool operator>= ( const vector<T,Alloc>& x, const vector<T,Alloc>& y )
{
return !operator<(x, y);
}
template <class T, class Alloc>
bool operator<= ( const vector<T,Alloc>& x, const vector<T,Alloc>& y )
{
return !operator>(x, y);
}
#endif