# 项目地址
使用
-
加入头文件到项目中
-
编写makefile
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/include/shmc)
target_link_libraries(${PROJECT_NAME} rt)
测试hashmap
使用1
#include <shmc/shm_hash_map.h>
namespace {
constexpr const char * kShmKey = "keyeddeee";
constexpr size_t kKeyNum = 100000;
constexpr size_t kNodeSize = 32;
constexpr size_t kNodeNum = 2000000;
}
int main(int argc, char **argv)
{
shmc::ShmHashMap<size_t, shmc::POSIX> hash_map_;
shmc::POSIX alloc_;
shmc::SetLogHandler(shmc::kDebug, [](shmc::LogLevel level, const char *s){
fprintf(stderr, "[%d] %s", level, s);
});
alloc_.Unlink(kShmKey + std::string("0"));
alloc_.Unlink(kShmKey + std::string("1"));
hash_map_.InitForWrite(kShmKey, kKeyNum, kNodeSize, kNodeNum);
hash_map_.Insert(1000, "hello");
std::string out;
hash_map_.Find(1000, &out);
assert(out == "hello");
hash_map_.Insert(10000, "world");
hash_map_.Find(10000, &out);
assert(out == "world");
hash_map_.Replace(10000, "replace");
hash_map_.Find(10000, &out);
assert(out == "replace");
hash_map_.Erase(10000);
hash_map_.Find(10000, &out);
alloc_.Unlink(kShmKey + std::string("0"));
alloc_.Unlink(kShmKey + std::string("1"));
}
读写
#include <shmc/shm_hash_map.h>
namespace {
constexpr const char * kShmKey = "keyeddeee";
constexpr size_t kKeyNum = 100000;
constexpr size_t kNodeSize = 32;
constexpr size_t kNodeNum = 2000000;
}
int main(int argc, char **argv)
{
shmc::ShmHashMap<const char *, shmc::POSIX> hash_map_;
shmc::POSIX alloc_;
shmc::SetLogHandler(shmc::kDebug, [](shmc::LogLevel level, const char *s){
fprintf(stderr, "[%d] %s", level, s);
});
alloc_.Unlink(kShmKey + std::string("0"));
alloc_.Unlink(kShmKey + std::string("1"));
if(!hash_map_.InitForWrite(kShmKey, kKeyNum, kNodeSize, kNodeNum) ){
printf("init failure\n");
exit(-1);
}
const char * key = "6666";
int len = 1000;
int buf[len];
for (int i = 0; i < len; i++) {
buf[i] = i;
}
if(!hash_map_.Insert(key, buf, sizeof(buf))){
printf("hash_map_.Insert failed\n");
exit(-1);
}
std::string out;
hash_map_.Find(key, &out);
printf("%lu, %lu\n", sizeof(buf), out.size());
const int* out_buf = reinterpret_cast<const int*>(out.data());
for (int i = 0; i < len; i++) {
if (i != out_buf[i]) {
printf("error, %d, %d\n", i, out_buf[i]);
exit(-1);
}
}
printf("finally hashmap size = %zu\n", hash_map_.size());
alloc_.Unlink(kShmKey + std::string("0"));
alloc_.Unlink(kShmKey + std::string("1"));
}
travel
#include <shmc/shm_hash_map.h>
namespace {
constexpr const char * kShmKey = "keyeddeee";
constexpr size_t kKeyNum = 100000;
constexpr size_t kNodeSize = 32;
constexpr size_t kNodeNum = 2000000;
}
int main(int argc, char **argv)
{
shmc::ShmHashMap<size_t , shmc::POSIX> hash_map_;
shmc::POSIX alloc_;
shmc::SetLogHandler(shmc::kDebug, [](shmc::LogLevel level, const char *s){
fprintf(stderr, "[%d] %s", level, s);
});
alloc_.Unlink(kShmKey + std::string("0"));
alloc_.Unlink(kShmKey + std::string("1"));
if(!hash_map_.InitForWrite(kShmKey, kKeyNum, kNodeSize, kNodeNum) ){
printf("init failure\n");
exit(-1);
}
for (size_t i = 0; i < 10; i++) {
char buf[20];
snprintf(buf, sizeof(buf), "%lu", i);
if(!hash_map_.Insert(i, buf, sizeof(buf))){
printf("hash_map_.Insert failed\n");
exit(-1);
}
}
// travel all
size_t sum = 0;
bool flag = hash_map_.Travel([&sum](const size_t& key, const std::string& val){
size_t val_n = strtoul(val.c_str(), NULL, 0);
if (val_n != key){
printf("1. strtoul failed, %d, %d\n", val_n, key);
exit(-1);
}
sum += key;
});
if (!flag){
printf("1. Travel failure\n");
exit(-1);
}
if(sum != 45UL){
printf("1. sum = %zu", sum);
exit(-1);
}
// travel with step 1
sum = 0;
typename decltype(hash_map_)::TravelPos pos;
do{
if (!hash_map_.Travel(&pos, 1, [&sum](const size_t& key, const std::string& val) {
size_t val_n = strtoul(val.c_str(), NULL, 0);
if (val_n != key){
printf("2. strtoul failed, %d, %d\n", val_n, key);
exit(-1);
}
sum += key;})){
printf("2. Travel failure\n");
exit(-1);
}
}while (!pos.at_origin());
if(sum != 45UL){
printf("2. sum = %zu", sum);
exit(-1);
}
printf("finally hashmap size = %zu\n", hash_map_.size());
alloc_.Unlink(kShmKey + std::string("0"));
alloc_.Unlink(kShmKey + std::string("1"));
}