从C++下去到 C: std::map 的替代品?

发布于 2024-12-05 14:44:09 字数 143 浏览 1 评论 0 原文

我正在寻找 std::map 的简约替代方案,它将进入 Windows 内核驱动程序,因此它应该相当快..预计它会容纳相对较小的数量(工作集中约为 200)键和大量的插入件。

寻找可以降低关键搜索成本的解决方案。

I am looking for minimalistic alternative for std::map<long, int>, which would go into Windows kernel driver, so it should be quite fast.. it is expected to hold a relatively small (~200 in working set) amount of keys and a large amount of inserts.

Looking for solution that can cut key search cost.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

心的位置 2024-12-12 14:44:09

已经为你做好了。

请参阅 RtlXxxGenericTable 和 RtlXxxGenericTableAvl 调用。

  • RtlInitializeElementGenericTable
  • RtlDeleteElementGenericTable
  • RtlEnumerateGenericTable
  • RtlEnumerateGenericTableWithoutSplaying
  • RtlGetElementGenericTable
  • RtlInsertElementGenericTable
  • RtlLookupElementGenericTable
  • RtlNumberGenericTableElements

  • RtlInitializeElementGenericTableAvl

  • RtlDeleteElementGenericTableAvl
  • RtlEnumerateGenericTableAvl
  • RtlGetElementGenericTableAvl
  • RtlInitializeGenericTable
  • RtlInsertElementGenericTableAvl
  • RtlLookupElementGenericTableAvl
  • RtlNumberGenericTableElementsAvl

Already done for you.

See the RtlXxxGenericTable and RtlXxxGenericTableAvl calls.

  • RtlInitializeElementGenericTable
  • RtlDeleteElementGenericTable
  • RtlEnumerateGenericTable
  • RtlEnumerateGenericTableWithoutSplaying
  • RtlGetElementGenericTable
  • RtlInsertElementGenericTable
  • RtlLookupElementGenericTable
  • RtlNumberGenericTableElements

  • RtlInitializeElementGenericTableAvl

  • RtlDeleteElementGenericTableAvl
  • RtlEnumerateGenericTableAvl
  • RtlGetElementGenericTableAvl
  • RtlInitializeGenericTable
  • RtlInsertElementGenericTableAvl
  • RtlLookupElementGenericTableAvl
  • RtlNumberGenericTableElementsAvl
捎一片雪花 2024-12-12 14:44:09

如果键的数量非常很小,例如10个左右,也许您可​​以只进行线性搜索。如果您注意将键空间压缩在内存中以最大限度地提高缓存命中率,那么它会非常快,并且在内存分配等方面的开销非常低。

If the number of keys is very small, e.g. 10 or something, perhaps you can get away with just a linear search. If you take care to keep the key-space compressed in memory to maximise cache hits, it can be pretty fast and have very low overhead in terms of memory allocations and so on.

漫漫岁月 2024-12-12 14:44:09

过去,对于少于几千个对象的映射,我发现创建一个按键值排序的 std::vector,然后使用二分搜索进行搜索比使用 std::map 快得多。

In the past, for maps with less than a few thousand objects, I've found that creating a std::vector sorted on the key value that is then searched for using a binary search is significantly faster than using a std::map.

怪我入戏太深 2024-12-12 14:44:09

您也可以用 C 实现 std::map 语义。只是它不会是模板

这是开始:

struct KeyValuePair
{
   KeyType key;
   ValueType value;
};

struct Map
{
   KeyValuePair *list; //it can be linkedlist as well
};

//these are the interfaces which operate on map
void Insert(Map * map, KeyType key, ValueType value);
void Update(Map * map, KeyType key, ValueType value);
int Find(Map * map, KeyType key, ValueType *outValue);

//Implement Get in terms of Find
ValueType Get(Map * map, KeyType key)
{
     ValueType value;
     Find(map, key, &value);
     return value;
}

You could implement std::map semantics in C as well. Only that it will not be template.

Here is the start:

struct KeyValuePair
{
   KeyType key;
   ValueType value;
};

struct Map
{
   KeyValuePair *list; //it can be linkedlist as well
};

//these are the interfaces which operate on map
void Insert(Map * map, KeyType key, ValueType value);
void Update(Map * map, KeyType key, ValueType value);
int Find(Map * map, KeyType key, ValueType *outValue);

//Implement Get in terms of Find
ValueType Get(Map * map, KeyType key)
{
     ValueType value;
     Find(map, key, &value);
     return value;
}
暖风昔人 2024-12-12 14:44:09

在 C 中,您需要两个伴随数组:一个用于键,另一个用于值。如果您可以封装这两者,以便用户可以坚持使用地图语义,那将会有所帮助。

You'll need two companion arrays in C: one for keys, the other for values. It'd help if you could encapsulate the two so users could stick to map semantics.

﹉夏雨初晴づ 2024-12-12 14:44:09

如果您需要用 C 语言实现字典的简单实现,那么有一天用 C 语言实现字典会更有趣……但我们并不总是有时间这样做。

所以你可以尝试看看 iniparser module one,它是一个可在内核中使用的小字典和/或嵌入式世界。

If you need a simple implementation of a dictionary in C, it's funnier to implement a dictionary in C one day... but we don't have always time to do it.

So you could try to have a look to iniparser module one, it is a little dictionary usable in a kernel and/or embedded world.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文