From 5df79c53745fde5d6c3340a2979b1429cd5892c1 Mon Sep 17 00:00:00 2001 From: Henrik Rydberg Date: Sat, 8 Oct 2011 20:30:28 +0200 Subject: Initial import of htcd system 1.0 Signed-off-by: Henrik Rydberg --- src/mt/mvec.h | 149 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 src/mt/mvec.h (limited to 'src/mt/mvec.h') diff --git a/src/mt/mvec.h b/src/mt/mvec.h new file mode 100644 index 0000000..9f31c1e --- /dev/null +++ b/src/mt/mvec.h @@ -0,0 +1,149 @@ +/************************************************************************* + * + * HTCd - Copyright (C) 1998-2006 Henrik Rydberg + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef MVECH +#define MVECH + +#include +#include +#include + +const int mv_mincaps=16; + +template class mrvec { +public: + typedef T* iterator; + typedef const T* const_iterator; + + mrvec(): + map((T*)malloc(mv_mincaps*sizeof(T))),dim(0),caps(mv_mincaps) {} + mrvec(int n): + map((T*)malloc(mv_mincaps*sizeof(T))),dim(0),caps(mv_mincaps) { resize(n); } + ~mrvec() { free(map); } + + int size() const { return dim; } + int capacity() const { return caps; } + + bool empty() const { return dim==0; } + bool nempty() const { return dim!=0; } + + T& at(int pos) { return map[pos]; } + const T& at(int pos) const { return map[pos]; } + + T& operator[](int pos) { return map[pos]; } + const T& operator[](int pos) const { return map[pos]; } + + iterator begin() { return map; } + const_iterator begin() const { return map; } + + iterator end() { return map+dim; } + const_iterator end() const { return map+dim; } + + T& front() { return map[0]; } + const T& front() const { return map[0]; } + + T& back() { return map[dim?dim-1:0]; } + const T& back() const { return map[dim?dim-1:0]; } + + //////////////////////////////////////////////////////////////// + + void clear() { dim=0; } + + iterator insert(iterator it,const T& s) { + int pos=it-map; ins(pos,1); return &(map[pos]=s); + } + void insert(iterator it,int n,const T& s) { + int pos=it-map; ins(pos,n); while(n--) map[pos++]=s; + } + void insert(iterator it,const_iterator p,const_iterator e) { + int pos=it-map; ins(pos,e-p); while(p class msvec : public mrvec { +public: + using mrvec::map; + using mrvec::dim; + typedef typename mrvec::iterator iterator; + typedef typename mrvec::const_iterator const_iterator; + + msvec() : mrvec() {} + msvec(int n) : mrvec(n) { for(int i=0;i::ins(pos,1); return new(map+pos) T(s); + } + void insert(iterator it,int n,const T& s) { + int pos=it-map; mrvec::ins(pos,n); while(n--) new(map+pos++) T(s); + } + void insert(iterator it,const_iterator p,const_iterator e) { + int pos=it-map; mrvec::ins(pos,e-p); while(p~T(); int pos=it-map; mrvec::ins(pos+1,-1); return map+pos; + } + + void push_back(const T& s) { reserve(dim+1); new(map+dim++) T(s); } + void pop_back(const T& s) { if(dim) map[--dim].~T(); } + + //////////////////////////////////////////////////////////////// + + void resize(int n) { + while(dim>n) map[--dim].~T(); + mrvec::reserve(n); + while(dim