]>
git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/d/dmd/root/array.h
ed128b93dd753b2d8053d659e0b18d372e4b9df1
1 /* Copyright (C) 2011-2018 by The D Language Foundation, All Rights Reserved
2 * http://www.digitalmars.com
3 * Distributed under the Boost Software License, Version 1.0.
4 * http://www.boost.org/LICENSE_1_0.txt
5 * https://github.com/dlang/dmd/blob/master/src/dmd/root/array.h
18 template <typename TYPE
>
28 #define SMALLARRAYCAP 1
29 TYPE smallarray
[SMALLARRAYCAP
]; // inline storage for small arrays
34 data
= SMALLARRAYCAP
? &smallarray
[0] : NULL
;
36 allocdim
= SMALLARRAYCAP
;
41 if (data
!= &smallarray
[0])
47 const char **buf
= (const char **)mem
.xmalloc(dim
* sizeof(const char *));
49 for (d_size_t u
= 0; u
< dim
; u
++)
51 buf
[u
] = ((RootObject
*)data
[u
])->toChars();
52 len
+= strlen(buf
[u
]) + 1;
54 char *str
= (char *)mem
.xmalloc(len
);
58 for (d_size_t u
= 0; u
< dim
; u
++)
72 void reserve(d_size_t nentries
)
74 //printf("Array::reserve: dim = %d, allocdim = %d, nentries = %d\n", (int)dim, (int)allocdim, (int)nentries);
75 if (allocdim
- dim
< nentries
)
78 { // Not properly initialized, someone memset it to zero
79 if (nentries
<= SMALLARRAYCAP
)
80 { allocdim
= SMALLARRAYCAP
;
81 data
= SMALLARRAYCAP
? &smallarray
[0] : NULL
;
84 { allocdim
= nentries
;
85 data
= (TYPE
*)mem
.xmalloc(allocdim
* sizeof(*data
));
88 else if (allocdim
== SMALLARRAYCAP
)
90 allocdim
= dim
+ nentries
;
91 data
= (TYPE
*)mem
.xmalloc(allocdim
* sizeof(*data
));
92 memcpy(data
, &smallarray
[0], dim
* sizeof(*data
));
96 /* Increase size by 1.5x to avoid excessive memory fragmentation
98 d_size_t increment
= dim
/ 2;
99 if (nentries
> increment
) // if 1.5 is not enough
100 increment
= nentries
;
101 allocdim
= dim
+ increment
;
102 data
= (TYPE
*)mem
.xrealloc(data
, allocdim
* sizeof(*data
));
107 void setDim(d_size_t newdim
)
111 reserve(newdim
- dim
);
124 memmove(data
+ 1, data
, dim
* sizeof(*data
));
129 void remove(d_size_t i
)
132 memmove(data
+ i
, data
+ i
+ 1, (dim
- i
- 1) * sizeof(data
[0]));
138 memset(data
,0,dim
* sizeof(data
[0]));
149 Array_sort_compare(const void *x
, const void *y
)
151 RootObject
*ox
= *(RootObject
**)const_cast<void *>(x
);
152 RootObject
*oy
= *(RootObject
**)const_cast<void *>(y
);
154 return ox
->compare(oy
);
160 qsort(data
, dim
, sizeof(RootObject
*), &ArraySort::Array_sort_compare
);
169 TYPE
& operator[] (d_size_t index
)
174 void insert(d_size_t index
, TYPE v
)
177 memmove(data
+ index
+ 1, data
+ index
, (dim
- index
) * sizeof(*data
));
182 void insert(d_size_t index
, Array
*a
)
189 memmove(data
+ index
+ d
, data
+ index
, (dim
- index
) * sizeof(*data
));
190 memcpy(data
+ index
, a
->data
, d
* sizeof(*data
));
195 void append(Array
*a
)
208 Array
*a
= new Array();
210 memcpy(a
->data
, data
, dim
* sizeof(*data
));
231 BitArray(const BitArray
&);