当前位置: 首页 > news >正文

C++ 哈希计数器

C++哈希计数器,代码见下:

#include<iostream>

using namespace std;

template<typename KeyType, typename ValueType>
class HashNode {
public:
	KeyType key;
	ValueType value;
	HashNode* next;

	HashNode(const KeyType& key, const ValueType& value) {
		this->key = key;
		this->value = value;
		this->next = NULL;
	}
};

template<typename KeyType, typename ValueType>
class HashTable {
private:
	int size;
	HashNode<KeyType, ValueType>** table;

	int hash(const KeyType& key) const {
		int hashkey = key % size;
		if (hashkey < 0) {
			hashkey += size;
		}
		return hashkey;
	}
public:
	HashTable(int size = 256);
	~HashTable();
	void insert(const KeyType& key, const ValueType& value);
	void remove(const KeyType& key);
	bool find(const KeyType& key, ValueType& value) const;
};

template<typename KeyType, typename ValueType>
HashTable<KeyType, ValueType>::HashTable(int size) {
	this->size = size;
	this->table = new HashNode<KeyType, ValueType>* [size];
	for (int i = -0; i < size; ++i) {
		this->table[i] = NULL;
	}
}

template<typename KeyType, typename ValueType>
HashTable<KeyType, ValueType>::~HashTable() {
	for (int i = 0; i < size; ++i) {
		if (table[i]) {
			HashNode<KeyType, ValueType>* current = table[i];
			while (current) {
				HashNode<KeyType, ValueType>* next = current->next;
				delete current;
				current = next;
			}
			table[i] = NULL;
		}
	}
	delete[] table;
	table = NULL;
}

template<typename KeyType, typename ValueType>
void HashTable<KeyType, ValueType>::insert(const KeyType& key, const ValueType& value) {
	int index = hash(key);
	HashNode<KeyType, ValueType>* now = new HashNode<KeyType, ValueType>(key, value);
	if (table[index] == NULL) {
		table[index] = now;
	}
	else {
		now->next = table[index];
		table[index] = now;
	}
}

template<typename KeyType, typename ValueType>
void HashTable<KeyType, ValueType>::remove(const KeyType& key) {
	int index = hash(key);
	if (table[index]) {
		if (table[index]->key == key) {
			HashNode<KeyType, ValueType>* next = table[index]->next;
			delete table[index];
			table[index] = next;
		}
		else {
			HashNode<KeyType, ValueType>* current = table[index];
			while (current->next && current->next->key != key) {
				current = current->next;
			}
			if (current->next) {
				HashNode<KeyType, ValueType>* next = current->next->next;
				delete current->next;
				current->next = next;
			}
		}
	}
}

template<typename KeyType, typename ValueType>
bool HashTable<KeyType, ValueType>::find(const KeyType& key, ValueType& value) const {
	int index = hash(key);
	if (table[index]) {
		if (table[index]->key == key) {
			value = table[index]->value;
			return true;
		}
		else {
			HashNode<KeyType, ValueType>* current = table[index];
			while (current->next && current->next->key != key) {
				current = current->next;
			}
			if (current->next) {
				value = current->next->value;
				return true;
			}
		}

	}
	return false;
}

template<typename KeyType>
class HashCounter {
private:
	int* counter;
	int counterIndex;
	int counterSize;
	HashTable<KeyType, int>* hash;
public:
	HashCounter(int size = 256);
	~HashCounter();
	void reset();
	int add(const KeyType& key);
	int sub(const KeyType& key);
	int get(const KeyType& key);
};

template<typename KeyType>
HashCounter<KeyType>::HashCounter(int size) {
	counterSize = size;
	counterIndex = 0;
	counter = new int[counterSize];
	hash = NULL;
	reset();
}

template<typename KeyType>
HashCounter<KeyType>::~HashCounter() {
	delete[]counter;
	if (hash) {
		delete hash;
		hash = NULL;
	}
}

template<typename KeyType>
void HashCounter<KeyType>::reset() {
	if (hash) {
		delete hash;
		hash = NULL;
	}
	hash = new HashTable<KeyType, int>(counterSize);
	counterIndex = 0;
	for (int i = 0; i < counterSize; ++i) {
		counter[i] = 0;
	}
}

template<typename KeyType>
int HashCounter<KeyType>::add(const KeyType& key) {
	int idx;
	if (!hash->find(key, idx)) {
		idx = counterIndex++;
		hash->insert(key, idx);
	}
	return ++counter[idx];
}

template<typename KeyType>
int HashCounter<KeyType>::sub(const KeyType& key) {
	int idx;
	if (hash->find(key, idx)) {
		return --counter[idx];
	}
	return 0;
}

template<typename KeyType>
int HashCounter<KeyType>::get(const KeyType& key) {
	int idx;
	if (hash->find(key, idx)) {
		return counter[idx];
	}
	return 0;
}

int main() {
	HashCounter<long long> hc(1000);
	hc.add(14);
	hc.add(14);
	hc.add(14);
	hc.add(14);
	hc.add(14);
	hc.add(14);
	hc.sub(14);
	cout << hc.get(14) << endl;

}

http://www.dtcms.com/a/85891.html

相关文章:

  • 《论语别裁》第02章 为政(03)星辰知多少
  • 红帽认证工程师价值
  • 补码详细分析
  • 在Dify中使用Echarts生成一个图表
  • LabVIEW FPGA与Windows平台数据滤波处理对比
  • Leetcode 3493. Properties Graph
  • SpringCloud-consul
  • 好用的Markdown阅读编辑器Typora破解记录
  • Midscene.js自然语言驱动的网页自动化全指南
  • 航拍图像阴影自动检测去除算法研究(大纲)
  • C++——权限初识
  • 深度剖析:复制带随机指针的链表算法实现
  • C++STL(三) :list的模拟实现
  • 使用Kubesec检查YAML文件安全
  • Django项目之订单管理part5
  • 前后端+数据库的项目实战:hbu迎新网-较复杂(下)javaweb
  • 运费服务模块通用需求分析
  • 算法刷题整理合集(七)·【算法赛】
  • Java面试黄金宝典12
  • STM32基础教程——PWM驱动LED呼吸灯
  • 【数理基础】【概率论与数理统计】概率论与数理统计本科课程总结、资料汇总、个人理解
  • 《AI大模型开发笔记》企业RAG技术实战(二)
  • AI比人脑更强,因为被植入思维模型【19】三脑理论思维模型
  • 我未来计划的Computer Use产品与对照组,即迷你版的AIGC中文脚本语言的区别
  • 本地部署Dify 添加Ollama模型DeepSeek
  • 2024年数维杯数学建模C题天然气水合物资源量评价解题全过程论文及程序
  • 全星研发管理 APQP 软件系统:以功能优势赋能企业研发
  • 死锁:当程序 “卡住“ 时,发生了什么?
  • UDP套接字编程(代码)
  • 聊聊langchain4j的MCP