纯C中没有STL,那用什么呢?(300分)

  • 主题发起人 主题发起人 real_clq
  • 开始时间 开始时间
R

real_clq

Unregistered / Unconfirmed
GUEST, unregistred user!
纯C中没有STL,那用什么呢?
可以"源源不断"地把字符写到一个string或list中,但是纯C中是没有这种东东的,是否要
自己写一个list呢?不知apache是怎么做的?
 
那不就是buff吗?
有无数的人这样谢的:
char mybuff[10000]
int mybuffhead,mybufftail
 
绝对不是.
 
C 里面的通常做法是
申请一块足够大的内存空间,然后自己管理。
我们现在写的 mime decoder 就是这样做的。
 
请看看用C语言描述的数据结构书吧。
 
找本数据结构的书看看,很简单的
 
侯捷的《STL源代码分析》一书的附录中,大陆的孟岩移植了美国一个SGI公司的一个版本的STL到VC6环境下,你可以找来这本书,参照着试试。
 
用纯C改写?
 
纯C只在理论上存在,没有人用在实际开发中的,除非是做特殊用途。所有的C编译器都有类库
 
C会有类库吗?
请各位老大对自己说的话负责.
 
可以自己作一个,以前我也作过一个的:
/**************************************************************
* 动态数组(cs_vector.h 定义)
*
* 数组支持自动增长,为了减少内存分配次数,数组的增长以
* BLOCK_SIZE为单位
*
* 作者:何清
* 日期:2003-05-29
*
*
* 更新历史:
*
* @2003-05-29
* 创建文档
**************************************************************/
#ifndef __CSCRIPTPROJECT_VECTOR_H__
#define __CSCRIPTPROJECT_VECTOR_H__
#include "cs_os.h"
typedef struct _Vector
{
LPVOID *data;
/* 存放数据元素的单元 */
int count;
/* 数据元素个数 */
int size;
/* 总共分配的内存单元 */
}VECTOR, *LPVECTOR;
LPVECTOR vector_new();
void vector_destroy(LPVECTOR self);
void vector_clear(LPVECTOR self);
BOOL vector_is_empty(LPVECTOR self);
int vector_get_count(LPVECTOR self);
int vector_get_size(LPVECTOR self);
void vector_resize(LPVECTOR self, int p_newsize);
LPVOID vector_at(LPVECTOR self, int p_index);
void vector_set_at(LPVECTOR self, int p_index, LPVOID p_item);
void vector_append(LPVECTOR self, LPVOID p_item);
void vector_insert(LPVECTOR self, int p_index, LPVOID p_item);
LPVOID vector_remove(LPVECTOR self, int p_index);
#endif /* end of __CSCRIPTPROJECT_VECTOR_H__ */
/**************************************************************
* 动态数组(cs_vector.c 实现)
*
*
*
* 作者:何清
* 日期:2003-05-29
*
*
* 更新历史:
*
* @2003-05-29
* 创建文档
**************************************************************/
#include "../include/cs_vector.h"
#include "../include/cs_mem.h"
/* 创建一个动态数组容器
* func 是一个用于释放数据元素的函数指针
*/
LPVECTOR vector_new()
{
LPVECTOR self;
self = (LPVECTOR)mem_new(sizeof(VECTOR));
self->data = NULL;
self->count = 0;
self->size = 0;
return self;
}
/* 释放动态数组容器 */
void vector_destroy(LPVECTOR self)
{
CHECK_NORETURN(self);
vector_clear(self);
mem_destroy(self->data);
mem_destroy(self);
self = NULL;
}
/* 将数组容器的所有单元清空 */
void vector_clear(LPVECTOR self)
{
int i;
CHECK_NORETURN(self);
for (i = 0;
i < self->size;
i++)
self->data = NULL;
self->count = 0;
}
/* 是否为空,如果返回值为 TRUE,说明数组是空的 */
BOOL vector_is_empty(LPVECTOR self)
{
CHECK_RETURN(self, FALSE);
return (self->count == 0);
}
/* 取数组容器中的数据元素个数 */
int vector_get_count(LPVECTOR self)
{
CHECK_RETURN(self, 0);
return self->count;
}
/* 取数组容器的大小 */
int vector_get_size(LPVECTOR self)
{
CHECK_RETURN(self, 0);
return self->size;
}
/* 扩张或收缩数组大小
* 如果收缩数组,则可能会使最后过长的数据元素丢失
*/
void vector_resize(LPVECTOR self, int p_newsize)
{
int i, size, block;
block = (p_newsize + BLOCK_SIZE - 1) >> BLOCK_SIZE_EXP << BLOCK_SIZE_EXP;

size = block * sizeof(LPVOID);

self->data = mem_resize( self->data, size);
self->size = block;
if (self->count > block) /* 如果数组被收缩,则数据元素也有可能被移除*/
self->count = block;
else
{
for (i = self->count;
i < self->size;
i++)
self->data = NULL;
}
}
/* 取指定索引的数据元素
* p_index一定要小于count值,否则总是返回NULL
*/
LPVOID vector_at(LPVECTOR self, int p_index)
{
CHECK_RETURN(self, NULL);
if (p_index >= self->count &amp;&amp;
0 > p_index)
return NULL;
else
return self->data[p_index];
}
/* 将一个数据元素放入到指定的单元中
* 如果指定的索引超出当前的数组大小,该数组会自动扩张
*/
void vector_set_at(LPVECTOR self, int p_index, LPVOID p_item)
{
CHECK_NORETURN(self);
if (p_index < 0) return;
if (p_index >= self->size)
vector_resize( self, p_index + 1);
self->data[p_index] = p_item;
self->count++;
}
/* 将一个元素放入到最后 */
void vector_append(LPVECTOR self, LPVOID p_item)
{
CHECK_NORETURN(self);
if (self->count == self->size)
vector_resize(self, self->size + 1);
self->data[self->count++] = p_item;
}
void vector_insert(LPVECTOR self, int p_index, LPVOID p_item)
{
CHECK_NORETURN(self);
if ( p_index < 0 )
p_index = 0;
if ( p_index == self->count ) /* 插入到最后 */
{
vector_append(self, p_item);
return;
}
else
if ( p_index > self->count ) /* 越界插入 */
{
if (p_index >= self->size) /* 数组单元不足*/
vector_resize(self, p_index + 1);
self->data[p_index] = p_item;
self->count = p_index + 1;
}
else
{
if (self->count == self->size) /* 数组单元不足*/
vector_resize(self, self->count + 1);
mem_move(&amp;(self->data[p_index]) , &amp;(self->data[p_index + 1]),
(self->count - p_index) * sizeof(LPVOID));
self->count ++;
self->data[p_index] = p_item;
}
}
LPVOID vector_remove(LPVECTOR self, int p_index)
{
LPVOID ptr = NULL;
CHECK_RETURN(self, NULL);

if ( p_index < 0 || p_index >= self->count)
return NULL;
ptr = self->data[p_index];
if ( p_index + 1 == self->count ) /* 移除最后一个 */
self->data[p_index] = NULL;
else
mem_copy(&amp;(self->data[p_index + 1]) , &amp;(self->data[p_index]),
(self->count - p_index) * sizeof(LPVOID));

self->count--;
self->data[self->count] = NULL;
return ptr;
}
 
