/*
 * Copyright (c) 1999 Johnny C. Lam.
 * All rights reserved.
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  The author makes no representations
 * about the suitability of this software for any purpose.  It is
 * provided "as is" without express or implied warranty.
 */

/* -*- C++ -*- */

template <typename T>
__const_auto_iterator<T>::__const_auto_iterator()
    : pp(0)
{}

template <typename T>
__const_auto_iterator<T>::__const_auto_iterator(const auto_ptr<T>* pp_)
    : pp(pp_)
{}

template <typename T>
bool
__const_auto_iterator<T>::operator!=(const self& it) const
{
    return it.pp != pp;
}

template <typename T>
__const_auto_iterator<T>::difference_type
__const_auto_iterator<T>::operator-(const self& it) const
{
    return pp - it.pp;
}

template <typename T>
__const_auto_iterator<T>
__const_auto_iterator<T>::operator++(int)
{
    return pp++;
}

template <typename T>
__const_auto_iterator<T>
__const_auto_iterator<T>::operator++()
{
    return ++pp;
}

template <typename T>
__const_auto_iterator<T>
__const_auto_iterator<T>::operator+=(difference_type n)
{
    return pp += n;
}

template <typename T>
__const_auto_iterator<T>
__const_auto_iterator<T>::operator+(difference_type n) const
{
    self tmp = *this;
    return tmp += n;
}

template <typename T>
__const_auto_iterator<T>
__const_auto_iterator<T>::operator--(int)
{
    return pp--;
}

template <typename T>
__const_auto_iterator<T>
__const_auto_iterator<T>::operator--()
{
    return --pp;
}

template <typename T>
__const_auto_iterator<T>
__const_auto_iterator<T>::operator-=(difference_type n)
{
    return (pp -= n);
}

template <typename T>
__const_auto_iterator<T>
__const_auto_iterator<T>::operator-(difference_type n) const
{
    self tmp = *this;
    return tmp -= n;
}

template <typename T>
const T*
__const_auto_iterator<T>::operator*()
{
    return pp->get();
}

template <typename T>
const T*
__const_auto_iterator<T>::operator->()
{
    return pp->get();
}

template <typename T>
inline
auto_vector<T>::~auto_vector()
{
    delete [] array;
}

template <typename T>
inline
auto_vector<T>::auto_vector(size_type capacity_)
    : array(0), array_end(0), capacity(0)
{
    if (capacity_ != 0)
	array = new auto_ptr<T>[capacity_];
    capacity = capacity_;
}

template <typename T>
inline
const T*
auto_vector<T>::operator[](size_type i) const
{
    return array[i].get();
}

template <typename T>
inline
T*
auto_vector<T>::operator[](size_type i)
{
    return array[i].get();
}

template <typename T>
inline
void
auto_vector<T>::assign(size_type i, auto_ptr<T>& p)
{
    array[i] = p;
}

template <typename T>
inline
void
auto_vector<T>::assign_direct(size_type i, T* p)
{
    array[i].reset(p);
}

template <typename T>
inline
void
auto_vector<T>::push_back(auto_ptr<T>& p)
{
    if (array_end == capacity)
	grow(capacity + 1);
    array[array_end++] = p;
}

template <typename T>
inline
auto_ptr<T>
auto_vector<T>::pop_back()
{
    return array[--array_end];
}

template <typename T>
inline
auto_vector<T>::const_iterator
auto_vector<T>::begin() const
{
    return array;
}

template <typename T>
inline
auto_vector<T>::const_iterator
auto_vector<T>::end() const
{
    return array + array_end;
}

template <typename T>
inline
auto_vector<T>::size_type
auto_vector<T>::size() const
{
    return array_end;
}
