Repository navigation
Expand file tree
/
Copy pathdarray.c
More file actions
92 lines (85 loc) · 2.16 KB
/
Copy pathdarray.c
File metadata and controls
92 lines (85 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
*
* Copyright 2016-2019 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <stdlib.h>
#include "darray.h"
#include "common/blur_array.h"
void init_array(DArray *a, size_t init_size)
{
a->array = (double *)malloc(init_size * sizeof(double));
memset(a->array, 0.0, init_size * sizeof(double));
a->used = 0;
a->size = init_size;
#ifdef MULTI_THREADING
pthread_mutex_init(&a->mutex, NULL);
#endif
}
void insert_array(DArray *a, double e)
{
#ifdef MULTI_THREADING
pthread_mutex_lock(&a->mutex);
#endif
if (a->used == a->size)
{
a->size *= 2;
double *temp;
temp = a->array;
temp += (a->size / 2);
memset(temp, 0.0, (a->size / 2) * sizeof(double));
a->array = (double *)realloc(a->array, a->size * sizeof(double));
}
a->array[a->used++] = e;
#ifdef MULTI_THREADING
pthread_mutex_unlock(&a->mutex);
#endif
}
void insert_array_at(DArray *a, double e, int pos)
{
#ifdef MULTI_THREADING
pthread_mutex_lock(&a->mutex);
#endif
if ((pos+1) > a->used)
{
a->used = pos+1;
}
while (a->used > a->size)
{
a->size *= 2;
a->array = (double *)realloc(a->array, a->size * sizeof(double));
double *temp;
temp = a->array;
temp += (a->size / 2);
memset(temp, 0.0, (a->size / 2) * sizeof(double));
}
a->array[pos] = e;
#ifdef MULTI_THREADING
pthread_mutex_unlock(&a->mutex);
#endif
}
double get_at(DArray *a, int pos)
{
return a->array[pos];
}
void free_array(DArray *a)
{
free(a->array);
a->array = NULL;
a->used = a->size = 0;
#ifdef MULTI_THREADING
pthread_mutex_destroy(&a->mutex);
#endif
}