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 1691 - (download) (as text) (annotate)
Thu Jan 26 19:36:51 2012 UTC (7 years, 7 months ago) by jhr
File size: 1663 byte(s)
  Working on dynamic sequences.
/*! \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;
    size_t      size;
    void        *data;
} 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 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);
}

Diderot_DynSeq_t *Diderot_DynSeqAppend (size_t elemSz, Diderot_DynSeq_t *, void *);
Diderot_DynSeq_t *Diderot_DynSeqPrepend (size_t elemSz, void *, Diderot_DynSeq_t *);
Diderot_DynSeq_t *Diderot_DynSeqConcat (size_t elemSz, Diderot_DynSeq_t *, Diderot_DynSeq_t *);

#endif /* !_DIDEROT_DYN_SEQ_H_ */

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