Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /branches/vis15/src/lib/include/diderot/dynseq.hxx
ViewVC logotype

Annotation of /branches/vis15/src/lib/include/diderot/dynseq.hxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4354 - (view) (download) (as text)

1 : jhr 3981 /*! \file dynseq.hxx
2 :     *
3 :     * \author John Reppy
4 :     */
5 :    
6 :     /*
7 :     * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
8 :     *
9 :     * COPYRIGHT (c) 2016 The University of Chicago
10 :     * All rights reserved.
11 :     */
12 :    
13 :     #ifndef _DIDEROT_DYNSEQ_HXX_
14 :     #define _DIDEROT_DYNSEQ_HXX_
15 :    
16 :     #include <memory>
17 :     #include <vector>
18 :     #include <initializer_list>
19 :     #include <teem/nrrd.h>
20 :    
21 :     namespace diderot {
22 :    
23 : jhr 3988 // QUESTION: can we use some type-level hacking to connect the base element type, shape,
24 :     // and sequence element type together (would help with resolving loading too)?
25 :    
26 : jhr 4028 namespace __details {
27 : jhr 4351 template <typename T> using load_fn_ptr = T (*) (const void *);
28 : jhr 4028 } // __details
29 :    
30 : jhr 4006 template <typename T> struct dynseq_traits;
31 :     // using value_type = ...;
32 :     // using base_type = ...;
33 : jhr 4028 // static __details::load_fn_ptr<base_type> get_load_fn (int ty);
34 :     // static const uint32_t ndims;
35 :     // static const uint32_t dims[ndims] = { ... };
36 : jhr 4006
37 : jhr 3981 template <typename T>
38 :     class dynseq {
39 :     public:
40 :    
41 : jhr 4351 using traits = dynseq_traits<T>;
42 : jhr 4028
43 : jhr 3981 //! construct an empty sequence
44 : jhr 4351 dynseq ();
45 : jhr 4028 //! construct an uninitialized sequence with the given number of elements
46 : jhr 4351 dynseq (uint32_t n);
47 : jhr 3981 //! construct a sequence from a fixed-size array
48 : jhr 4351 dynseq (uint32_t n, const T *data);
49 : jhr 3981 //! construct a sequence from an array initializer (e.g., `dynseq{1, 2, 3}`)
50 : jhr 4351 dynseq (std::initializer_list<T> il);
51 : jhr 3982 //! construct a sequence for a range lo..hi (integer types only)
52 : jhr 4351 dynseq (T const &lo, T const &hi);
53 : jhr 3982 //! copy constructor; this just copies the reference
54 : jhr 4351 dynseq (const dynseq &seq);
55 : jhr 3982 //! copy constructor with additional space reserved
56 : jhr 4351 dynseq (const dynseq &seq, uint32_t n);
57 : jhr 3981
58 : jhr 4351 ~dynseq () { };
59 : jhr 3981
60 : jhr 4071 // load a sequence from a nrrd or nrrd file; return true on error
61 : jhr 4351 bool load (world_base *wrld, const Nrrd *nin);
62 :     bool load (world_base *wrld, std::string const &file);
63 : jhr 4071
64 : jhr 4351 uint32_t length () const { return this->_seq->size(); }
65 : jhr 3982
66 : jhr 4351 const T& operator[] (uint32_t i) const { return (*this->_seq)[i]; }
67 : jhr 3981
68 : jhr 4351 typename std::vector<T>::const_iterator cbegin() const noexcept
69 :     {
70 :     return this->_seq->cbegin();
71 :     }
72 :     typename std::vector<T>::const_iterator cend() const noexcept
73 :     {
74 :     return this->_seq->cend();
75 :     }
76 : jhr 3981
77 : jhr 3982 // destructive operations for adding elements to a sequence
78 : jhr 4351 dynseq &prepend (T const &x);
79 :     dynseq &append (T const &x);
80 :     dynseq &concat (dynseq const &seq);
81 : jhr 3982
82 :     // wrappers for the above operations
83 : jhr 4351 static dynseq prepend (T const &x, dynseq<T> const &seq)
84 :     {
85 :     dynseq result(seq, 1);
86 :     return result.prepend(x);
87 :     }
88 :     static dynseq append (dynseq const &seq, T const &x)
89 :     {
90 :     dynseq result(seq, 1);
91 :     return result.append(x);
92 :     }
93 :     static dynseq concat (dynseq const &seq1, dynseq<T> const &seq2)
94 :     {
95 :     dynseq result(seq1, seq2.length());
96 :     return result.concat(seq2);
97 :     }
98 : jhr 3982
99 : jhr 4351 void *data () const { return this->_seq->data(); }
100 : jhr 3988
101 : jhr 4065 // copy the contents of the sequence to the specified address and return the address
102 :     // immediately following the copied data
103 : jhr 4351 char *copy_to (char *cp);
104 : jhr 4065
105 : jhr 3981 private:
106 : jhr 4351 std::shared_ptr<std::vector<T>> _seq;
107 : jhr 3981 };
108 :    
109 :     /***** inline functions *****/
110 :    
111 :     template <typename T>
112 : jhr 4032 inline dynseq<T>::dynseq ()
113 : jhr 4351 : _seq(std::make_shared<std::vector<T>>(std::vector<T>()))
114 : jhr 3981 { }
115 :    
116 :     template <typename T>
117 : jhr 4032 inline dynseq<T>::dynseq (uint32_t nelems)
118 : jhr 4351 : _seq(std::make_shared<std::vector<T>>(std::vector<T>(nelems)))
119 : jhr 4028 { }
120 :    
121 :     template <typename T>
122 : jhr 4032 inline dynseq<T>::dynseq (uint32_t n, const T *data)
123 : jhr 4351 : _seq(std::make_shared<std::vector<T>>(std::vector<T>()))
124 : jhr 3981 {
125 : jhr 4351 this->_seq->reserve (n);
126 : jhr 4354 for (int i = 0; i < n; i++) {
127 :     this->_seq->push_back(data[i]);
128 :     }
129 : jhr 3981 }
130 :    
131 :     template <typename T>
132 : jhr 4032 inline dynseq<T>::dynseq (std::initializer_list<T> il)
133 : jhr 4351 : _seq(std::make_shared<std::vector<T>>(std::vector<T>(il)))
134 : jhr 3981 { }
135 :    
136 : jhr 4032 template <typename T>
137 :     inline dynseq<T>::dynseq (dynseq const &seq)
138 : jhr 4351 : _seq(seq._seq)
139 : jhr 4032 { }
140 :    
141 : jhr 3981 } // namespace diderot
142 :    
143 : jhr 3987 #include "dynseq-inst.hxx"
144 :    
145 : jhr 3981 #endif // !_DIDEROT_DYNSEQ_HXX_

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0