File tree Expand file tree Collapse file tree
source/source_cell/module_neighlist Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #ifndef NEIGHBOR_TYPES_H
2+ #define NEIGHBOR_TYPES_H
3+
4+ #include < cstdint>
5+ #include < cstddef>
6+ #include < limits>
7+ #include < stdexcept>
8+ #include < string>
9+
10+ namespace ModuleNeighList
11+ {
12+
13+ using GlobalAtomId = std::int64_t ;
14+ using LocalAtomIndex = std::int32_t ;
15+ using NeighborCount = std::int32_t ;
16+
17+ inline int checked_int_size (const std::size_t value, const char * context)
18+ {
19+ if (value > static_cast <std::size_t >(std::numeric_limits<int >::max ()))
20+ {
21+ throw std::overflow_error (std::string (context) + " exceeds int range." );
22+ }
23+ return static_cast <int >(value);
24+ }
25+
26+ inline LocalAtomIndex checked_local_atom_index (const std::size_t value, const char * context)
27+ {
28+ if (value > static_cast <std::size_t >(std::numeric_limits<LocalAtomIndex>::max ()))
29+ {
30+ throw std::overflow_error (std::string (context) + " exceeds local atom index range." );
31+ }
32+ return static_cast <LocalAtomIndex>(value);
33+ }
34+
35+ inline std::size_t checked_size_product (const std::size_t lhs,
36+ const std::size_t rhs,
37+ const char * context)
38+ {
39+ if (lhs != 0 && rhs > std::numeric_limits<std::size_t >::max () / lhs)
40+ {
41+ throw std::overflow_error (std::string (context) + " size product overflows." );
42+ }
43+ return lhs * rhs;
44+ }
45+
46+ inline std::size_t checked_size_sum (const std::size_t lhs,
47+ const std::size_t rhs,
48+ const char * context)
49+ {
50+ if (rhs > std::numeric_limits<std::size_t >::max () - lhs)
51+ {
52+ throw std::overflow_error (std::string (context) + " size sum overflows." );
53+ }
54+ return lhs + rhs;
55+ }
56+
57+ } // namespace ModuleNeighList
58+
59+ #endif // NEIGHBOR_TYPES_H
You can’t perform that action at this time.
0 commit comments