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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1714 - (view) (download) (as text)

1 : jhr 1691 /*! \file dyn-seq.h
2 :     *
3 :     * \author John Reppy
4 :     */
5 :    
6 :     /*
7 :     * COPYRIGHT (c) 2012 The Diderot Project (http://diderot-language.cs.uchicago.edu)
8 :     * All rights reserved.
9 :     */
10 :    
11 :     #ifndef _DIDEROT_DYN_SEQ_H_
12 :     #define _DIDEROT_DYN_SEQ_H_
13 :    
14 :     typedef struct {
15 :     unsigned int base;
16 : jhr 1714 size_t nElems; //!< number of elements in the sequence
17 :     size_t size; //!< size of data buffer in elements
18 :     void *data; //!< pointer to underlying data buffer
19 : jhr 1691 } Diderot_DynSeq_t;
20 :    
21 :     #define DYNSEQ_SUB(ty, seq, i) (*((ty *)Diderot_DynSeqAddr(sizeof(ty), seq, i)))
22 :     #define DYNSEQ_MK(ty, n, data) Diderot_DynSeqMk(sizeof(ty), n, data)
23 :    
24 : jhr 1714 inline unsigned int Diderot_DynSeqLength (Diderot_DynSeq_t *seq)
25 :     {
26 :     return seq->nElems;
27 :     }
28 :    
29 : jhr 1691 inline Diderot_DynSeq_t *Diderot_DynSeqAlloc (size_t elemSz, int initialSz)
30 :     {
31 :     Diderot_DynSeq_t *seq = NEW(Diderot_DynSeq_t);
32 :     seq->base = 0;
33 :     seq->nElems = initialSz;
34 :     seq->size = initialSz; // FIXME: allow some padding
35 :     seq->data = CheckedAlloc (elemSz * seq->size);
36 :     return seq;
37 :     }
38 :    
39 :     inline Diderot_DynSeq_t *Diderot_DynSeqMk (size_t elemSz, int nElems, void *elems)
40 :     {
41 :     Diderot_DynSeq_t *seq = Diderot_DynSeqAlloc(elemSz, nElems);
42 :     unsigned char *p = (unsigned char *)seq->data;
43 :     unsigned char *q = (unsigned char *)elems;
44 :     for (int i = 0; i < nElems; i++) {
45 :     memcpy(p, q, elemSz);
46 :     p += elemSz;
47 :     q += elemSz;
48 :     }
49 :     return seq;
50 :     }
51 :    
52 :     inline void *Diderot_DynSeqAddr (size_t elemSz, Diderot_DynSeq_t *seq, int i)
53 :     {
54 :     i = (seq->base + i) % seq->size;
55 :     return ((char *)(seq->data) + elemSz * i);
56 :     }
57 :    
58 : jhr 1714 //! append an element to a sequence
59 : jhr 1691 Diderot_DynSeq_t *Diderot_DynSeqAppend (size_t elemSz, Diderot_DynSeq_t *, void *);
60 : jhr 1714
61 :     //! prepend an element to a sequence
62 : jhr 1691 Diderot_DynSeq_t *Diderot_DynSeqPrepend (size_t elemSz, void *, Diderot_DynSeq_t *);
63 : jhr 1714
64 :     //! concatenate two sequences
65 : jhr 1691 Diderot_DynSeq_t *Diderot_DynSeqConcat (size_t elemSz, Diderot_DynSeq_t *, Diderot_DynSeq_t *);
66 :    
67 : jhr 1714 //! \brief copy the elements of a sequence to an array
68 :     //! \param elemSz the size of the sequence elements in bytes
69 :     //! \param dst the destination array
70 :     //! \param seq the source sequence
71 :     //! \return the address of the element that follows the array
72 :     void *Diderot_DynSeqCopy (size_t elemSz, void *dst, Diderot_DynSeq_t *seq);
73 :    
74 : jhr 1691 #endif /* !_DIDEROT_DYN_SEQ_H_ */

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