程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> larbin 復用的函數和結構體

larbin 復用的函數和結構體

編輯:關於C語言
 

/* Read a whole file
*/
char *readfile (int fds) {
ssize_t pos = 0;
ssize_t size = 512;
int cont = 1;
char buf[500];
ssize_t nbRead;
char *res = new char[size];
while(cont == 1) {
switch (nbRead = read(fds, &buf, 500)) {
case 0 : cont = 0; break;
case -1 : if (errno != EINTR && errno != EIO) cont = -1; break;
default :
if (pos + nbRead >= size) {
size *= 2;
char *tmp = new char[size];
memcpy(tmp, res, pos);
delete res;
res = tmp;
}
memcpy(res+pos, buf, nbRead);
pos += nbRead;
break;
}
}
res[pos] = 0;
return res;
}


//此為一個隊列。
/* standard fifo in RAM WITHOUT synchronisation */

#ifndef FIFO_H
#define FIFO_H
#include "assert.h"
#include <cstdlib>
#define uint unsigned int
#define maxUrlsBySite 2000
template <class T>
class Fifo {
public:
uint in, out;
uint size;
T **tab;

/* Specific constructor */
Fifo (uint size=maxUrlsBySite);

/* Destructor */
~Fifo ();

/* give the first object and let it in */
inline T* read () { return tab[out]; }

/* read the first obj if exist */
T *tryRead ();

/* get the first object */
T *get ();

/* get the first object (non totally blocking)
* return NULL if there is none
*/
T *tryGet ();

/* add an object in the Fifo */
void put (T *obj);

/* put an obj that has just been get
* this function must be called only to put back an obj
* that has just been tken with get */
void rePut (T *obj);

/* how many items are there inside ? */
int getLength ();

/* is this fifo empty ? */
inline bool isEmpty () { return in == out; }
};

template <class T>
Fifo<T>::Fifo (uint size) {
tab = new T*[size];
this->size = size;
in = 0;
out = 0;
}

template <class T>
Fifo<T>::~Fifo () {
delete [] tab;
}

template <class T>
T *Fifo<T>::tryRead () {
if (in == out) {
return;
} else {
return tab[out];
}
}

template <class T>
T *Fifo<T>::get () {
T *tmp;
assert (in != out);
tmp = tab[out];
out = (out + 1) % size;
return tmp;
}

template <class T>
T *Fifo<T>::tryGet () {
T *tmp = NULL;
if (in != out) {
// The stack is not empty
tmp = tab[out];
out = (out + 1) % size;
}
return tmp;
}

template <class T>
void Fifo<T>::put (T *obj) {
tab[in] = obj;
in = (in + 1) % size;
if (in == out) {
T **tmp;
tmp = new T*[2*size];
for (uint i=out; i<size; i++) {
tmp[i] = tab[i];
}
for (uint i=0; i<in; i++) {
tmp[i+size] = tab[i];
}
in += size;
size *= 2;
delete [] tab;
tab = tmp;
}
}

template <class T>
void Fifo<T>::rePut (T *obj) {
out = (out + size - 1) % size;
tab[out] = obj;
}

template <class T>
int Fifo<T>::getLength () {
return (in + size - out) % size;
}

#endif // FIFO_H


// Larbin
// Sebastien Ailleret
// 04-02-00 -> 14-03-00

#ifndef VECTOR_HH
#define VECTOR_HH

#include "types.h"

#include <assert.h>

//該模板定義的實際上是T類型的一個二維數組
/*
它的定義方法與STL中的有很大不同
Vector<char> forbExt;
定義的不是一個char型的數組,而是一個char*的數組
這個數組的每一個元素都是一個字符型指針,分別指向一個字符串

*/
template <class T>
class Vector
{
private:
/** This array contain the object */
T **tab; //數組的指針
/** Number of object in the array */
uint pos; //當前位置
/** Size of the array */
uint size; //數組的大小

public:
/** Constructor */
Vector (uint size = StdVectSize); //構造函數,數組的
/** Destructor */
~Vector ();
/** Re-init this vector : empty it all */
void recycle (uint size = StdVectSize);
/** add an element to this vector */
void addElement (T *elt);
/** give the size of the vector */
inline uint getLength () { return pos; }
/** give the array containing the objects */
inline T **getTab() { return tab; }
/** get an element of this Vector */
T *operator [] (uint i);

};

/** Constructor
* @param size the initial capacity of the Vector
*/
///////////////////////////////////////////////////////
//
//函數功能:構造函數
//參數:uint size 定義數組的長度
//返回值:無
//注:數組中各元素的類型為T的指針(T*),而非T
/////////////////////////////////////////////////////////
template <class T>
Vector<T>::Vector (uint size)
{
this->size = size;
pos = 0;
tab = new T*[size]; //T* 表示T的指針類型

}

/** Destructor */
///////////////////////////////////////////////////////
//
//函數功能:析構函數
//
/////////////////////////////////////////////////////////
template <class T>
Vector<T>::~Vector ()
{
for (uint i=0; i<pos; i++)
{
delete tab[i];
}
delete [] tab;

}

/** Re-init this vector : empty it all */
///////////////////////////////////////////////////////////////
//
//函數功能:重新初始化數組
//參數:重新初始化的數組的長度
//返回值:void
//
//////////////////////////////////////////////////////////////////
template <class T>
void Vector<T>::recycle (uint size)
{
for (uint i=0; i<pos; i++)
{
delete tab[i];
}

if (this->size > size) //如果 新的長度 小於 原來的長度
{
this->size = size;
delete [] tab;
tab = new T*[size];
}
pos = 0;
//如果新的大於原有的長度,那麼不做其他操作。
}

/** add an element to this vector */
//////////////////////////////////////////////////////////////////
//
//函數功能:向數組中添加一個元素
//參數:T *elt 所添加的元素
//返回值:void
//
/////////////////////////////////////////////////////////////////
template <class T>
void Vector<T>::addElement (T *elt)
{
assert (pos <= size); //如果當前pos位置,大於size,則錯誤

if (pos == size)
{
//數組滿了的情況,
size *= 2; //數組的長度擴大2倍
T **tmp = new T*[size]; //重新申請空間

for (uint i=0; i<pos; i++)
{
//把原來數組中的元素,拷貝到新的空間中去
tmp[i] = tab[i];
}

delete [] tab; //刪除原來的數組空間
tab = tmp;
}

tab[pos] = elt; //將新的元素添加到數組中

pos++;
}

/** get an element of this Vector */
//////////////////////////////////////////////////////////
//
//函數功能:[]運算符重載
//參數:uint i 數組的下標
//返回值:該下標的數組元素
//
///////////////////////////////////////////////////////////
template <class T>
T *Vector<T>::operator [] (uint i)
{
if (i<pos)
{
return tab[i];
}
else
{
return NULL;
}
}

#endif // VECTOR_HH
 

 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved