4545#include < TiledArray/utility.h>
4646#include < madness/world/archive.h>
4747#include " TiledArray/error.h"
48+ #include " TiledArray/platform.h"
4849
4950namespace TiledArray {
5051
@@ -55,6 +56,66 @@ using vector = std::vector<T>;
5556template <typename T, std::size_t N = TA_MAX_SOO_RANK_METADATA>
5657using svector = boost::container::small_vector<T, N>;
5758
59+ } // namespace container
60+
61+ // size_of etc.
62+
63+ template <MemorySpace S, typename T, typename A>
64+ constexpr std::size_t size_of (const std::vector<T, A>& t) {
65+ std::size_t sz = 0 ;
66+ if constexpr (S == MemorySpace::Host) {
67+ sz += sizeof (t);
68+ }
69+ if constexpr (allocates_memory_space<S>(t.get_allocator ())) {
70+ if constexpr (is_constexpr_size_of_v<S, T>) {
71+ sz += sizeof (T) * t.capacity ();
72+ } else {
73+ sz += std::accumulate (
74+ t.begin (), t.end (), std::size_t {0 },
75+ [](const std::size_t s, const T& t) { return s + size_of<S>(t); });
76+ sz += (t.capacity () - t.size ()) * sizeof (T);
77+ }
78+ }
79+ return sz;
80+ }
81+
82+ template <MemorySpace S, typename T, typename VoidAlloc, typename Options>
83+ constexpr bool allocates_memory_space (
84+ const boost::container::small_vector_allocator<T, VoidAlloc, Options>& a) {
85+ return S == MemorySpace::Host;
86+ }
87+
88+ template <MemorySpace S, typename T, std::size_t N, typename A>
89+ constexpr std::size_t size_of (
90+ const boost::container::small_vector<T, N, A>& t) {
91+ std::size_t sz = 0 ;
92+ if constexpr (S == MemorySpace::Host) {
93+ sz += sizeof (t);
94+ }
95+ if (allocates_memory_space<S>(t.get_allocator ())) {
96+ std::size_t data_sz = 0 ;
97+ if constexpr (is_constexpr_size_of_v<S, T>) {
98+ data_sz += sizeof (T) * t.capacity ();
99+ } else {
100+ data_sz += std::accumulate (
101+ t.begin (), t.end (), std::size_t {0 },
102+ [](const std::size_t s, const T& t) { return s + size_of<S>(t); });
103+ data_sz += (t.capacity () - t.size ()) * sizeof (T);
104+ }
105+ if (t.capacity () > N ||
106+ S != MemorySpace::Host) { // dynamically allocated buffer => account
107+ // for tne entirety of data_sz
108+ sz += data_sz;
109+ } else { // data in internal buffer => account for the
110+ // dynamically-allocated part of data_sz
111+ sz += (data_sz - t.capacity () * sizeof (T));
112+ }
113+ }
114+ return sz;
115+ }
116+
117+ namespace container {
118+
58119template <typename Range>
59120std::enable_if_t <detail::is_integral_range_v<Range> &&
60121 detail::is_sized_range_v<Range>,
0 commit comments