⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utils.cpp

📁 SMS gateway. SMS protocol for CHINA mobile, unicom, lingtong. Using mysql to exchange message.
💻 CPP
字号:
/*
 * utils.cpp - Utility classes, including HashTable and FIFO
 *
 * Author:		Kyle Hu
 * Version:		0.1 Alpha
 * Create:		Jun 26 2003
 */

#include <stdio.h>
#include <malloc.h>
#include "utils.h"

HashTable::HashTable(int init, float factor)
{
	int c = 1;
	while (c < init)
		c <<= 1;

	nCapacity = c;
	fLoadFactor = factor;
	
	ppBuffer = (HASH_NODE **) calloc(c, sizeof(HASH_NODE *));
}

HashTable::~HashTable()
{
	for (int i = 0; i < nCapacity; i++)
	{
		if (ppBuffer[i])
		{
		}
	}
}

void *HashTable::Get(int id)
{
	int key = Hash(id) & (nCapacity - 1);

	HASH_NODE *pPrev = NULL;
	HASH_NODE *pNode = ppBusyPool[key];
	void *p = NULL;

	while (pNode)
	{
		if (pNode->nID == id)
		{
			p = pNode->p;
			break;
		}

		pPrev = pNode;
		pNode = pNode->pNextNode;
	}

	return p;
}

void *HashTable::Delete(int id)
{
	int key = Hash(id) & (nCapacity - 1);

	HASH_NODE *pPrev = NULL;
	HASH_NODE *pNode = ppBusyPool[key];
	void *p = NULL;

	while (pNode)
	{
		if (pNode->nID == id)
		{
			// Remove the node
			if (pPrev)
				// Not the first node of the key position
				pPrev->pNextNode = pNode->pNextNode;
			else
				// The first node
				ppBusyPool[key] = pNode->pNextNode;

			p = pNode->p;
			free(pNode);
			nSize--;

			break;
		}

		pPrev = pNode;
		pNode = pNode->pNextNode;
	}

	return p;
}

void HashTable::Put(int id, void *p)
{
	int key = Hash(id) & (nCapacity - 1);

	HASH_NODE *pPrev = NULL;
	HASH_NODE *pNode = ppBusyPool[key];

	while (pNode)
	{
		if (pNode->nID == id)
		{
			// Replace the node
			pNode->p = p;
			break;
		}

		pPrev = pNode;
		pNode = pNode->pNextNode;
	}

	if (!pNode)
	{
		pNode = (HASH_NODE *) malloc(sizeof(HASH_NODE));
		pNode->p = p;
		if (pPrev)
		{
			// Not the first element
			pPrev->pNextNode = pNode;
			pNode->pNextNode = NULL;
		}
		else
		{
			// The first element in this position
			ppBusyPool[key] = pNode;
			pNode->pNextNode = NULL;
		}
	}

	nSize++;
}

void *FIFO::Get()
{
	if (pIdlePool == NULL)
		return NULL;

	LINKED_LIST_NODE *pNode = pIdlePool;
	if (pNode->pNextNode)
	{
		// Not the last one... Move to the next node
		pIdlePool = pNode->pNextNode;
		if (pIdlePool->pNextNode == pNode)
		{
			// Only one node left
			pIdlePool->pNextNode = NULL;
			pIdlePool->pPrevNode = NULL;
		}
		else
		{
			LINKED_LIST_NODE *p = pNode->pPrevNode;
			pIdlePool->pPrevNode = p;
			p->pNextNode = pIdlePool;
		}
	}
	else
	{
		// Last node in idle pool
		pIdlePool = NULL;
	}

	void *p = pNode->p;
	free(pNode);
	return p;
}

void FIFO::Put(void *p)
{
	LINKED_LIST_NODE *pNode = pIdlePool;
	LINKED_LIST_NODE *pNewNode = (NODE *) malloc(sizeof(LINKED_LIST_NODE));
	pNewNode->p = p;

	if (pNode)
	{
		pNewNode->pNextNode = pNode;
		if (pNode->pPrevNode)
		{
			pNode->pPrevNode->pNextNode = pNewNode;
			pNewNode->pPrevNode = pNode->pPrevNode;
			pNode->pPrevNode = pNewNode;
		}
		else
		{
			pNode->pNextNode = pNewNode;
			pNewNode->pPrevNode = pNode;
			pNode->pPrevNode = pNewNode;
		}
	}
	else
	{
		pNewNode->pNextNode = NULL;
		pNewNode->pPrevNode = NULL;
		pIdlePool = pNewNode;
	}
}

CONNECTION *FIFO::Peek()
{
	return pIdlePool == NULL ? NULL : pIdlePool->pConn;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -