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  return *reinterpret_cast<std::size_t const*>(key.data());
35 }
36 
37 static std::size_t
38 extract(SHAMapHash const& key)
39 {
40  return *reinterpret_cast<std::size_t const*>(key.as_uint256().data());
41 }
42 
43 static std::size_t
45 {
46  return static_cast<std::size_t>(key);
47 }
48 
49 static std::size_t
50 extract(std::string const& key)
51 {
52  return ::beast::uhash<>{}(key);
53 }
54 
55 template <typename Key>
57 partitioner(Key const& key, std::size_t const numPartitions)
58 {
59  return extract(key) % numPartitions;
60 }
61 
62 template std::size_t
64  LedgerIndex const& key,
65  std::size_t const numPartitions);
66 
67 template std::size_t
68 partitioner<uint256>(uint256 const& key, std::size_t const numPartitions);
69 
70 template std::size_t
71 partitioner<SHAMapHash>(SHAMapHash const& key, std::size_t const numPartitions);
72 
73 template std::size_t
74 partitioner<std::string>(
75  std::string const& key,
76  std::size_t const numPartitions);
77 
78 } // 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:57
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:115
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:529
ripple::base_uint
Integers of any length that is a multiple of 32-bits.
Definition: base_uint.h:75
ripple::partitioner
std::size_t partitioner(Key const &key, std::size_t const numPartitions)
Definition: partitioned_unordered_map.cpp:57
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
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