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 3988 - (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 3981 template <typename T>
27 :     class dynseq {
28 :     public:
29 :    
30 :     //! construct an empty sequence
31 :     dynseq ();
32 :     //! construct a sequence from a fixed-size array
33 :     dynseq (uint32_t n, const T *data);
34 :     //! construct a sequence from an array initializer (e.g., `dynseq{1, 2, 3}`)
35 :     dynseq (std::initializer_list<T> il);
36 : jhr 3982 //! construct a sequence for a range lo..hi (integer types only)
37 :     dynseq (T const &lo, T const &hi);
38 :     //! copy constructor; this just copies the reference
39 : jhr 3981 dynseq (const dynseq &seq);
40 : jhr 3982 //! copy constructor with additional space reserved
41 :     dynseq (const dynseq &seq, uint32_t n);
42 : jhr 3981
43 :     ~dynseq ();
44 :    
45 : jhr 3982 uint32_t length () const { return this->_seq->size(); }
46 :    
47 : jhr 3985 const T& operator[] (uint32_t i) const { return (*this->_seq)[i]; }
48 : jhr 3981
49 : jhr 3985 typename std::vector<T>::const_iterator cbegin() const noexcept
50 : jhr 3981 {
51 :     return this->_seq->cbegin();
52 :     }
53 : jhr 3985 typename std::vector<T>::const_iterator cend() const noexcept
54 : jhr 3981 {
55 :     return this->_seq->cend();
56 :     }
57 :    
58 : jhr 3982 // destructive operations for adding elements to a sequence
59 :     dynseq &prepend (T const &x);
60 :     dynseq &append (T const &x);
61 :     dynseq &concat (dynseq const &seq);
62 :    
63 :     // wrappers for the above operations
64 :     static dynseq prepend (T const &x, dynseq<T> const &seq)
65 :     {
66 :     dynseq result(seq, 1);
67 :     return result.prepend(x);
68 :     }
69 :     static dynseq append (dynseq const &seq, T const &x)
70 :     {
71 :     dynseq result(seq, 1);
72 :     return result.append(x);
73 :     }
74 :     static dynseq concat (dynseq const &seq1, dynseq<T> const &seq2)
75 :     {
76 :     dynseq result(seq1, seq2.length());
77 :     return result.concat(seq2);
78 :     }
79 :    
80 : jhr 3988 void *data () const { return this->_seq->data(); }
81 :    
82 : jhr 3981 private:
83 :     std::shared_ptr<std::vector<T>> _seq;
84 :     };
85 :    
86 :     /***** inline functions *****/
87 :    
88 :     template <typename T>
89 :     dynseq<T>::dynseq ()
90 : jhr 3985 : _seq(std::make_shared<std::vector<T>>(std::vector<T>()))
91 : jhr 3981 { }
92 :    
93 :     template <typename T>
94 :     dynseq<T>::dynseq (uint32_t n, const T *data)
95 : jhr 3985 : _seq(std::make_shared<std::vector<T>>(std::vector<T>()))
96 : jhr 3981 {
97 : jhr 3982 this->_seq->reserve (n);
98 : jhr 3981 std::memcpy (this->_seq->data(), data, n * sizeof(T));
99 :     }
100 :    
101 :     template <typename T>
102 :     dynseq<T>::dynseq (std::initializer_list<T> il)
103 : jhr 3985 : _seq(std::make_shared<std::vector<T>>(std::vector<T>(il)))
104 : jhr 3981 { }
105 :    
106 : jhr 3988 /***** functions for loading sequences from nrrd files and objects *****/
107 :    
108 :     //! load from nrrd; type-specialized versions of this function are generated by the compiler
109 :     //
110 :     template <typename T>
111 :     dynseq<T> load_dynseq_from_nrrd (const Nrrd *nin, bool &sts);
112 :    
113 :     template <typename T>
114 :     dynseq<T> load_dynseq_from_file (world_base *wrld, std::string const &file, bool &sts);
115 :    
116 : jhr 3981 } // namespace diderot
117 :    
118 : jhr 3987 #include "dynseq-inst.hxx"
119 :    
120 : jhr 3981 #endif // !_DIDEROT_DYNSEQ_HXX_

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