allege.h头文件
#ifndef _ALLEGE_H
#define _ALLEGE_H
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
inline void allege_error(int arg,const char* msg)
{
if (!arg)
{
fprintf(stderr,"error: %s\n",msg);
#ifdef NDEBUG
exit(1);
#endif
}
}
#define allege(expr,msg) { allege_error((expr)?1:0,msg); assert(expr);}
#define allegemem(expr) allege(expr,"out of memory")
#define allegefile(expr) allege(expr,"can't open file")
#endif
tstash.h头文件
#ifndef TSTASH_H
#define TSTASH_H
#include <stdlib.h>
#include <assert.h>
#include "allege.h"
enum owns{no=0,yes=1,Default};
template<class T,int sz>class tstashiter;
template<class T,int size=20>
class tstash
{
int quantity;
int next;
owns own;
void inflate(int increase=size);
protected:
T** storage;
public:
tstash(owns own=yes);
~tstash();
int Owns() const
{
return own;
}
void Owns(owns newown)
{
own=newown;
}
int add(T* element);
int remove(int index,owns d=Default);
T* operator[](int index);
int cout() const
{
return next;
}
friend class tstashiter<T,size>;
};
template<class T,int size=20>
class tstashiter
{
tstash<T,size> & ts;
int index;
public:
tstashiter(tstash<T,size>& TS):ts(TS),index(0){}
tstashiter(const tstashiter<T,size>& RS):ts(tstashiter.ts),index(tstashiter.index){}
void forward(int amount)
{
index+=amount;
if (index>=ts.next)
{
index=ts.next-1;
}
}
void backward(int amount)
{
index-=amount;
if (index<0)
{
index=0;
}
}
int operator++()
{
if (++index>=ts.next)
return 0;
return 1;
}
int operator++(int) {return operator++();}
int operator--()
{
if(--index<0)
return 0;
return 1;
}
int operator--(int) {return operator--();}
operator int()
{
return (index>=0 && index<ts.next);
}
T* operator->()
{
T* t=ts.storage[index];
if(t)
return t;
allege(0,"tstashiter operator-> return 0");
return 0;
}
int remove(owns d=Default)
{
return ts.remove(index,d);
}
};
template<class T,int sz>tstash<T,sz>::tstash(owns wns):own(wns)
{
quantity=0;
storage=0;
next=0;
}
template<class T,int sz>tstash<T,sz>::~tstash()
{
if(!storage)
return ;
if (own==yes)
{
for (int i=0;i<cout();i++)
{
delete storage[i];
}
}
free(storage);
}
template<class T,int sz> int tstash<T,sz>::add(T* element)
{
if (next>=quantity)
{
inflate();
}
storage[next++]=element;
return (next-1);
}
template<class T,int sz>int tstash<T,sz>::remove(int index,owns d)
{
if (index>=next || index<0)
return 0;
switch (d)
{
case Default:
if (own!=yes)
break;
case yes:
delete storage[index];
case no:
storage[index]=0;
}
return 1;
}
template<class T,int sz>inline T* tstash<T,sz>::operator[](int index)
{
assert(index<next && index>=0);
return storage[index];
}
template<class T,int sz>void tstash<T,sz>::inflate(int increase)
{
void *V=realloc(storage,(quantity+increase)*sizeof(T*));
// assert(V!=NULL);
allegemem(V);
storage=(T**)V;
quantity+=increase;
}
#endif
源文件:
#include "tstash.h"
#include <iostream>
#include <fstream>
#include "allege.h"
#include <string>
using namespace std;
const bufsize=256;
ofstream out("tstest.out");
ofstream strout("strtest.out");
class Int
{
int i;
public:
Int(int I=0):i(I)
{
out<<">"<<i<<endl;
}
~Int()
{
out<<"~"<<i<<endl;
}
operator int() const {return i;}
friend ostream& operator<<(ostream& os,const Int& x)
{
return os<<x.i;
}
};
class stringV:public string
{
char* s;
stringV(const stringV&);
void operator=(stringV&);
public:
stringV(const char *sv="")
{
// s=(char*)malloc(strlen(sv)+1);
s=new char[strlen(sv)+1];
allegemem(s);
strcpy(s,sv);
}
~stringV() {delete s;}
operator const char*() const {return s;}
char * str() const {return s;}
friend stringV* makestring(const char *s)
{
return new stringV(s);
}
static stringV* make(const char *s)
{
return new stringV(s);
}
friend ostream& operator<<(ostream& os,const stringV& sv)
{
return os<<sv.s;
}
};
void main()
{
tstash<Int> intstash;
for (int i=0;i<30;i++)
{
intstash.add(new Int(i));
}
tstashiter<Int> init(intstash);
init.forward(5);
for (int j=0;j<20;j++,init++)
{
init.remove();
}
for (int k=0;k<intstash.cout();k++)
{
if(intstash[k])
out<<*intstash[k]<<endl;
}
ifstream file("readInfor.txt");
allegefile(file);
char buf[bufsize];
tstash<stringV> stringVstash;
while (file.getline(buf,bufsize))
{
stringVstash.add(makestring(buf));
}
for (int u=0;u<stringVstash.cout();u++)
{
if (stringVstash[u])
{
strout<<*stringVstash[u]<<endl;
}
}
tstashiter<stringV> it(stringVstash);
int jp=25;
it.forward(jp);
while(it)
{
strout<<jp++<<it->str()<<endl;
it++;
}
}