rippled
partitioned_unordered_map.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2021 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/basics/partitioned_unordered_map.h>
21 
22 #include <ripple/basics/SHAMapHash.h>
23 #include <ripple/basics/base_uint.h>
24 #include <ripple/beast/hash/uhash.h>
25 #include <ripple/protocol/Protocol.h>
26 #include <limits>
27 #include <string>
28 
29 namespace ripple {
30 
31 static std::size_t
32 extract(uint256 const& key)
33 {
34  std::size_t result;
35  // Use memcpy to avoid unaligned UB
36  // (will optimize to equivalent code)
37  std::memcpy(&result, key.data(), sizeof(std::size_t));
38  return result;
39 }
40 
41 static std::size_t
42 extract(SHAMapHash const& key)
43 {
44  return *reinterpret_cast<std::size_t const*>(key.as_uint256().data());
45 }
46 
47 static std::size_t
49 {
50  return static_cast<std::size_t>(key);
51 }
52 
53 static std::size_t
54 extract(std::string const& key)
55 {
56  return ::beast::uhash<>{}(key);
57 }
58 
59 template <typename Key>
61 partitioner(Key const& key, std::size_t const numPartitions)
62 {
63  return extract(key) % numPartitions;
64 }
65 
66 template std::size_t
68  LedgerIndex const& key,
69  std::size_t const numPartitions);
70 
71 template std::size_t
72 partitioner<uint256>(uint256 const& key, std::size_t const numPartitions);
73 
74 template std::size_t
75 partitioner<SHAMapHash>(SHAMapHash const& key, std::size_t const numPartitions);
76 
77 template std::size_t
78 partitioner<std::string>(
79  std::string const& key,
80  std::size_t const numPartitions);
81 
82 } // namespace ripple
ripple::partitioner< LedgerIndex >
template std::size_t partitioner< LedgerIndex >(LedgerIndex const &key, std::size_t const numPartitions)
ripple::LedgerIndex
std::uint32_t LedgerIndex
A ledger index.
Definition: Protocol.h:90
std::string
STL class.
ripple::partitioner< SHAMapHash >
template std::size_t partitioner< SHAMapHash >(SHAMapHash const &key, std::size_t const numPartitions)
ripple::base_uint::data
pointer data()
Definition: base_uint.h:122
ripple::SHAMapHash
Definition: SHAMapHash.h:32
ripple::partitioner< uint256 >
template std::size_t partitioner< uint256 >(uint256 const &key, std::size_t const numPartitions)
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:550
ripple::base_uint< 256 >
ripple::partitioner
std::size_t partitioner(Key const &key, std::size_t const numPartitions)
Definition: partitioned_unordered_map.cpp:61
std::uint32_t
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
limits
std::size_t
std::memcpy
T memcpy(T... args)
ripple::SHAMapHash::as_uint256
uint256 const & as_uint256() const
Definition: SHAMapHash.h:43
ripple::extract
static std::size_t extract(uint256 const &key)
Definition: partitioned_unordered_map.cpp:32
string