又如一个List
/**************************************************************
* 哈希表管理单元(cs_hash.h 定义)
*
*
*
* 作者:何清
* 日期:2003-06-09
*
*
* 更新历史:
*
* @2003-06-09
* 创建文档
**************************************************************/
#ifndef __CSCRIPTPROJECT_HASH_H__
#define __CSCRIPTPROJECT_HASH_H__
#include "cs_os.h"

/* 计算Hash值的函数,则用户提供 */
typedef unsigned int (*HashFunc) (LPVOID key);
typedef void (*UserFunc) (LPVOID value, LPVOID data);
typedef struct _HashNode
{
LPVOID key;
LPVOID value;
struct _HashNode *next;
}HashNode;
typedef struct _HashTable
{
DWORD size;
DWORD nnodes;
HashNode **nodes;
HashFunc hash_func;
EqualFunc key_equal_func;
}HASHTABLE, *LPHASHTABLE;

/*
*Hash tables
*/
LPHASHTABLE hash_table_new (HashFunc hash_func, EqualFunc key_equal_func);
void hash_table_destroy (LPHASHTABLE hash_table);
LPVOID hash_table_find (LPHASHTABLE hash_table, LPVOID key);
void hash_table_insert (LPHASHTABLE hash_table, LPVOID key, LPVOID value);
BOOL hash_table_remove (LPHASHTABLE hash_table, LPVOID key);
void hash_table_foreach(LPHASHTABLE hash_table, UserFunc user_func, LPVOID data);
unsigned int hash_table_get_size (LPHASHTABLE hash_table);
/* Hash Functions
*/
unsigned int direct_hash (LPVOID v);

