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

SCM Repository

[diderot] Annotation of /branches/vis12/src/lib/common/dyn-seq.c
ViewVC logotype

Annotation of /branches/vis12/src/lib/common/dyn-seq.c

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : jhr 1733 /*! \file dyn-seq.c
2 :     *
3 :     * \author John Reppy
4 :     *
5 :     * An implementation of dynamic sequences.
6 :     */
7 :    
8 :     /*
9 :     * COPYRIGHT (c) 2012 The Diderot Project (http://diderot-language.cs.uchicago.edu)
10 :     * All rights reserved.
11 :     */
12 :    
13 :     #include "Diderot/diderot.h"
14 :    
15 :     // compute the address of the i'th element in the sequence
16 :     STATIC_INLINE void* ElemAddr(void *base, size_t elemSz, unsigned int i)
17 :     {
18 :     return (void *)((char *)base + i*elemSz);
19 :     }
20 :    
21 :     //! append an element to a sequence
22 :     Diderot_DynSeq_t *Diderot_DynSeqAppend (size_t elemSz, Diderot_DynSeq_t *seq, void *item)
23 :     {
24 :     if (seq->nElems == seq->size) {
25 :     // grow the data array
26 :     unsigned int newSize = (seq->size < 64) ? seq->size + 8 : seq->size + 64;
27 :     void *newData = CheckedAlloc (elemSz * newSize);
28 :     Diderot_DynSeqCopy (elemSz, newData, seq);
29 :     seq->size = newSize;
30 :     seq->base = 0;
31 :     FREE(seq->data);
32 :     seq->data = newData;
33 :     }
34 :     unsigned int i = (seq->base + seq->nElems) % seq->size;
35 : jhr 1906 seq->nElems++;
36 : jhr 1733 memcpy(ElemAddr(seq->data, elemSz, i), item, elemSz);
37 :     return seq;
38 :     }
39 :    
40 :     //! prepend an element to a sequence
41 :     //Diderot_DynSeq_t *Diderot_DynSeqPrepend (size_t elemSz, void *, Diderot_DynSeq_t *);
42 :    
43 :     //! concatenate two sequences
44 :     //Diderot_DynSeq_t *Diderot_DynSeqConcat (size_t elemSz, Diderot_DynSeq_t *, Diderot_DynSeq_t *);
45 :    
46 :     //! \brief copy the elements of a sequence to an array
47 :     //! \param elemSz the size of the sequence elements in bytes
48 :     //! \param dst the destination array
49 :     //! \param seq the source sequence
50 :     //! \return the address of the element that follows the array
51 :     void *Diderot_DynSeqCopy (size_t elemSz, void *dst, Diderot_DynSeq_t *seq)
52 :     {
53 :     unsigned int n = seq->nElems;
54 :     if (seq->base + n > seq->size) {
55 :     // sequence wraps around, so we need to copy it in two steps
56 :     n = seq->size - seq->base;
57 :     memcpy (dst, ElemAddr(seq->data, elemSz, seq->base), n*elemSz);
58 :     dst = (void *)((char *)dst + n*elemSz);
59 :     n = seq->nElems - n;
60 :     memcpy (dst, ElemAddr(seq->data, elemSz, 0), n*elemSz);
61 :     return (void *)((char *)dst + n*elemSz);
62 :     }
63 :     else {
64 :     size_t cpySz = n*elemSz;
65 :     memcpy (dst, ElemAddr(seq->data, elemSz, seq->base), cpySz);
66 :     return (void *)((char *)dst + cpySz);
67 :     }
68 :     }

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