写手vector

Posted Stump

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了写手vector相关的知识,希望对你有一定的参考价值。

#define DEFAULT_CAPACITY 3

typedef int Rank;

class Fib {
	private:
		int f, g;
	public:
		Fib( int n ) { f = 1; g = 0; while( g < n ) next(); }
		inline int get() { return g; }
		inline int next() { g += f; f = g - f; return g; }
		inline int prev() { f = g - f; g -= f; return g; }
};

template <typename T> class Vector {
	protected:
		Rank _size; int _capacity; T* _elem;
		inline void copyfrom( T const *A, Rank l, Rank r ) {
			_elem = new T[_capacity = ( r - l ) << 1]; _size = 0;
			while( l < r )
				_elem[_size++] = A[l++];
		}
		inline void expand() {
			if( _size < _capacity )
				return;
			if( _capacity < DEFAULT_CAPACITY )
				_capacity = DEFAULT_CAPACITY;
			T* oldElem = _elem;
			_elem = new T[_capacity <<= 1];
			for( register int i = 0; i <= _size; ++i )
				_elem[i] = oldElem[i];
			delete [] oldElem;
		}
		inline void shrink() {
			if( _capacity < DEFAULT_CAPACITY << 1 )
				return;
			if( _size << 2 > _capacity )
				return;
			T* oldElem = _elem;
			_elem = new T[_capacity >>= 1];
			for( register int i = 0; i <= _size; ++i )
				_elem[i] = oldElem[i];
			delete [] oldElem;
		}
		inline Rank fibsearch( T *A, T const &e, Rank l, Rank r ) {
			Fib fib( r - l );
			while( l < r ) {
				while( r - l < fib.get() )
					fib.prev();
				Rank mid = l + fib.get() - 1;
				if( e < A[mid] )
					r = mid;
				else
					if( A[mid] < e )
						l = mid + 1;
					else
						return mid;
			}
			return -1;
		}
		inline Rank find( T const &e, Rank l, Rank r ) {
			while( ( l < r-- ) && e != _elem[r] );
			return r;
		}
	public:
		Vector( int c = DEFAULT_CAPACITY, int s = 0, T v = 0 ) {
			_elem = new T[_capacity = c]; for ( _size = 0; _size < s; _elem[_size++] = v );
		}
		Vector( T const* A, Rank n ) { copyFrom ( A, 0, n ); }
		Vector ( T const* A, Rank lo, Rank hi ) { copyFrom ( A, lo, hi ); }
		Vector ( Vector<T> const& V ) { copyFrom ( V._elem, 0, V._size ); }
		Vector ( Vector<T> const& V, Rank lo, Rank hi ) { copyFrom ( V._elem, lo, hi ); }
		~Vector() { delete [] _elem; }
		Rank size() const { return _size; }
		bool empty() const { return !_size; }
		Rank find ( T const& e ) const { return find ( e, 0, _size ); }
		Rank find ( T const& e, Rank lo, Rank hi ) const;
		Rank search ( T const& e ) const { return ( 0 >= _size ) ? -1 : search ( e, 0, _size ); }
		Rank search ( T const& e, Rank lo, Rank hi ) const;
		T& operator [] ( Rank r ) const;
		Vector<T> & operator = ( Vector<T> const& );
		inline T remove( Rank r ) { T e = _elem[r]; remove( r, r + 1 ); return e; }
		inline Rank insert( Rank r, T const &e ) {
			expand();
			for( register int i = _size; r < i; --i )
				_elem[i] = _elem[i - 1];
			_elem[r] = e, ++_size;
			return r;
		}
};

  

以上是关于写手vector的主要内容,如果未能解决你的问题,请参考以下文章

每日随笔网文小说工具及书籍推荐 ① ( 网文写手的入门书籍推荐 | 网文写手的进阶书籍推荐 | 提高文学素养的写作课 )

这个代码片段究竟做了啥?

《小白的CFD之旅》招募写手

以下代码片段的时间复杂度是多少?

吐槽爆料,写手也能变股东

小马稿件靠谱,做写手以来投稿赚来的第一桶金