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&gt;:</div><div><br></div><div>&nbsp; &nbsp; template&lt;typename T&gt;<=
/div><div>&nbsp; &nbsp; struct literal_vector;</div><div><br></div><div>T m=
ust be a literal type, and for each T, std::literal_vector&lt;T&gt; is a li=
teral type.</div><div><br></div><div>std::literal_vector&lt;T&gt; has an in=
terface similar to std::vector&lt;T&gt;. &nbsp;Like std::vector&lt;T&gt;, 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; &nbsp; constexpr std::literal_vector&lt;int&gt; concat(std::literal_ve=
ctor&lt;int&gt; a, std::literal_vector&lt;int&gt; b)</div><div>&nbsp; &nbsp=
; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; std::literal_vector&lt;int&gt; c;=
</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; for (auto x : a)</div=
><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.push_back(x);</div><div><=
br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; for (auto x : b)</div><div>&nbsp;=
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.push_back(x);</div><div><br></div><di=
v>&nbsp; &nbsp; &nbsp; &nbsp; return c;</div><div>&nbsp; &nbsp; }</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&lt;T&gt; 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&lt;T&gt; as subobjects. &nbsp;Using literal_vector&lt;T&gt; in th=
is fashion subsumes the use cases for predynamic storage duration. &nbsp;ne=
w and delete expressions in constant expressions and constexpr functions ar=
e no longer needed. &nbsp;You can just use std::literal_vector&lt;T&gt; if =
you need "predynamic" storage duration at compile-time. &nbsp;The mechanism=
 by which std::literal_vector&lt;T&gt; is implemented becomes an implementa=
tion detail not exposed to the user. &nbsp;That is, std::literal_vector&lt;=
T&gt; 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&lt;T&gt; we can see how to create a non-template string_literal class o=
f literal type:</div><div><br></div><div>&nbsp; &nbsp; struct string_litera=
l</div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; ...</div>=
<div><br></div><div>&nbsp; &nbsp; private:</div><div>&nbsp; &nbsp; &nbsp; &=
nbsp; std::literal_vector&lt;char&gt; data;</div><div>&nbsp; &nbsp; };</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>&nbsp; &nbsp; template&lt;c=
lass T&gt;</div><div>&nbsp; &nbsp; struct node</div><div>&nbsp; &nbsp; {</d=
iv><div>&nbsp; &nbsp; &nbsp; &nbsp; T value;</div><div>&nbsp; &nbsp; &nbsp;=
 &nbsp; size_t L, R; // index of subtrees in binary_tree::nodes</div><div>&=
nbsp; &nbsp; };</div><div><br></div><div>&nbsp; &nbsp; template&lt;class T&=
gt;</div><div>&nbsp; &nbsp; struct binary_tree</div><div>&nbsp; &nbsp; {</d=
iv><div>&nbsp; &nbsp; &nbsp; &nbsp; size_t root; // index of root node in n=
odes<br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; std::literal_vector&lt;node&=
gt; nodes;</div><div>&nbsp; &nbsp; };</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>&nbsp; &nbsp; struct vertex<=
br></div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; std::li=
teral_vector&lt;size_t&gt; edges; // indexes into digraph::verticies</div><=
div>&nbsp; &nbsp; };</div><div><br></div><div>&nbsp; &nbsp; struct digraph<=
/div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; std::litera=
l_vector&lt;vertex&gt; verticies;</div><div>&nbsp; &nbsp; };</div><div><br>=
</div><div>&nbsp; &nbsp; template&lt;class T&gt;</div><div>&nbsp; &nbsp; st=
ruct bucket</div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp;=
 hash_t hash_code;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; std::literal_vecto=
r&lt;T&gt; contents;</div><div>&nbsp; &nbsp; };</div><div><br></div><div>&n=
bsp; &nbsp;template&lt;class T&gt;</div><div>&nbsp; &nbsp;struct dynamic_ha=
sh_table</div><div>&nbsp; &nbsp;{</div><div>&nbsp; &nbsp; &nbsp; &nbsp;std:=
:literal_vector&lt;bucket&lt;T&gt;&gt; buckets;</div><div>&nbsp; &nbsp;};</=
div><div><br></div><div>&nbsp; &nbsp;template&lt;class T&gt;</div><div>&nbs=
p; &nbsp;struct matrix</div><div>&nbsp; &nbsp;{</div><div>&nbsp; &nbsp; &nb=
sp; &nbsp;...</div><div><br></div><div>&nbsp; &nbsp;private:</div><div>&nbs=
p; &nbsp; &nbsp; &nbsp;std::literal_vector&lt;T&gt; cells; // stored in row=
-major order</div><div>&nbsp; &nbsp;};</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&quot; 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--

.