All pastes #491224 Raw Edit

Anonymous

public cpp v1 · immutable
·published 2007-05-16 14:52 UTC
rendered paste body
#ifndef TSET_H_#define TSET_H_#include "myexception.h"#include "memory.h"template <typename T> class TSet{	T* start, *lastElement, *endOfStorage;	int elCount, vectorSize;	bool absolute;	void initPointers();		static std::allocator<T> alloc;		public:	typedef T* iterator;		public:		iterator begin() { return start; };	iterator end() { return lastElement; };		~TSet()	{		delete [] start;	}	TSet() : elCount(0), vectorSize(100),absolute(false) { 		start = static_cast<T*>(operator new(vectorSize * sizeof(T)));			lastElement = start;		endOfStorage = &start[vectorSize];	}	TSet(int absoluteSize) : absolute(true), vectorSize(absoluteSize), elCount(0) {		start = new T[vectorSize];		lastElement = start;		endOfStorage = &start[vectorSize];	}	void add(T& x);	void remove(T& x);	bool isStored(T& x);	int size() { return elCount; };};template <typename T> void TSet<T>::add(T& x){	for (iterator it = begin(); it != end(); it++)	{		if (it == &x)			throw myexception("Element already added");	}	if(lastElement != endOfStorage) {		construct(lastElement,x);	} 	else {		T *temp = static_cast<T*>(operator new(vectorSize*vectorSize*sizeof(T)));		bcopy(start,temp,(vectorSize)*sizeof(T));		/*		for (; start != endOfStorage; start++) {			start->~T();			//delete start;		}		*/		delete [] (start);				start = temp;		endOfStorage = &start[vectorSize*vectorSize];		lastElement = &start[vectorSize];		vectorSize *= vectorSize;		construct(lastElement,x);				}	elCount++;	lastElement++;}template <typename T1, typename T2> void construct(T1* p, const T2& value) {    new (p) T1(value);}//TSet<std::string> *blah =  new TSet<std::string>();#endif /*TSET_H_*/