1/*
2    File:       DynamicArray.h
3
4    Contains:   Interface to the CDynamicArray class
5
6
7*/
8
9#ifndef __CDYNAMICARRAY_H
10#define __CDYNAMICARRAY_H
11
12#include "IrDATypes.h"
13
14
15//--------------------------------------------------------------------------------
16//      Forward and external class declarations
17//--------------------------------------------------------------------------------
18
19class CArrayIterator;
20    // CArrayIterator knows how to traverse a CDynamicArray.
21    // In particular, it will bend indexes to account for on-the-fly
22    // element insertion and deletion.
23
24
25enum Parameters {
26    kDefaultElementSize = sizeof(void *),   // was 4
27    kDefaultChunkSize = sizeof(void *)	    // 4
28};
29
30//--------------------------------------------------------------------------------
31//      CDynamicArray
32//--------------------------------------------------------------------------------
33
34class CDynamicArray : public OSObject
35{
36    OSDeclareDefaultStructors(CDynamicArray);
37
38public:
39
40	    static CDynamicArray *cDynamicArray(Size elementSize = kDefaultElementSize,
41						ArrayIndex chunkSize = kDefaultChunkSize);
42	    Boolean     init(Size elementSize = kDefaultElementSize, ArrayIndex chunkSize = kDefaultChunkSize);
43	    void        free();
44
45    // array manipulation primitives
46
47	    ArrayIndex  GetArraySize(void);
48	    IrDAErr     SetArraySize(ArrayIndex theSize);
49	    IrDAErr     SetElementCount(ArrayIndex theSize);        // like SetArraySize, but sets logical size, too
50
51	    void*       ElementPtrAt(ArrayIndex index);
52	    void*       SafeElementPtrAt(ArrayIndex index);
53	    IrDAErr     GetElementsAt(ArrayIndex index, void* elemPtr, ArrayIndex count);
54	    IrDAErr     InsertElementsBefore(ArrayIndex startHere, void* elemPtr, ArrayIndex count);
55	    IrDAErr     ReplaceElementsAt(ArrayIndex index, void* elemPtr, ArrayIndex count);
56	    IrDAErr     RemoveElementsAt(ArrayIndex index, ArrayIndex count);
57	    IrDAErr     RemoveAll(void);
58
59    // miscellaneous functions
60
61	    Boolean     IsEmpty(void);
62
63	    IrDAErr     Merge(CDynamicArray* aDynamicArray);
64
65protected:
66
67	Size            ComputeByteCount(ArrayIndex count);
68
69	ArrayIndex      fSize;          // logical size of array
70
71private:
72
73	friend class CArrayIterator;
74
75	Size            fElementSize;   // size of a single element
76	ArrayIndex      fChunkSize;     // grow/shrink array by this many elements
77	ArrayIndex      fAllocatedSize; // physical size of array
78	void*           fArrayBlock;    // element storage
79	CArrayIterator* fIterator;      // linked list of iterators active on this array
80
81}; // CDynamicArray
82
83
84//--------------------------------------------------------------------------------
85//      inline functions
86//--------------------------------------------------------------------------------
87
88inline ArrayIndex CDynamicArray::GetArraySize()
89    { return fSize; }
90
91inline Boolean CDynamicArray::IsEmpty()
92    { return (fSize == 0); }
93
94inline void* CDynamicArray::ElementPtrAt(ArrayIndex index)
95    { return (void*)((char *)fArrayBlock + (fElementSize * index)); }
96
97inline Size CDynamicArray::ComputeByteCount(ArrayIndex count)
98    { return (fElementSize * count); }
99
100inline IrDAErr CDynamicArray::RemoveAll()
101    { return RemoveElementsAt(0, fSize); }
102
103
104#endif  /*  __CDYNAMICARRAY_H   */
105