【发布时间】:2014-10-03 06:40:47
【问题描述】:
我有一个需要快速查找的记录表,因此决定进行哈希表查找。
现在,最终出现的问题是我必须根据多个键查找记录。
例如,下面的所有 4 个键都应指向同一记录。
key1 -> a,b,c,d,e
key2 -> a,b,d
key3 -> a,b,e
key4 -> c
问题 #1 然后,此模式显示出与指定多个键的数据库查找的相似性。那么,B-tree 数据结构是否比多哈希表设计更适合使用?
问题 #2 一个特殊的尝试是否更适合这个问题。默认实现需要所有键 a+b+c+d+e 作为查找键。如果我必须查找 a+b+d,那么在查找时必须从这个主密钥中跳过 c & e。但是,这个想法会奏效还是已经存在?
问题 #3 另一个想法是我是否将东西插入到我的表中,同时我建立另一个查找表,其中包含每个记录的索引。这样,我可以为每个键设置多个掩码,并扫描此查找表以查找匹配的记录。我猜类似于 CAM 表。但是如果我必须扫描整个表,性能就会下降。是否可以将哈希表 + 索引逻辑混合在一起以提供速度和最佳内存使用?
到目前为止,已经尝试使用 boost multi index、uthash、trie 等来尝试实现适合所有 4 个问题的设计,但到目前为止还没有成功。我喜欢 boost multi index,但它有自己的问题,禁止我使用。
虽然我使用 C 语言进行编程和测试设计,但我对 java、php、python 等任何其他语言都很好。
任何解决此问题的其他想法将不胜感激。
我想实现的解决方案的伪代码:
/* Keys */
struct key1_s {
int src;
int dst;
char name[10];
int t1;
int t2;
};
struct key2_s {
int src;
int dst;
char name[10];
};
struct key3_s {
int src;
int dst;
int t1;
};
struct key4_s {
int src;
int dst;
int t2;
};
/* Record */
struct record_s {
int src;
int dst;
char name[10];
int t1;
int t2;
int age;
int sex;
int mobile;
}
struct record_s record[2] = {
{1, 2, "jack", 5, 6, 50, 1, 1234567890},
{3, 4, "john", 7, 8, 60, 2, 1122334455}
};
table.insert(record[0]);
table.insert(record[1]);
/* search using key1 */
struct key1_s key1;
key1.src = 1;
key1.dst = 2;
strncpy(key1.name, "jack", 10);
key1.t1 = 5;
key1.t2 = 6;
table.find(key1); // should return pointer to record[0]
/* search using key2 */
struct key2_s key2;
key2.src = 1;
key2.dst = 2;
strncpy(key1.name, "jack", 10);
table.find(key2); // should return pointer to record[0]
/* search using key3 */
struct key3_s key3;
key3.src = 1;
key3.dst = 2;
key3.t1 = 5;
table.find(key3); // should return pointer to record[0]
如果 find() 返回一个成功的指针,那么我想更新年龄、性别、手机等记录字段。
【问题讨论】:
-
抱歉符号混乱,为了澄清我的意思,添加一些伪代码
标签: python c++ boost data-structures