Topic: Proposal: std::literal_vector (replaces Predynamic
Author: Andrew Tomazos <andrewtomazos@gmail.com>
Date: Sat, 22 Mar 2014 18:18:26 -0700 (PDT)
Raw View
------=_Part_812_5732476.1395537506230
Content-Type: text/plain; charset=UTF-8
I've been thinking about the predynamic storage duration idea [1] I
presented a while back and I think I may have come up with a better,
simpler and cleaner way of dealing with the use cases:
We propose the standardization of a class template std::literal_vector<T>:
template<typename T>
struct literal_vector;
T must be a literal type, and for each T, std::literal_vector<T> is a
literal type.
std::literal_vector<T> has an interface similar to std::vector<T>. Like
std::vector<T>, it can store an arbitrarily-long random-access sequence of
T that can change in size:
For example:
constexpr std::literal_vector<int> concat(std::literal_vector<int> a,
std::literal_vector<int> b)
{
std::literal_vector<int> c;
for (auto x : a)
c.push_back(x);
for (auto x : b)
c.push_back(x);
return c;
}
Notice that concat is not a function template, it is just a regular
constexpr function.
The implementation of std::literal_vector<T> requires internal compiler
support (it cannot be implemented in pure library code), but it does not
require core language changes.
User-defined literal types can then be composed, without further compiler
support, by using one or more std::literal_vector<T> as subobjects. Using
literal_vector<T> in this fashion subsumes the use cases for predynamic
storage duration. new and delete expressions in constant expressions and
constexpr functions are no longer needed. You can just use
std::literal_vector<T> if you need "predynamic" storage duration at
compile-time. The mechanism by which std::literal_vector<T> is implemented
becomes an implementation detail not exposed to the user. That is,
std::literal_vector<T> becomes the fundamental primitive that is used to
build more complex literal types.
For example, using std::literal_vector<T> we can see how to create a
non-template string_literal class of literal type:
struct string_literal
{
...
private:
std::literal_vector<char> data;
};
That was easy - but notice we can also create a binary tree of literal type:
template<class T>
struct node
{
T value;
size_t L, R; // index of subtrees in binary_tree::nodes
};
template<class T>
struct binary_tree
{
size_t root; // index of root node in nodes
std::literal_vector<node> nodes;
};
You can imagine how to add a free list to reuse nodes and so on, but you
get the idea - from here, it doesn't require much imagination to see how
literal_vectors can be used as an "address space" to build arbitrarily
complex self-referential data structures:
struct vertex
{
std::literal_vector<size_t> edges; // indexes into
digraph::verticies
};
struct digraph
{
std::literal_vector<vertex> verticies;
};
template<class T>
struct bucket
{
hash_t hash_code;
std::literal_vector<T> contents;
};
template<class T>
struct dynamic_hash_table
{
std::literal_vector<bucket<T>> buckets;
};
template<class T>
struct matrix
{
...
private:
std::literal_vector<T> cells; // stored in row-major order
};
and so forth.
Feedback appreciated.
[1]:
https://groups.google.com/a/isocpp.org/d/msg/std-proposals/-GbnSNmJ8bE/z3yvFxtzC6AJ
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_812_5732476.1395537506230
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">I've been thinking about the predynamic storage duration i=
dea [1] I presented a while back and I think I may have come up with a bett=
er, simpler and cleaner way of dealing with the use cases:<div><br></div><d=
iv>We propose the standardization of a class template std::literal_vector&l=
t;T>:</div><div><br></div><div> template<typename T><=
/div><div> struct literal_vector;</div><div><br></div><div>T m=
ust be a literal type, and for each T, std::literal_vector<T> is a li=
teral type.</div><div><br></div><div>std::literal_vector<T> has an in=
terface similar to std::vector<T>. Like std::vector<T>, i=
t can store an arbitrarily-long random-access sequence of T that can change=
in size:</div><div><br></div><div>For example:</div><div><br></div><div>&n=
bsp; constexpr std::literal_vector<int> concat(std::literal_ve=
ctor<int> a, std::literal_vector<int> b)</div><div>  =
; {</div><div> std::literal_vector<int> c;=
</div><div><br></div><div> for (auto x : a)</div=
><div> c.push_back(x);</div><div><=
br></div><div> for (auto x : b)</div><div> =
c.push_back(x);</div><div><br></div><di=
v> return c;</div><div> }</div><div=
><br></div><div>Notice that concat is not a function template, it is just a=
regular constexpr function.</div><div><br></div><div>The implementation of=
std::literal_vector<T> requires internal compiler support (it cannot=
be implemented in pure library code), but it does not require core languag=
e changes.</div><div><br></div><div>User-defined literal types can then be =
composed, without further compiler support, by using one or more std::liter=
al_vector<T> as subobjects. Using literal_vector<T> in th=
is fashion subsumes the use cases for predynamic storage duration. ne=
w and delete expressions in constant expressions and constexpr functions ar=
e no longer needed. You can just use std::literal_vector<T> if =
you need "predynamic" storage duration at compile-time. The mechanism=
by which std::literal_vector<T> is implemented becomes an implementa=
tion detail not exposed to the user. That is, std::literal_vector<=
T> becomes the fundamental primitive that is used to build more complex =
literal types.</div><div><br></div><div>For example, using std::literal_vec=
tor<T> we can see how to create a non-template string_literal class o=
f literal type:</div><div><br></div><div> struct string_litera=
l</div><div> {</div><div> ...</div>=
<div><br></div><div> private:</div><div> &=
nbsp; std::literal_vector<char> data;</div><div> };</div=
><div><br></div><div>That was easy - but notice we can also create a binary=
tree of literal type:</div><div><br></div><div> template<c=
lass T></div><div> struct node</div><div> {</d=
iv><div> T value;</div><div> =
size_t L, R; // index of subtrees in binary_tree::nodes</div><div>&=
nbsp; };</div><div><br></div><div> template<class T&=
gt;</div><div> struct binary_tree</div><div> {</d=
iv><div> size_t root; // index of root node in n=
odes<br></div><div> std::literal_vector<node&=
gt; nodes;</div><div> };</div><div><br></div><div>You can imag=
ine how to add a free list to reuse nodes and so on, but you get the idea -=
from here, it doesn't require much imagination to see how literal_vectors =
can be used as an "address space" to build arbitrarily complex self-referen=
tial data structures:</div><div><br></div><div> struct vertex<=
br></div><div> {</div><div> std::li=
teral_vector<size_t> edges; // indexes into digraph::verticies</div><=
div> };</div><div><br></div><div> struct digraph<=
/div><div> {</div><div> std::litera=
l_vector<vertex> verticies;</div><div> };</div><div><br>=
</div><div> template<class T></div><div> st=
ruct bucket</div><div> {</div><div> =
hash_t hash_code;</div><div> std::literal_vecto=
r<T> contents;</div><div> };</div><div><br></div><div>&n=
bsp; template<class T></div><div> struct dynamic_ha=
sh_table</div><div> {</div><div> std:=
:literal_vector<bucket<T>> buckets;</div><div> };</=
div><div><br></div><div> template<class T></div><div>&nbs=
p; struct matrix</div><div> {</div><div> &nb=
sp; ...</div><div><br></div><div> private:</div><div>&nbs=
p; std::literal_vector<T> cells; // stored in row=
-major order</div><div> };</div><div><br></div><div>and so fort=
h.</div><div><br></div><div>Feedback appreciated.</div><div><br></div><div>=
[1]: https://groups.google.com/a/isocpp.org/d/msg/std-proposals/-GbnSNmJ8bE=
/z3yvFxtzC6AJ<br></div><div><br></div></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_812_5732476.1395537506230--
.