@@ -66,21 +66,6 @@ union inline_array_or_vector {
6666 inline_array iarray;
6767 heap_vector hvector;
6868
69- bool is_inline () const {
70- // It is undefined behavior to access the inactive member of a
71- // union directly. However, it is well-defined to reinterpret_cast any
72- // pointer into a pointer to char and examine it as an array
73- // of bytes. See
74- // https://dev-discuss.pytorch.org/t/unionizing-for-profit-how-to-exploit-the-power-of-unions-in-c/444#the-memcpy-loophole-4
75- bool result = false ;
76- static_assert (offsetof (inline_array, is_inline) == 0 ,
77- " untagged union implementation relies on this" );
78- static_assert (offsetof (heap_vector, is_inline) == 0 ,
79- " untagged union implementation relies on this" );
80- std::memcpy (&result, reinterpret_cast <const char *>(this ), sizeof (bool ));
81- return result;
82- }
83-
8469 inline_array_or_vector () : iarray () {}
8570
8671 ~inline_array_or_vector () {
@@ -122,6 +107,21 @@ union inline_array_or_vector {
122107 }
123108 return *this ;
124109 }
110+
111+ bool is_inline () const {
112+ // It is undefined behavior to access the inactive member of a
113+ // union directly. However, it is well-defined to reinterpret_cast any
114+ // pointer into a pointer to char and examine it as an array
115+ // of bytes. See
116+ // https://dev-discuss.pytorch.org/t/unionizing-for-profit-how-to-exploit-the-power-of-unions-in-c/444#the-memcpy-loophole-4
117+ bool result = false ;
118+ static_assert (offsetof (inline_array, is_inline) == 0 ,
119+ " untagged union implementation relies on this" );
120+ static_assert (offsetof (heap_vector, is_inline) == 0 ,
121+ " untagged union implementation relies on this" );
122+ std::memcpy (&result, reinterpret_cast <const char *>(this ), sizeof (bool ));
123+ return result;
124+ }
125125};
126126
127127template <typename T, std::size_t InlineSize>
@@ -144,7 +144,7 @@ struct small_vector {
144144
145145 T const *data () const {
146146 if (is_inline ()) {
147- return & m_repr.iarray .arr [ 0 ] ;
147+ return m_repr.iarray .arr . data () ;
148148 }
149149 return m_repr.hvector .vec .data ();
150150 }
@@ -157,7 +157,7 @@ struct small_vector {
157157 return m_repr.hvector .vec [idx];
158158 }
159159
160- T operator [](std::size_t idx) const {
160+ T const & operator [](std::size_t idx) const {
161161 assert (idx < size ());
162162 if (is_inline ()) {
163163 return m_repr.iarray .arr [idx];
@@ -195,13 +195,16 @@ struct small_vector {
195195 }
196196
197197 void sort () {
198- T *begin = nullptr ;
199198 if (is_inline ()) {
200- begin = &m_repr.iarray .arr [0 ];
199+ if (m_repr.iarray .size < m_repr.iarray .arr .size ()) {
200+ std::sort (m_repr.iarray .arr .begin (),
201+ m_repr.iarray .arr .begin () + m_repr.iarray .size );
202+ } else {
203+ std::sort (m_repr.iarray .arr .begin (), m_repr.iarray .arr .end ());
204+ }
201205 } else {
202- begin = m_repr.hvector .vec .data ( );
206+ std::sort (m_repr. hvector . vec . begin (), m_repr.hvector .vec .end () );
203207 }
204- std::sort (begin, begin + size ());
205208 }
206209
207210private:
0 commit comments