#endif /* end of __CSCRIPTPROJECT_HASH_H__ */
/**************************************************************
* 哈希表管理单元(cs_hash.c 实现)
*
*
*
* 作者:何清
* 日期:2003-06-09
*
*
* 更新历史:
*
* @2003-06-09
* 创建文档
**************************************************************/
#include "../include/cs_hash.h"
#include "../include/cs_mem.h"
#include <stdio.h>
#include <stdlib.h>
#define HASH_TABLE_MIN_SIZE 11
#define HASH_TABLE_MAX_SIZE 13845163

static const unsigned int primes[] = {
11, 19, 37, 73, 109, 163, 251, 367, 557, 823,
1237, 1861, 2777, 4177, 6247, 9371, 14057,
21089, 31627, 47431, 71143, 106721, 160073, 240101,
360163, 540217, 810343, 1215497, 1823231, 2734867,
4102283, 6153409, 9230113, 13845163
};
static const unsigned int nprimes = sizeof (primes) / sizeof (primes[0]);
static void hash_table_resize(LPHASHTABLE hash_table);
static HashNode** hash_table_find_node(LPHASHTABLE hash_table, LPVOID key);
static HashNode* hash_node_new(LPVOID key, LPVOID value);
static void hash_node_destroy(HashNode *hash_node);
static void hash_nodes_destroy(HashNode *hash_node);
static unsigned int primes_closest(unsigned int num);
static void hash_table_needresize(LPHASHTABLE hash_table);
/* 创建一个hash表容器 */
LPHASHTABLE hash_table_new (HashFunc hash_func, EqualFunc key_equal_func)
{
LPHASHTABLE hash_table;
unsigned int i;
hash_table = (LPHASHTABLE)mem_new( sizeof(HASHTABLE) );
hash_table->size = HASH_TABLE_MIN_SIZE;
hash_table->nnodes = 0;
hash_table->hash_func = hash_func ? hash_func : direct_hash;
hash_table->key_equal_func = key_equal_func;
hash_table->nodes = mem_new ( sizeof(HashNode*)*hash_table->size);
for (i = 0;
i < hash_table->size;
i++)
hash_table->nodes = NULL;
return hash_table;
}
/* 删除一个Hash表容器 */
void hash_table_destroy(LPHASHTABLE hash_table)
{
unsigned int i;
if ( NULL == hash_table )
return;
for (i = 0;
i < hash_table->size;
i++)
hash_nodes_destroy(hash_table->nodes);
mem_destroy(hash_table->nodes);
mem_destroy(hash_table);
}
static HashNode** hash_table_find_node(LPHASHTABLE hash_table, LPVOID key)
{
HashNode **node;
node = &amp;hash_table->nodes[(* hash_table->hash_func) (key) % hash_table->size];
if (hash_table->key_equal_func)
while (*node &amp;&amp;
!(*hash_table->key_equal_func) ((*node)->key, key))
node = &amp;(*node)->next;
else
while (*node &amp;&amp;
(*node)->key != key)
node = &amp;(*node)->next;
return node;
}
/* 从hash表中找一个元素 */
LPVOID hash_table_find(LPHASHTABLE hash_table, LPVOID key)
{
HashNode *node;
if ( hash_table == NULL || key == NULL) return NULL;
node = *hash_table_find_node(hash_table, key);
if ( node )
return node->value;
else
return NULL;
}
/* 将一个元素插入到Hash表容器中 */
void hash_table_insert(LPHASHTABLE hash_table, LPVOID key, LPVOID value)
{
HashNode **node;
if ( hash_table == NULL )
return;
node = hash_table_find_node(hash_table, key);
if (*node)
{
(*node)->value = value;
}
else
{
*node = hash_node_new(key, value);
hash_table->nnodes++;
hash_table_needresize(hash_table);
}
}
BOOL hash_table_remove(LPHASHTABLE hash_table, LPVOID key)
{
HashNode **node, *dest;
if ( hash_table ==NULL ) return FALSE;
node = hash_table_find_node (hash_table, key);
if (*node)
{
dest = *node;
(*node) = dest->next;
hash_node_destroy(dest);
hash_table->nnodes--;
hash_table_needresize(hash_table);
return TRUE;
}
return FALSE;
}
void hash_table_foreach(LPHASHTABLE hash_table, UserFunc user_func, LPVOID data)
{
HashNode *node;
unsigned int i;
if ( hash_table == NULL || user_func == NULL )return;
for (i = 0;
i < hash_table->size;
i++)
for (node = hash_table->nodes;
node;
node = node->next)
(* user_func) (node->value, data);
}
unsigned int hash_table_get_size(LPHASHTABLE hash_table)
{
if ( hash_table ==NULL ) return 0;
return hash_table->nnodes;
}
static void hash_table_needresize(LPHASHTABLE hash_table)
{
if ((hash_table->size >= 3 * hash_table->nnodes &amp;&amp;
hash_table->size > HASH_TABLE_MIN_SIZE) ||
(3 * hash_table->size <= hash_table->nnodes &amp;&amp;
hash_table->size < HASH_TABLE_MAX_SIZE))
hash_table_resize (hash_table);
}
static void hash_table_resize(LPHASHTABLE hash_table)
{
HashNode **new_nodes;
HashNode *node;
HashNode *next;
unsigned int hash_val;
int new_size;
unsigned int i;
i = primes_closest(hash_table->nnodes);
new_size = i > HASH_TABLE_MAX_SIZE ? HASH_TABLE_MAX_SIZE : i < HASH_TABLE_MIN_SIZE ? HASH_TABLE_MIN_SIZE : i
new_nodes = mem_new( sizeof(HashNode*)*new_size);
for (i = 0;
i < hash_table->size;
i++)
for (node = hash_table->nodes;
node;
node = next)
{
next = node->next;
hash_val = (* hash_table->hash_func) (node->key) % new_size;
node->next = new_nodes[hash_val];
new_nodes[hash_val] = node;
}
mem_destroy(hash_table->nodes);
hash_table->nodes = new_nodes;
hash_table->size = new_size;
}
static HashNode* hash_node_new(LPVOID key, LPVOID value)
{
HashNode *hash_node;
hash_node = mem_new( sizeof(HashNode) );
hash_node->key = key;
hash_node->value = value;;
hash_node->next = NULL;
return hash_node;
}
static void hash_node_destroy(HashNode *hash_node)
{
hash_node->key = NULL;
hash_node->value = NULL;
mem_destroy(hash_node);
}
static void hash_nodes_destroy(HashNode *hash_node)
{
if (hash_node)
{
HashNode *node = hash_node;
HashNode *temp;
while (node->next)
{
node->key = NULL;
node->value = NULL;
temp = node;
node = node->next;
mem_destroy(temp);
}
node->key = NULL;
node->value = NULL;
mem_destroy(node);
}
}
static unsigned int primes_closest(unsigned int num)
{
unsigned int i;
for (i = 0;
i < nprimes;
i++)
if (primes > num)
return primes;
return primes[nprimes - 1];
}
unsigned int direct_hash (LPVOID v)
{
return (unsigned int)v;
}
 
后退
顶部