Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
int128/example/container_hash.cpp at develop · boostorg/int128 · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
boostorg
/
int128
Public
Notifications
You must be signed in to change notification settings
Fork
5
Star
19
Code
Issues
0
Pull requests
0
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Files
Expand file tree
develop
Breadcrumbs
int128
/
example
/
container_hash.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
119 lines (99 loc) · 4.43 KB
develop
Breadcrumbs
int128
/
example
/
container_hash.cpp
Copy path
Top
File metadata and controls
Code
Blame
119 lines (99 loc) · 4.43 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//
Copyright 2026 Matt Borland
//
Distributed under the Boost Software License, Version 1.0.
//
https://www.boost.org/LICENSE_1_0.txt
//
This example demonstrates Boost.ContainerHash integration with int128 types.
//
Including <boost/int128/hash.hpp> injects hash_value overloads for the library
//
types, so boost::hash, boost::hash_combine, boost::hash_range, and the
//
boost::unordered containers (which default to boost::hash) all work with no
//
extra configuration.
#
include
<
boost/int128/int128.hpp
>
#
include
<
boost/int128/hash.hpp
>
//
tag::exclude[]
//
The Boost.ContainerHash and Boost.Unordered headers trip strict warnings the
//
test build enables (old-style casts, and a float-to-double promotion in
//
std::ceil on older clang); silence them for these third-party headers only.
//
This is not needed in normal use.
#
if
defined(__GNUC__) || defined(__clang__)
#
pragma
GCC diagnostic push
#
pragma
GCC diagnostic ignored "-Wold-style-cast"
#
pragma
GCC diagnostic ignored "-Wdouble-promotion"
#
endif
#
if
defined(_MSC_VER)
#
pragma
warning(push)
#
pragma
warning(disable: 4848)
#
endif
//
end::exclude[]
#
include
<
boost/container_hash/hash.hpp
>
#
include
<
boost/unordered/unordered_map.hpp
>
#
include
<
boost/unordered/unordered_flat_map.hpp
>
//
tag::exclude[]
#
if
defined(__GNUC__) || defined(__clang__)
#
pragma
GCC diagnostic pop
#
endif
//
end::exclude[]
#
include
<
cstddef
>
#
include
<
functional
>
#
include
<
iostream
>
#
include
<
string
>
using
boost::int128::uint128;
using
boost::int128::int128;
//
A user-defined composite key that holds 128-bit fields. Providing a hash_value
//
overload in the type's own namespace lets Boost.ContainerHash find it via ADL,
//
and boost::hash_combine reuses the int128 hashes supplied by hash.hpp.
struct
point
{
int128 x;
int128 y;
};
bool
operator
==(
const
point& lhs,
const
point& rhs)
{
return
lhs.
x
== rhs.
x
&& lhs.
y
== rhs.
y
;
}
std::
size_t
hash_value
(
const
point& p)
{
std::
size_t
seed {
0
};
boost::hash_combine
(seed, p.
x
);
boost::hash_combine
(seed, p.
y
);
return
seed;
}
int
main
()
{
std::cout <<
"
=== boost::hash on 128-bit types ===
"
<< std::endl;
//
boost::hash<T> dispatches to the hash_value overloads from hash.hpp, which
//
delegate to std::hash, so the two functors always agree.
const
uint128 big {
UINT64_C
(
0xDEADBEEF
),
UINT64_C
(
0xCAFEBABE12345678
)};
const
int128 neg {-
123456789012345678LL
};
std::cout <<
"
boost::hash matches std::hash (uint128):
"
<< std::boolalpha << (boost::hash<uint128>{}(big) == std::hash<uint128>{}(big)) << std::endl;
std::cout <<
"
boost::hash matches std::hash (int128):
"
<< (boost::hash<int128>{}(neg) == std::hash<int128>{}(neg)) << std::endl;
std::cout <<
"
\n
=== boost::unordered_map<uint128, ...> ===
"
<< std::endl;
//
boost::unordered_map defaults to boost::hash<Key>, so uint128 keys need
//
no explicit hasher.
boost::unordered_map<uint128, std::string> labels {};
labels[uint128{
1
,
0
}] =
"
two to the sixty-fourth
"
;
//
2^64
labels[uint128{
UINT64_C
(
0x8000000000000000
),
0
}] =
"
two to the one hundred twenty-seventh
"
;
//
2^127
labels[uint128{
42
}] =
"
forty-two
"
;
std::cout <<
"
Entries:
"
<< labels.
size
() << std::endl;
std::cout <<
"
Label at 2^64:
"
<< labels[uint128{
1
,
0
}] << std::endl;
std::cout <<
"
Contains 42:
"
<< (labels.
find
(uint128{
42
}) != labels.
end
()) << std::endl;
std::cout <<
"
\n
=== hash_combine for a composite key ===
"
<< std::endl;
//
The point hasher combines two int128 fields; boost::hash<point> finds it
//
via ADL, letting point be used as a key directly.
boost::unordered_map<point,
long
> populations {};
populations[point{
10
,
20
}] =
5000000
;
populations[point{-
30
,
40
}] =
250000
;
std::cout <<
"
Cities stored:
"
<< populations.
size
() << std::endl;
std::cout <<
"
Population at (10, 20):
"
<< populations[point{
10
,
20
}] << std::endl;
std::cout <<
"
Same coordinate hashes equal:
"
<< (boost::hash<point>{}(point{
10
,
20
}) == boost::hash<point>{}(point{
10
,
20
})) << std::endl;
std::cout <<
"
\n
=== boost::unordered_flat_map<int128, ...> ===
"
<< std::endl;
//
The modern flat container also defaults to boost::hash.
boost::unordered_flat_map<int128,
int
> counts {};
counts[int128{-
1
}] =
1
;
counts[int128{
0
}] =
2
;
counts[int128{
1
}] =
3
;
std::cout <<
"
Flat map size:
"
<< counts.
size
() << std::endl;
std::cout <<
"
counts[-1] =
"
<< counts[int128{-
1
}] << std::endl;
return
0
;
}
You can’t perform that action at this time.