Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_911 / libraries / libjni-ecwcompress / include / NCSArray.h @ 39111

History | View | Annotate | Download (3.68 KB)

1
/********************************************************
2
** Copyright 1999 Earth Resource Mapping Pty Ltd.
3
** This document contains unpublished source code of
4
** Earth Resource Mapping Pty Ltd. This notice does
5
** not indicate any intention to publish the source
6
** code contained herein.
7
**
8
** FILE:           NCSMalloc.h
9
** CREATED:        Thu Feb 25 09:19:00 WST 1999
10
** AUTHOR:         Simon Cope
11
** PURPOSE:        NCS Memory handling routines.
12
** EDITS:
13
 *******************************************************/
14

    
15
#ifndef NCSARRAY_H
16
#define NCSARRAY_H
17

    
18
#ifdef __cplusplus
19
extern "C" {
20
#endif // __cplusplus
21

    
22
#ifndef NCSMALLOC_H
23
#include "NCSMalloc.h"
24
#endif
25

    
26
/*
27
** Set to cause arrays to grow/shrink by a granular factor instead of for each element.
28
*/
29
#define NCS_ARRAY_GRANULAR
30

    
31
#ifdef NCS_ARRAY_GRANULAR
32

    
33
/*
34
** Granular array factor (nelements to grow/shrink)
35
*/
36
#define NCS_ARRAY_GRANULARITY 16
37
                /*
38
                ((void *)pArray) = (void *)NCSRealloc((void *)(pArray),        \
39
                        ((nElements) + NCS_ARRAY_GRANULARITY) *                \
40
                        sizeof(*(pArray)),                                \
41
                        FALSE);                                                \
42
                */
43

    
44
#define NCSArrayInsertElement(pArray, nElements, nIndex, pElement)        \
45
        if(nElements % NCS_ARRAY_GRANULARITY == 0) {                        \
46
                void *pData = (void *)NCSRealloc((void *)(pArray),        \
47
                        ((nElements) + NCS_ARRAY_GRANULARITY) *                \
48
                        sizeof(*(pArray)),                                \
49
                        FALSE);                                                \
50
                pArray = pData;                                                \
51
        }                                                                \
52
        if((nIndex) < (nElements)) {                                        \
53
                memmove(&((pArray)[(nIndex) + 1]),                        \
54
                        &((pArray)[(nIndex)]),                                \
55
                        ((nElements) - (nIndex)) * sizeof(*(pArray)));        \
56
        }                                                                \
57
        if(pElement) {                                                        \
58
                (pArray)[(nIndex)] = *(pElement);                        \
59
        } else {                                                        \
60
                memset(&((pArray)[(nIndex)]), 0, sizeof(*pArray));        \
61
        }                                                                \
62
        (nElements) += 1
63

    
64
#define NCSArrayAppendElement(pArray, nElements, pElement)                \
65
        NCSArrayInsertElement(pArray, nElements, nElements, pElement)
66

    
67
#define NCSArrayRemoveElement(pArray, nElements, nIndex)                \
68
        if((nIndex) < (nElements) - 1) {                                \
69
                memmove(&((pArray)[(nIndex)]),                                \
70
                        &((pArray)[(nIndex) + 1]),                        \
71
                        ((nElements) - (nIndex) - 1) * sizeof(*(pArray)));        \
72
        }                                                                \
73
        (nElements) -= 1;                                                \
74
        if(nElements % NCS_ARRAY_GRANULARITY == 0) {                        \
75
                if((nElements) > 0) {                                        \
76
                        void *pData = (void *)NCSRealloc((pArray),        \
77
                                (nElements) * sizeof(*(pArray)),        \
78
                                FALSE);                                        \
79
                        pArray = pData;                                        \
80
                } else {                                                \
81
                        NCSFree((pArray));                                \
82
                        pArray = (void*)NULL;                                \
83
                }                                                        \
84
        }
85

    
86
#else /* NCS_ARRAY_GRANULAR */
87

    
88
#define NCSArrayInsertElement(pArray, nElements, nIndex, pElement)        \
89
                                (pArray) = NCSRealloc((pArray),                                                                                \
90
                                                                          (nElements + 1) * sizeof(*(pArray)),                        \
91
                                                                          TRUE);                                                                                \
92
                                if((nIndex) < (nElements)) {                                                                                \
93
                                        memmove(&((pArray)[(nIndex) + 1]),                                                                \
94
                                                        &((pArray)[(nIndex)]),                                                                        \
95
                                                        ((nElements) - (nIndex)) * sizeof(*(pArray)));                        \
96
                                }                                                                                                                                        \
97
                                if(pElement) {                                                                                                                \
98
                                        (pArray)[(nIndex)] = *(pElement);                                                                \
99
                                }                                                                                                                                        \
100
                                (nElements) += 1
101

    
102
#define NCSArrayAppendElement(pArray, nElements, pElement)                                                        \
103
                                NCSArrayInsertElement(pArray, nElements, nElements, pElement)
104

    
105
#define NCSArrayRemoveElement(pArray, nElements, nIndex)                                                        \
106
                                if((nIndex) < (nElements) - 1) {                                                                        \
107
                                        memmove(&((pArray)[(nIndex)]),                                                                        \
108
                                                    &((pArray)[(nIndex) + 1]),                                                                \
109
                                                        ((nElements) - (nIndex) - 1) * sizeof(*(pArray)));                \
110
                                }                                                                                                                                        \
111
                                (nElements) -= 1;                                                                                                        \
112
                                if((nElements) > 0) {                                                                                                \
113
                                        (pArray) = NCSRealloc((pArray),                                                                        \
114
                                                                                  (nElements) * sizeof(*(pArray)),                        \
115
                                                                                  FALSE);                                                                        \
116
                                } else {                                                                                                                        \
117
                                        NCSFree((pArray));                                                                                                \
118
                                        (pArray) = (void*)NULL;                                                                                        \
119
                                }
120

    
121
#endif /* NCS_ARRAY_GRANULAR */
122

    
123
#ifdef __cplusplus
124
}
125
#endif
126

    
127
#endif /* NCSARRAY_H */