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

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