目录
1. 头文件
1.1 字典头文件
template <typename K, typename V> struct Dictionary { //词典Dictionary模板类
virtual int size() const = 0; //当前词条总数
virtual bool put(K, V) = 0; //插入词条(禁止雷同词条时可能失败)
virtual V* get(K k) = 0; //读取词条
virtual bool remove(K k) = 0; //删除词条
};
1.2 词条头文件
//词条模板类
template <typename K, typename V> struct Entry {
K key; V value; //关键码、数值
Entry(K k = K(), V v = V()) : key(k), value(v) {}; //默认构造函数
Entry(Entry<K, V> const& e) : key(e.key), value(e.value) {}; //基于克隆的构造函数
bool operator< (Entry<K, V> const& e) { return key < e.key; } //比较器:小于
bool operator> (Entry<K, V> const& e) { return key > e.key; } //比较器:大于
bool operator== (Entry<K, V> const& e) { return key == e.key; } //判等器:等于
bool operator!= (Entry<K, V> const& e) { return key != e.key; } //判等器:不等于
}; //得益于比较器和判等器,从此往后,不必严格区分词条及其对应的关键码
1.3 位图头文件
#include <cstdlib>
#include <cstdio>
#include <memory.h>
class Bitmap { //位图Bitmap类
private:
unsigned char* M; int N; //比特图所存放的空间M[],容量为N*sizeof(char)*8比特
protected:
void init(int n) { M = new unsigned char[N = (n + 7) / 8]; memset(M, 0, N); }
public:
Bitmap(int n = 8) { init(n); } //按指定或默认规模创建比特图(为测试暂时选用较小的默认值)
Bitmap(char* file, int n = 8) //按指定或默认规模,从指定文件中读取比特图
{
//默认的mode依赖于fstream类型
init(n); FILE* fp = fopen(file, "r"); fread(M, sizeof(char), N, fp); fclose(fp);
}
~Bitmap() { delete[] M; M = NULL; } //析构时释放比特图空间
void set(int k) { expand(k); M[k >> 3] |= (0x80 >> (k & 0x07)); }
void clear(int k) { expand(k); M[k >> 3] &= ~(0x80 >> (k & 0x07)); }
bool test(int k) { expand(k); return M[k >> 3] & (0x80 >> (k & 0x07)); }
void dump(char* file) //将位图整体导出至指定的文件,以便对此后的新位图批量初始化
{
FILE* fp = fopen(file, "w"); fwrite(M, sizeof(char), N, fp); fclose(fp);
}
char* bits2string(int n) { //将前n位转换为字符串——
expand(n - 1); //此时可能被访问的最高位为bitmap[n - 1]
char* s = new char[n + 1]; s[n] = '\0'; //字符串所占空间,由上层调用者负责释放
for (int i = 0; i < n; i++) s[i] = test(i) ? '1' : '0';
return s; //返回字符串位置
}
void expand(int k) { //若被访问的Bitmap[k]已出界,则需扩容
if (k < 8 * N) return; //仍在界内,无需扩容
int oldN = N; unsigned char* oldM = M;
init(2 * k); //与向量类似,加倍策略
memcpy_s(M, N, oldM, oldN); delete[] oldM; //原数据转移至新空间
}
void print(int n) //逐位打印以检验位图内容,非必需接口
{
expand(n); for (int i = 0; i < n; i++) printf(test(i) ? "1" : "0");
}
};