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

SCM Repository

[diderot] View of /branches/vis12/src/include/Diderot/dyn-seq.h
ViewVC logotype

View of /branches/vis12/src/include/Diderot/dyn-seq.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3291 - (download) (as text) (annotate)
Wed Oct 14 21:25:00 2015 UTC (3 years, 11 months ago) by jhr
File size: 5303 byte(s)
code is copyright University of Chicago
/*! \file dyn-seq.h
 *
 * \author John Reppy
 */

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

#ifndef _DIDEROT_DYN_SEQ_H_
#define _DIDEROT_DYN_SEQ_H_

typedef struct {
    unsigned int base;		//!< data[base] is the first element in the sequence
    unsigned int nElems;	//!< number of elements in the sequence
    unsigned int size;		//!< size of data buffer in elements
    void        *data;		//!< pointer to underlying data buffer
} Diderot_DynSeq_t;

#define DYNSEQ_SUB(ty, seq, i)  (*((ty *)Diderot_DynSeqAddr(sizeof(ty), seq, i)))
#define DYNSEQ_MK(ty, n, data)  Diderot_DynSeqMk(sizeof(ty), n, data)

STATIC_INLINE unsigned int Diderot_DynSeqLength (Diderot_DynSeq_t *seq)
{
    return seq->nElems;
}
 
STATIC_INLINE Diderot_DynSeq_t *Diderot_DynSeqAlloc (size_t elemSz, int initialSz)
{
    Diderot_DynSeq_t *seq = NEW(Diderot_DynSeq_t);
    seq->base = 0;
    seq->nElems = initialSz;
    seq->size = initialSz + 8;  // FIXME: be smarter about padding
    seq->data = CheckedAlloc (elemSz * seq->size);
    return seq;
}

STATIC_INLINE Diderot_DynSeq_t *Diderot_DynSeqMk (size_t elemSz, int nElems, void *elems)
{
    Diderot_DynSeq_t *seq = Diderot_DynSeqAlloc(elemSz, nElems);
    memcpy(seq->data, elems, elemSz * nElems);
    return seq;
}

STATIC_INLINE void Diderot_DynSeqFree (Diderot_DynSeq_t *seq)
{
    free (seq->data);
    free (seq);
}

STATIC_INLINE void *Diderot_DynSeqAddr (size_t elemSz, Diderot_DynSeq_t *seq, int i)
{
    i = (seq->base + i) % seq->size;
    return ((char *)(seq->data) + elemSz * i);
}

//! append an element to a sequence
Diderot_DynSeq_t *Diderot_DynSeqAppend (size_t elemSz, Diderot_DynSeq_t *, void *);

//! prepend an element to a sequence
Diderot_DynSeq_t *Diderot_DynSeqPrepend (size_t elemSz, void *, Diderot_DynSeq_t *);

//! concatenate two sequences
Diderot_DynSeq_t *Diderot_DynSeqConcat (size_t elemSz, Diderot_DynSeq_t *, Diderot_DynSeq_t *);

//! \brief copy the elements of a sequence to an array
//! \param elemSz the size of the sequence elements in bytes
//! \param dst the destination array
//! \param seq the source sequence
//! \return the address of the element that follows the array
void *Diderot_DynSeqCopy (size_t elemSz, void *dst, Diderot_DynSeq_t *seq);


//! load a dynamic sequence from a Nrrd file, where the sequence elements have bool ground type.
//! \param wrld the world; used to report errors
//! \param nin the nrrd to check
//! \param nDims the number of dimensions in the sequence elements
//! \param dims the array of sequence element dimensions
//! \return the dynamic sequence, or zero on error
Diderot_DynSeq_t *Diderot_DynSeqLoadBool (WorldPrefix_t *wrld, Nrrd *nin, unsigned int nDims, unsigned int *dims);

//! load a dynamic sequence from a Nrrd file, where the sequence elements have bool ground type.
//! \param wrld the world; used to report errors
//! \param name the name of the nrrd file to load
//! \param nDims the number of dimensions in the sequence elements
//! \param dims the array of sequence element dimensions
//! \return the dynamic sequence, or zero on error
Diderot_DynSeq_t *Diderot_DynSeqLoadBoolFromFile (WorldPrefix_t *wrld, const char *name, unsigned int nDims, unsigned int *dims);

//! load a dynamic sequence from a Nrrd file, where the sequence elements have int ground type.
//! \param wrld the world; used to report errors
//! \param nin the nrrd to check
//! \param nDims the number of dimensions in the sequence elements
//! \param dims the array of sequence element dimensions
//! \return the dynamic sequence, or zero on error
Diderot_DynSeq_t *Diderot_DynSeqLoadInt (WorldPrefix_t *wrld, Nrrd *nin, unsigned int nDims, unsigned int *dims);

//! load a dynamic sequence from a Nrrd file, where the sequence elements have int ground type.
//! \param wrld the world; used to report errors
//! \param name the name of the nrrd file to load
//! \param nDims the number of dimensions in the sequence elements
//! \param dims the array of sequence element dimensions
//! \return the dynamic sequence, or zero on error
Diderot_DynSeq_t *Diderot_DynSeqLoadIntFromFile (WorldPrefix_t *wrld, const char *name, unsigned int nDims, unsigned int *dims);

//! load a dynamic sequence from a Nrrd file, where the sequence elements have real ground type.
//! \param wrld the world; used to report errors
//! \param nin the nrrd to check
//! \param nDims the number of dimensions in the sequence elements
//! \param dims the array of sequence element dimensions
//! \return the dynamic sequence, or zero on error
Diderot_DynSeq_t *Diderot_DynSeqLoadReal (WorldPrefix_t *wrld, Nrrd *nin, unsigned int nDims, unsigned int *dims);

//! load a dynamic sequence from a Nrrd file, where the sequence elements have real ground type.
//! \param wrld the world; used to report errors
//! \param name the name of the nrrd file to load
//! \param nDims the number of dimensions in the sequence elements
//! \param dims the array of sequence element dimensions
//! \return the dynamic sequence, or zero on error
Diderot_DynSeq_t *Diderot_DynSeqLoadRealFromFile (WorldPrefix_t *wrld, const char *name, unsigned int nDims, unsigned int *dims);

#endif /* !_DIDEROT_DYN_SEQ_H_ */

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