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 1714 - (download) (as text) (annotate)
Sat Mar 10 00:00:50 2012 UTC (7 years, 5 months ago) by jhr
File size: 2309 byte(s)
  Working on library generation
/*! \file dyn-seq.h
 *
 * \author John Reppy
 */

/*
 * COPYRIGHT (c) 2012 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 */

#ifndef _DIDEROT_DYN_SEQ_H_
#define _DIDEROT_DYN_SEQ_H_

typedef struct {
    unsigned int base;
    size_t      nElems;		//!< number of elements in the sequence
    size_t      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)

inline unsigned int Diderot_DynSeqLength (Diderot_DynSeq_t *seq)
{
    return seq->nElems;
}
 
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;  // FIXME: allow some padding
    seq->data = CheckedAlloc (elemSz * seq->size);
    return seq;
}

inline Diderot_DynSeq_t *Diderot_DynSeqMk (size_t elemSz, int nElems, void *elems)
{
    Diderot_DynSeq_t *seq = Diderot_DynSeqAlloc(elemSz, nElems);
    unsigned char *p = (unsigned char *)seq->data;
    unsigned char *q = (unsigned char *)elems;
    for (int i = 0;  i < nElems;  i++) {
        memcpy(p, q, elemSz);
        p += elemSz;
        q += elemSz;
    }
    return seq;
}

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);

#endif /* !_DIDEROT_DYN_SEQ_H_ */

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