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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log

Revision 4030 - (download) (as text) (annotate)
Thu Jun 23 04:28:53 2016 UTC (3 years, 3 months ago) by jhr
File size: 3918 byte(s)
  Working on merge: dynamic sequence loading
/*! \file dynseq.hxx
 * \author John Reppy

 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
 * COPYRIGHT (c) 2016 The University of Chicago
 * All rights reserved.


#include <memory>
#include <vector>
#include <initializer_list>
#include <teem/nrrd.h>

namespace diderot {

// QUESTION: can we use some type-level hacking to connect the base element type, shape,
// and sequence element type together (would help with resolving loading too)?

    namespace __details {
	template <typename T> using load_fn_ptr = T (*) (const void *);
    } // __details

    template <typename T> struct dynseq_traits;
    // using value_type = ...;
    // using base_type = ...;
    // static __details::load_fn_ptr<base_type> get_load_fn (int ty);
    // static const uint32_t ndims;
    // static const uint32_t dims[ndims] = { ... };

    template <typename T>
    class dynseq {

	using traits = dynseq_traits<T>;

      //! construct an empty sequence
	dynseq ();
      //! construct an uninitialized sequence with the given number of elements
	dynseq (uint32_t n);
      //! construct a sequence from a fixed-size array
	dynseq (uint32_t n, const T *data);
      //! construct a sequence from an array initializer (e.g., `dynseq{1, 2, 3}`)
	dynseq (std::initializer_list<T> il);
      //! construct a sequence for a range lo..hi (integer types only)
	dynseq (T const &lo, T const &hi);
      //! copy constructor; this just copies the reference
	dynseq (const dynseq &seq);
      //! copy constructor with additional space reserved
	dynseq (const dynseq &seq, uint32_t n);

	~dynseq ();

	uint32_t length () const { return this->_seq->size(); }

	const T& operator[] (uint32_t i) const { return (*this->_seq)[i]; }

	typename std::vector<T>::const_iterator cbegin() const noexcept
	    return this->_seq->cbegin();
	typename std::vector<T>::const_iterator cend() const noexcept
	    return this->_seq->cend();

      // destructive operations for adding elements to a sequence
	dynseq &prepend (T const &x);
	dynseq &append (T const &x);
	dynseq &concat (dynseq const &seq);

      // wrappers for the above operations
	static dynseq prepend (T const &x, dynseq<T> const &seq)
	    dynseq result(seq, 1);
	    return result.prepend(x);
	static dynseq append (dynseq const &seq, T const &x)
	    dynseq result(seq, 1);
	    return result.append(x);
	static dynseq concat (dynseq const &seq1, dynseq<T> const &seq2)
	    dynseq result(seq1, seq2.length());
	    return result.concat(seq2);

	void *data () const { return this->_seq->data(); }

	std::shared_ptr<std::vector<T>> _seq;

  /***** inline functions *****/

    template <typename T>
    dynseq<T>::dynseq ()
	: _seq(std::make_shared<std::vector<T>>(std::vector<T>()))
    { }

    template <typename T>
    dynseq<T>::dynseq (uint32_t nelems)
	: _seq(std::make_shared<std::vector<T>>(std::vector<T>(nelems)))
    { }

    template <typename T>
    dynseq<T>::dynseq (uint32_t n, const T *data)
	: _seq(std::make_shared<std::vector<T>>(std::vector<T>()))
	this->_seq->reserve (n);
	std::memcpy (this->_seq->data(), data, n * sizeof(T));

    template <typename T>
    dynseq<T>::dynseq (std::initializer_list<T> il)
	: _seq(std::make_shared<std::vector<T>>(std::vector<T>(il)))
    { }

  /***** functions for loading sequences from nrrd files and objects *****/

  //! load from nrrd; type-specialized versions of this function are generated by the compiler
    template <typename T>
    dynseq<T> load_dynseq_from_nrrd (world_base *wrld, const Nrrd *nin, bool &sts);

    template <typename T>
    dynseq<T> load_dynseq_from_file (world_base *wrld, std::string const &file, bool &sts);

} // namespace diderot

#include "dynseq-inst.hxx"


ViewVC Help
Powered by ViewVC 1.0.0