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 4065 - (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 :     template <typename T> using load_fn_ptr = T (*) (const void *);
28 :     } // __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 4028 using traits = dynseq_traits<T>;
42 :    
43 : jhr 3981 //! construct an empty sequence
44 :     dynseq ();
45 : jhr 4028 //! construct an uninitialized sequence with the given number of elements
46 :     dynseq (uint32_t n);
47 : jhr 3981 //! construct a sequence from a fixed-size array
48 :     dynseq (uint32_t n, const T *data);
49 :     //! construct a sequence from an array initializer (e.g., `dynseq{1, 2, 3}`)
50 :     dynseq (std::initializer_list<T> il);
51 : jhr 3982 //! construct a sequence for a range lo..hi (integer types only)
52 :     dynseq (T const &lo, T const &hi);
53 :     //! copy constructor; this just copies the reference
54 : jhr 3981 dynseq (const dynseq &seq);
55 : jhr 3982 //! copy constructor with additional space reserved
56 :     dynseq (const dynseq &seq, uint32_t n);
57 : jhr 3981
58 : jhr 4031 ~dynseq () { };
59 : jhr 3981
60 : jhr 3982 uint32_t length () const { return this->_seq->size(); }
61 :    
62 : jhr 3985 const T& operator[] (uint32_t i) const { return (*this->_seq)[i]; }
63 : jhr 3981
64 : jhr 3985 typename std::vector<T>::const_iterator cbegin() const noexcept
65 : jhr 3981 {
66 :     return this->_seq->cbegin();
67 :     }
68 : jhr 3985 typename std::vector<T>::const_iterator cend() const noexcept
69 : jhr 3981 {
70 :     return this->_seq->cend();
71 :     }
72 :    
73 : jhr 3982 // destructive operations for adding elements to a sequence
74 :     dynseq &prepend (T const &x);
75 :     dynseq &append (T const &x);
76 :     dynseq &concat (dynseq const &seq);
77 :    
78 :     // wrappers for the above operations
79 :     static dynseq prepend (T const &x, dynseq<T> const &seq)
80 :     {
81 :     dynseq result(seq, 1);
82 :     return result.prepend(x);
83 :     }
84 :     static dynseq append (dynseq const &seq, T const &x)
85 :     {
86 :     dynseq result(seq, 1);
87 :     return result.append(x);
88 :     }
89 :     static dynseq concat (dynseq const &seq1, dynseq<T> const &seq2)
90 :     {
91 :     dynseq result(seq1, seq2.length());
92 :     return result.concat(seq2);
93 :     }
94 :    
95 : jhr 3988 void *data () const { return this->_seq->data(); }
96 :    
97 : jhr 4065 // copy the contents of the sequence to the specified address and return the address
98 :     // immediately following the copied data
99 :     char *copy_to (char *cp);
100 :    
101 : jhr 3981 private:
102 :     std::shared_ptr<std::vector<T>> _seq;
103 :     };
104 :    
105 :     /***** inline functions *****/
106 :    
107 :     template <typename T>
108 : jhr 4032 inline dynseq<T>::dynseq ()
109 : jhr 3985 : _seq(std::make_shared<std::vector<T>>(std::vector<T>()))
110 : jhr 3981 { }
111 :    
112 :     template <typename T>
113 : jhr 4032 inline dynseq<T>::dynseq (uint32_t nelems)
114 : jhr 4030 : _seq(std::make_shared<std::vector<T>>(std::vector<T>(nelems)))
115 : jhr 4028 { }
116 :    
117 :     template <typename T>
118 : jhr 4032 inline dynseq<T>::dynseq (uint32_t n, const T *data)
119 : jhr 3985 : _seq(std::make_shared<std::vector<T>>(std::vector<T>()))
120 : jhr 3981 {
121 : jhr 3982 this->_seq->reserve (n);
122 : jhr 3981 std::memcpy (this->_seq->data(), data, n * sizeof(T));
123 :     }
124 :    
125 :     template <typename T>
126 : jhr 4032 inline dynseq<T>::dynseq (std::initializer_list<T> il)
127 : jhr 3985 : _seq(std::make_shared<std::vector<T>>(std::vector<T>(il)))
128 : jhr 3981 { }
129 :    
130 : jhr 4032 template <typename T>
131 :     inline dynseq<T>::dynseq (dynseq const &seq)
132 :     : _seq(seq._seq)
133 :     { }
134 :    
135 : jhr 3988 /***** functions for loading sequences from nrrd files and objects *****/
136 :    
137 :     //! load from nrrd; type-specialized versions of this function are generated by the compiler
138 :     //
139 :     template <typename T>
140 : jhr 4030 dynseq<T> load_dynseq_from_nrrd (world_base *wrld, const Nrrd *nin, bool &sts);
141 : jhr 3988
142 :     template <typename T>
143 :     dynseq<T> load_dynseq_from_file (world_base *wrld, std::string const &file, bool &sts);
144 :    
145 : jhr 3981 } // namespace diderot
146 :    
147 : jhr 3987 #include "dynseq-inst.hxx"
148 :    
149 : jhr 3981 #endif // !_DIDEROT_DYNSEQ_HXX_

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