标签: sparsehash

稀疏哈希表背后的主要实现思路是什么?

为什么Google sparsehash开源库有两个实现:密集哈希表和稀疏哈希表?

hash hashtable data-structures sparsehash

19
推荐指数
1
解决办法
8688
查看次数

使用gtest和google sparsehash时重新定义元组

所有测试用例都以某种方式包含<gtest/gtest.h>并且<google/dense_hash_map>无法为我构建.通常后者是间接包含但我可以重现这样的问题:

#include <gtest/gtest.h>
#include <google/dense_hash_map>

TEST(SparsehashTest, justPass) {
  ASSERT_TRUE(true);
};


int main(int argc, char** argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}
Run Code Online (Sandbox Code Playgroud)

问题:

In file included from /usr/include/c++/5/tr1/functional:39:0,
                 from /usr/local/include/sparsehash/dense_hash_map:106,
                 from /usr/local/include/google/dense_hash_map:34,
                 from /home/me/xxx/test/SparsehashTest.cpp:6:
/usr/include/c++/5/tr1/tuple:130:11: error: redefinition of ‘class std::tuple< <template-parameter-1-1> >’
     class tuple : public _Tuple_impl<0, _Elements...>
           ^
In file included from /home/me/xxx/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:697:0,
                 from /home/me/xxx/third_party/googletest/googletest/include/gtest/internal/gtest-internal.h:40,
                 from /home/me/xxx/third_party/googletest/googletest/include/gtest/gtest.h:58,
                 from /home/me/xxx/test/SparsehashTest.cpp:5:
/usr/include/c++/5/tuple:463:11: error: previous definition of ‘class std::tuple< <template-parameter-1-1> >’
     class tuple : public _Tuple_impl<0, _Elements...>
Run Code Online (Sandbox Code Playgroud)

所以sparsehash包括/usr/include/c++/5/tr1/tuple …

c++ compilation g++ googletest sparsehash

9
推荐指数
1
解决办法
1716
查看次数

Google Sparsehash在类型上使用realloc(),这不是简单的可复制

考虑这个简单的程序:

#include <string>
#include <sparsehash/dense_hash_map>

int main()
{
    google::dense_hash_map<std::string, int> map;
    map["foo"] = 0;
}
Run Code Online (Sandbox Code Playgroud)

使用GCC 8.2和-Wclass-memaccess(或-Wall)进行编译会产生警告:

sparsehash/internal/libc_allocator_with_realloc.h:68:40: warning:
‘void* realloc(void*, size_t)’ moving an object of non-trivially copyable type
    ‘struct std::pair<const std::__cxx11::basic_string<char>, int>’;
use ‘new’ and ‘delete’ instead [-Wclass-memaccess]
    return static_cast<pointer>(realloc(p, n * sizeof(value_type)));
                                ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)

问题是:

  1. 是不确定的行为?
  2. 您能否建议可以应用于应用程序代码的修复或解决方法(不是通过更改Sparsehash或避免使用它)?
  3. (加分点)你能构造一个实际上由于这个而行为不正常的程序(使用std :: string或你自己的非平凡类型)?到目前为止,我还没有看到使用std :: string作为密钥类型的代码中的任何问题,尽管std :: string必须是一个非常常用的密钥类型.

我在这里提出了一个问题:https://github.com/sparsehash/sparsehash/issues/149

c++ realloc undefined-behavior sparsehash gcc8

6
推荐指数
1
解决办法
281
查看次数