/*! \file pideque.h
 * \brief Dynamic array of any type
 *
 * This file declares PIDeque
*/
/*
	PIP - Platform Independent Primitives
	Dynamic array of any type
	Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef PIDEQUE_H
#define PIDEQUE_H

#include "picontainers.h"


template <typename T>
class PIDeque {
public:
	inline PIDeque(): pid_data(0), pid_size(0), pid_rsize(0), pid_start(0) {
		PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T))
	}
	inline PIDeque(const PIDeque<T> & other): pid_data(0), pid_size(0), pid_rsize(0), pid_start(0) {
		PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T))
		alloc(other.pid_size, true);
		newT(pid_data + pid_start, other.pid_data + other.pid_start, pid_size);
	}
	inline PIDeque(std::initializer_list<T> init_list): pid_data(0), pid_size(0), pid_rsize(0), pid_start(0) {
		PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T))
		alloc(init_list.size(), true);
		newT(pid_data, init_list.begin(), init_list.size());
	}
	inline PIDeque(const T * data, size_t size): pid_data(0), pid_size(0), pid_rsize(0), pid_start(0) {
		PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T))
		alloc(size, true);
		newT(pid_data + pid_start, data, pid_size);
	}
	inline PIDeque(size_t pid_size, const T & f = T()): pid_data(0), pid_size(0), pid_rsize(0), pid_start(0) {
		PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T))
		resize(pid_size, f);
	}
	inline PIDeque(PIDeque<T> && other): pid_data(other.pid_data), pid_size(other.pid_size), pid_rsize(other.pid_rsize), pid_start(other.pid_start) {
		PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T))
		other._reset();
	}
	inline virtual ~PIDeque() {
		PIINTROSPECTION_CONTAINER_DELETE(T)
		PIINTROSPECTION_CONTAINER_FREE(T, (pid_rsize))
		deleteT(pid_data + pid_start, pid_size);
		dealloc();
		_reset();
	}

	inline PIDeque<T> & operator =(const PIDeque<T> & other) {
		if (this == &other) return *this;
		deleteT(pid_data + pid_start, pid_size);
		alloc(other.pid_size, true);
		newT(pid_data + pid_start, other.pid_data + other.pid_start, pid_size);
		return *this;
	}

	inline PIDeque<T> & operator =(PIDeque<T> && other) {
		swap(other);
		return *this;
	}

	typedef T value_type;

	class iterator {
		friend class PIDeque<T>;
	private:
		inline iterator(PIDeque<T> * v, size_t p): parent(v), pos(p) {}
		PIDeque<T> * parent;
		size_t pos;
	public:
		inline iterator(): parent(0), pos(0) {}
		inline T & operator *() {return (*parent)[pos];}
		inline const T & operator *() const {return (*parent)[pos];}
		inline void operator ++() {++pos;}
		inline void operator ++(int) {++pos;}
		inline void operator --() {--pos;}
		inline void operator --(int) {--pos;}
		inline bool operator ==(const iterator & it) const {return (pos == it.pos);}
		inline bool operator !=(const iterator & it) const {return (pos != it.pos);}
	};

	class const_iterator {
		friend class PIDeque<T>;
	private:
		inline const_iterator(const PIDeque<T> * v, size_t p): parent(v), pos(p) {}
		const PIDeque<T> * parent;
		size_t pos;
	public:
		inline const_iterator(): parent(0), pos(0) {}
		inline const T & operator *() const {return (*parent)[pos];}
		inline void operator ++() {++pos;}
		inline void operator ++(int) {++pos;}
		inline void operator --() {--pos;}
		inline void operator --(int) {--pos;}
		inline bool operator ==(const const_iterator & it) const {return (pos == it.pos);}
		inline bool operator !=(const const_iterator & it) const {return (pos != it.pos);}
	};

	class reverse_iterator {
		friend class PIDeque<T>;
	private:
		inline reverse_iterator(PIDeque<T> * v, size_t p): parent(v), pos(p) {}
		PIDeque<T> * parent;
		size_t pos;
	public:
		inline reverse_iterator(): parent(0), pos(0) {}
		inline T & operator *() {return (*parent)[pos];}
		inline const T & operator *() const {return (*parent)[pos];}
		inline void operator ++() {--pos;}
		inline void operator ++(int) {--pos;}
		inline void operator --() {++pos;}
		inline void operator --(int) {++pos;}
		inline bool operator ==(const reverse_iterator & it) const {return (pos == it.pos);}
		inline bool operator !=(const reverse_iterator & it) const {return (pos != it.pos);}
	};

	class const_reverse_iterator {
		friend class PIDeque<T>;
	private:
		inline const_reverse_iterator(const PIDeque<T> * v, size_t p): parent(v), pos(p) {}
		const PIDeque<T> * parent;
		size_t pos;
	public:
		inline const_reverse_iterator(): parent(0), pos(0) {}
		inline const T & operator *() const {return (*parent)[pos];}
		inline void operator ++() {--pos;}
		inline void operator ++(int) {--pos;}
		inline void operator --() {++pos;}
		inline void operator --(int) {++pos;}
		inline bool operator ==(const const_reverse_iterator & it) const {return (pos == it.pos);}
		inline bool operator !=(const const_reverse_iterator & it) const {return (pos != it.pos);}
	};

	inline iterator begin() {return iterator(this, 0);}
	inline iterator end() {return iterator(this, pid_size);}
	inline const_iterator begin() const {return const_iterator(this, 0);}
	inline const_iterator end() const {return const_iterator(this, pid_size);}
	inline reverse_iterator rbegin() {return reverse_iterator(this, pid_size - 1);}
	inline reverse_iterator rend() {return reverse_iterator(this, -1);}
	inline const_reverse_iterator rbegin() const {return const_reverse_iterator(this, pid_size - 1);}
	inline const_reverse_iterator rend() const {return const_reverse_iterator(this, -1);}

	inline size_t size() const {return pid_size;}
	inline ssize_t size_s() const {return pid_size;}
	inline size_t length() const {return pid_size;}
	inline size_t capacity() const {return pid_rsize;}
	inline size_t _start() const {return pid_start;}
	inline bool isEmpty() const {return (pid_size == 0);}

	inline T & operator [](size_t index) {return pid_data[pid_start + index];}
	inline const T & operator [](size_t index) const {return pid_data[pid_start + index];}
	inline const T & at(size_t index) const {return pid_data[pid_start + index];}
	inline T & back() {return pid_data[pid_start + pid_size - 1];}
	inline const T & back() const {return pid_data[pid_start + pid_size - 1];}
	inline T & front() {return pid_data[pid_start];}
	inline const T & front() const {return pid_data[pid_start];}
	inline bool operator ==(const PIDeque<T> & t) const {
		if (pid_size != t.pid_size) return false;
		for (size_t i = 0; i < pid_size; ++i)
			if (t[i] != (*this)[i])
				return false;
		return true;
	}
	inline bool operator !=(const PIDeque<T> & t) const {return !(*this == t);}
	inline bool operator >(const PIDeque<T> & t) const {
		if (pid_size != t.pid_size) return pid_size > t.pid_size;
		for (size_t i = 0; i < pid_size; ++i)
			if (t[i] != (*this)[i]) return t[i] > (*this)[i];
		return false;
	}
	inline bool contains(const T & v) const {
		for (size_t i = pid_start; i < pid_start + pid_size; ++i)
			if (v == pid_data[i])
				return true;
		return false;
	}
	inline int etries(const T & v) const {
		int ec = 0;
		for (size_t i = pid_start; i < pid_start + pid_size; ++i)
			if (v == pid_data[i]) ++ec;
		return ec;
	}
	inline ssize_t indexOf(const T & v) const {
		for (ssize_t i = pid_start; i < pid_start + (ssize_t)pid_size; ++i)
			if (v == pid_data[i])
				return i - pid_start;
		return -1;
	}
	inline ssize_t lastIndexOf(const T & v) const {
		for (ssize_t i = pid_start + (ssize_t)pid_size - 1; i >= pid_start; --i)
			if (v == pid_data[i])
				return i - pid_start;
		return -1;
	}

	inline T * data(size_t index = 0) {return &(pid_data[pid_start + index]);}
	inline const T * data(size_t index = 0) const {return &(pid_data[pid_start + index]);}

	template<typename T1 = T, typename std::enable_if<
				 !std::is_trivially_copyable<T1>::value
				 , int>::type = 0>
	inline PIDeque<T> & clear() {
		resize(0);
		return *this;
	}
	template<typename T1 = T, typename std::enable_if<
				 std::is_trivially_copyable<T1>::value
				 , int>::type = 0>
	inline PIDeque<T> & clear() {
		PIINTROSPECTION_CONTAINER_UNUSED(T, pid_size)
		pid_size = 0;
		return *this;
	}

	inline PIDeque<T> & fill(const T & f = T()) {
		deleteT(pid_data + pid_start, pid_size);
		PIINTROSPECTION_CONTAINER_USED(T, pid_size)
		for (size_t i = pid_start; i < pid_start + pid_size; ++i)
			elementNew(pid_data + i, f);
		return *this;
	}
	inline PIDeque<T> & assign(const T & f = T()) {return fill(f);}
	template<typename T1 = T, typename std::enable_if<
				 !std::is_trivially_copyable<T1>::value
				 , int>::type = 0>
	inline PIDeque<T> & assign(size_t new_size, const T & f) {
		resize(new_size);
		return fill(f);
	}
	template<typename T1 = T, typename std::enable_if<
				 std::is_trivially_copyable<T1>::value
				 , int>::type = 0>
	inline PIDeque<T> & assign(size_t new_size, const T & f) {
		_resizeRaw(new_size);
		return fill(f);
	}

	inline PIDeque<T> & resize(size_t new_size, const T & f = T()) {
		if (new_size < pid_size) {
			deleteT(&(pid_data[new_size + pid_start]), pid_size - new_size);
			pid_size = new_size;
		}
		if (new_size > pid_size) {
			size_t os = pid_size;
			alloc(new_size, true);
			PIINTROSPECTION_CONTAINER_USED(T, (new_size-os))
			for (size_t i = os + pid_start; i < new_size + pid_start; ++i) elementNew(pid_data + i, f);
		}
		return *this;
	}

	template<typename T1 = T, typename std::enable_if<
				 std::is_trivially_copyable<T1>::value
				 , int>::type = 0>
	inline PIDeque<T> & _resizeRaw(size_t new_size) {
		if (new_size > pid_size) {
			PIINTROSPECTION_CONTAINER_USED(T, (new_size-pid_size));
		}
		if (new_size < pid_size) {
			PIINTROSPECTION_CONTAINER_UNUSED(T, (pid_size-new_size));
		}
		alloc(new_size, true);
		return *this;
	}

	inline PIDeque<T> & reserve(size_t new_size) {
		if (new_size <= pid_rsize) return *this;
		size_t os = pid_size;
		alloc(new_size, true);
		pid_size = os;
		return *this;
	}

	inline PIDeque<T> & insert(size_t index, const T & v = T()) {
		bool dir = pid_rsize <= 2 ? true : (index >= pid_rsize / 2 ? true : false);
		if (dir) {
			alloc(pid_size + 1, true);
			if (index < pid_size - 1) {
				size_t os = pid_size - index - 1;
				memmove((void*)(&(pid_data[index + pid_start + 1])), (const void*)(&(pid_data[index + pid_start])), os * sizeof(T));
			}
		} else {
			alloc(pid_size + 1, false, -1);
			if (index > 0)
				memmove((void*)(&(pid_data[pid_start])), (const void*)(&(pid_data[pid_start + 1])), index * sizeof(T));
		}
		PIINTROSPECTION_CONTAINER_USED(T, 1)
		elementNew(pid_data + pid_start + index, v);
		return *this;
	}
	inline PIDeque<T> & insert(size_t index, T && v) {
		bool dir = pid_rsize <= 2 ? true : (index >= pid_rsize / 2 ? true : false);
		if (dir) {
			alloc(pid_size + 1, true);
			if (index < pid_size - 1) {
				size_t os = pid_size - index - 1;
				memmove((void*)(&(pid_data[index + pid_start + 1])), (const void*)(&(pid_data[index + pid_start])), os * sizeof(T));
			}
		} else {
			alloc(pid_size + 1, false, -1);
			if (index > 0)
				memmove((void*)(&(pid_data[pid_start])), (const void*)(&(pid_data[pid_start + 1])), index * sizeof(T));
		}
		PIINTROSPECTION_CONTAINER_USED(T, 1)
		elementNew(pid_data + pid_start + index, std::move(v));
		return *this;
	}
	inline PIDeque<T> & insert(size_t index, const PIDeque<T> & other) {
		if (other.isEmpty()) return *this;
		assert(&other != this);
		bool dir = pid_rsize <= 2 ? true : (index >= pid_rsize / 2 ? true : false);
		if (dir) {
			ssize_t os = pid_size - index;
			alloc(pid_size + other.pid_size, true);
			if (os > 0)
				memmove((void*)(&(pid_data[index + pid_start + other.pid_size])), (const void*)(&(pid_data[index + pid_start])), os * sizeof(T));
		} else {
			alloc(pid_size + other.pid_size, false, -other.pid_size);
			if (index > 0)
				memmove((void*)(&(pid_data[pid_start])), (const void*)(&(pid_data[pid_start + other.pid_size])), index * sizeof(T));
		}
		newT(pid_data + pid_start + index, other.pid_data + other.pid_start, other.pid_size);
		return *this;
	}

	inline PIDeque<T> & remove(size_t index, size_t count = 1) {
		if (count == 0) return *this;
		if (index + count >= pid_size) {
			resize(index);
			return *this;
		}
		size_t os = pid_size - index - count;
		deleteT(&(pid_data[index + pid_start]), count);
		if (os <= index) {
			if (os > 0) memmove((void*)(&(pid_data[index + pid_start])), (const void*)(&(pid_data[index + pid_start + count])), os * sizeof(T));
		} else {
			if (index > 0) memmove((void*)(&(pid_data[pid_start + count])), (const void*)(&(pid_data[pid_start])), index * sizeof(T));
			pid_start += count;
		}
		pid_size -= count;
		return *this;
	}

	inline void swap(PIDeque<T> & other) {
		piSwap<T*>(pid_data, other.pid_data);
		piSwap<size_t>(pid_size, other.pid_size);
		piSwap<size_t>(pid_rsize, other.pid_rsize);
		piSwap<ssize_t>(pid_start, other.pid_start);
	}

	typedef int (*CompareFunc)(const T * , const T * );
	static int compare_func(const T * t0, const T * t1) {return (*t0) < (*t1) ? -1 : ((*t0) == (*t1) ? 0 : 1);}
	inline PIDeque<T> & sort(CompareFunc compare = compare_func) {
		piqsort(pid_data + pid_start, pid_size, sizeof(T), (int(*)(const void * , const void * ))compare);
		return *this;
	}

	inline PIDeque<T> & enlarge(llong pid_size) {
		llong ns = size_s() + pid_size;
		if (ns <= 0) clear();
		else resize(size_t(ns));
		return *this;
	}

	inline PIDeque<T> & removeOne(const T & v) {
		for (size_t i = 0; i < pid_size; ++i)
			if (pid_data[i + pid_start] == v) {
				remove(i);
				return *this;
			}
		return *this;
	}
	inline PIDeque<T> & removeAll(const T & v) {
		for (ssize_t i = 0; i < ssize_t(pid_size); ++i)
			if (pid_data[i + pid_start] == v) {
				remove(i);
				--i;
			}
		return *this;
	}

	inline PIDeque<T> & push_back(const T & v) {
		alloc(pid_size + 1, true);
		PIINTROSPECTION_CONTAINER_USED(T, 1);
		elementNew(pid_data + pid_start + pid_size - 1, v);
		return *this;
	}
	inline PIDeque<T> & push_back(T && v) {
		alloc(pid_size + 1, true);
		PIINTROSPECTION_CONTAINER_USED(T, 1);
		elementNew(pid_data + pid_start + pid_size - 1, std::move(v));
		return *this;
	}
	inline PIDeque<T> & append(const T & v) {return push_back(v);}
	inline PIDeque<T> & append(T && v) {return push_back(std::move(v));}
	inline PIDeque<T> & append(const PIDeque<T> & t) {
		assert(&t != this);
		size_t ps = pid_size;
		alloc(pid_size + t.pid_size, true);
		newT(pid_data + ps + pid_start, t.pid_data + t.pid_start, t.pid_size);
		return *this;
	}
	inline PIDeque<T> & operator <<(const T & v) {return push_back(v);}
	inline PIDeque<T> & operator <<(T && v) {return push_back(std::move(v));}
	inline PIDeque<T> & operator <<(const PIDeque<T> & t) {return append(t);}

	inline PIDeque<T> & push_front(const T & v) {insert(0, v); return *this;}
	inline PIDeque<T> & push_front(T && v) {insert(0, std::move(v)); return *this;}
	inline PIDeque<T> & prepend(const T & v) {return push_front(v);}
	inline PIDeque<T> & prepend(T && v) {return push_front(std::move(v));}

	inline PIDeque<T> & pop_back() {if (pid_size == 0) return *this; resize(pid_size - 1); return *this;}
	inline PIDeque<T> & pop_front() {if (pid_size == 0) return *this; remove(0); return *this;}

	inline T take_back() {T t(back()); pop_back(); return t;}
	inline T take_front() {T t(front()); pop_front(); return t;}

	template <typename ST>
	PIDeque<ST> toType() const {
		PIDeque<ST> ret(pid_size);
		for (uint i = 0; i < pid_size; ++i)
			ret[i] = ST(pid_data[i + pid_start]);
		return ret;
	}

	const PIDeque<T> & forEach(std::function<void(const T &)> f) const {
		for (uint i = 0; i < pid_size; ++i)
			f(pid_data[i + pid_start]);
		return *this;
	}
	PIDeque<T> copyForEach(std::function<T(const T &)> f) const {
		PIDeque<T> ret; ret.reserve(pid_size);
		for (uint i = 0; i < pid_size; ++i)
			ret << f(pid_data[i + pid_start]);
		return ret;
	}
	PIDeque<T> & forEachInplace(std::function<T(const T &)> f) {
		for (uint i = 0; i < pid_size; ++i)
			pid_data[i + pid_start] = f(pid_data[i + pid_start]);
		return *this;
	}
	template <typename ST>
	PIDeque<ST> toType(std::function<ST(const T &)> f) const {
		PIDeque<ST> ret; ret.reserve(pid_size);
		for (uint i = 0; i < pid_size; ++i)
			ret << f(pid_data[i + pid_start]);
		return ret;
	}

private:
	inline void _reset() {pid_size = pid_rsize = pid_start = 0; pid_data = 0;}
	inline size_t asize(ssize_t s) {
		if (s <= 0) return 0;
		if (pid_rsize + pid_rsize >= size_t(s) && pid_rsize < size_t(s))
			return pid_rsize + pid_rsize;
		ssize_t t = 0, s_ = s - 1;
		while (s_ >> t)
			++t;
		return (1 << t);
	}
	template<typename T1 = T, typename std::enable_if<
			!std::is_trivially_copyable<T1>::value
			, int>::type = 0>
	inline void newT(T * dst, const T * src, size_t s) {
		PIINTROSPECTION_CONTAINER_USED(T, s)
		for (size_t i = 0; i < s; ++i)
			elementNew(dst + i, src[i]);
	}
	template<typename T1 = T, typename std::enable_if<
				 std::is_trivially_copyable<T1>::value
				 , int>::type = 0>
	inline void newT(T * dst, const T * src, size_t s) {
		PIINTROSPECTION_CONTAINER_USED(T, s)
		memcpy((void*)(dst), (const void*)(src), s * sizeof(T));
	}
	template<typename T1 = T, typename std::enable_if<
			!std::is_trivially_copyable<T1>::value
			, int>::type = 0>
	inline void deleteT(T * d, size_t sz) {
		PIINTROSPECTION_CONTAINER_UNUSED(T, sz)
		if ((uchar*)d != 0) {
			for (size_t i = 0; i < sz; ++i)
				elementDelete(d[i]);
		}
	}
	template<typename T1 = T, typename std::enable_if<
				 std::is_trivially_copyable<T1>::value
				 , int>::type = 0>
	inline void deleteT(T * d, size_t sz) {
		PIINTROSPECTION_CONTAINER_UNUSED(T, sz)
	}
	template<typename T1 = T, typename std::enable_if<
				 !std::is_trivially_copyable<T1>::value
				 , int>::type = 0>
	inline void elementNew(T * to, const T & from) {new(to)T(from);}
	template<typename T1 = T, typename std::enable_if<
				 !std::is_trivially_copyable<T1>::value
				 , int>::type = 0>
	inline void elementNew(T * to, T && from) {new(to)T(std::move(from));}
	template<typename T1 = T, typename std::enable_if<
				 std::is_trivially_copyable<T1>::value
				 , int>::type = 0>
	inline void elementNew(T1 * to, const T & from) {(*to) = from;}
	template<typename T1 = T, typename std::enable_if<
				 std::is_trivially_copyable<T1>::value
				 , int>::type = 0>
	inline void elementNew(T * to, T && from) {(*to) = std::move(from);}
	template<typename T1 = T, typename std::enable_if<
				 !std::is_trivially_copyable<T1>::value
				 , int>::type = 0>
	inline void elementDelete(T & from) {from.~T();}
	template<typename T1 = T, typename std::enable_if<
				 std::is_trivially_copyable<T1>::value
				 , int>::type = 0>
	inline void elementDelete(T & from) {}
	inline void dealloc() {
		if ((uchar*)pid_data != 0) free((uchar*)pid_data);
		pid_data = 0;
	}
	inline void checkMove(bool direction) {
		if (pid_size >= 4) {
			if (pid_size < pid_rsize / 6) {
				if (pid_start < ssize_t(pid_size + pid_size) || pid_start > (ssize_t(pid_rsize) - ssize_t(pid_size) - ssize_t(pid_size))) {
					ssize_t ns = (pid_rsize - pid_size) / 2;
					if (pid_start != ns) {
						memmove((void*)(pid_data + ns), (const void*)(pid_data + pid_start), pid_size * sizeof(T));
						pid_start = ns;
					}
				}
			}
		} else {
			ssize_t ns = (pid_rsize - pid_size) / 2;
			if (pid_start != ns) {
				memmove((void*)(pid_data + ns), (const void*)(pid_data + pid_start), pid_size * sizeof(T));
				pid_start = ns;
			}
		}
	}
	inline void alloc(size_t new_size, bool direction, ssize_t start_offset = 0) { // direction == true -> alloc forward
		if (direction) {
			if (pid_start + new_size <= pid_rsize) {
				pid_size = new_size;
				checkMove(direction);
				return;
			}
			pid_size = new_size;
			size_t as = asize(pid_start + new_size);
			if (as != pid_rsize) {
				PIINTROSPECTION_CONTAINER_ALLOC(T, (as-pid_rsize))
				T * p_d = (T*)(realloc((void*)(pid_data), as*sizeof(T)));
				assert(p_d);
				pid_data = p_d;
				pid_rsize = as;
			}
		} else {
			size_t as;
			if (pid_start + start_offset < 0)
				as = asize(pid_rsize - start_offset);
			else as = pid_rsize;

			if (as > pid_rsize) {
				T * td = (T*)(malloc(as * sizeof(T)));
				ssize_t ns = pid_start + as - pid_rsize;
				PIINTROSPECTION_CONTAINER_ALLOC(T, (as-pid_rsize))
				if (pid_rsize > 0 && pid_data != 0) {
					memcpy((void*)(td + ns), (const void*)(pid_data + pid_start), pid_size * sizeof(T));
					dealloc();
				}
				pid_data = td;
				pid_rsize = as;
				pid_start = ns;
			}
			pid_start += start_offset;
			pid_size = new_size;
			checkMove(direction);
		}
	}

	T * pid_data;
	size_t pid_size, pid_rsize;
	ssize_t pid_start;
};


#ifdef PIP_STD_IOSTREAM
template<typename T>
inline std::ostream & operator <<(std::ostream & s, const PIDeque<T> & v) {s << "{"; for (size_t i = 0; i < v.size(); ++i) {s << v[i]; if (i < v.size() - 1) s << ", ";} s << "}"; return s;}
#endif

template<typename T>
inline PICout operator <<(PICout s, const PIDeque<T> & v) {
	s.space();
	s.setControl(0, true);
	s << "{";
	for (size_t i = 0; i < v.size(); ++i) {
		s << v[i];
		if (i < v.size() - 1)
			s << ", ";
	}
	s << "}";
	s.restoreControl();
	return s;
}

template<typename T> inline void piSwap(PIDeque<T> & f, PIDeque<T> & s) {f.swap(s);}


#endif // PIDEQUE_H
