@@ -1,127 +1,156 @@
|
|
1 |
#include <assert.h>
|
2 |
#include <stdlib.h>
|
|
|
3 |
|
4 |
#include "array.h"
|
5 |
|
6 |
|
7 |
typedef struct array
|
8 |
{
|
9 |
void** elements;
|
10 |
size_t capacity;
|
11 |
size_t count;
|
|
|
12 |
} array_t;
|
13 |
|
|
|
|
|
|
|
14 |
|
15 |
array_t* array_create(size_t capacity)
|
16 |
{
|
17 |
assert(capacity);
|
18 |
|
19 |
array_t* array = calloc(1, sizeof(array_t));
|
20 |
if ( array == NULL )
|
21 |
return NULL;
|
22 |
|
23 |
array->capacity = capacity;
|
24 |
array->count = 0;
|
25 |
|
|
|
|
|
26 |
array->elements = (void**)malloc( capacity * sizeof(void*) );
|
27 |
if ( array->elements == NULL )
|
28 |
-
return free(array), NULL;
|
29 |
|
30 |
return array;
|
31 |
}
|
32 |
|
33 |
void array_destroy(array_t* array)
|
34 |
{
|
35 |
assert(array);
|
36 |
|
|
|
|
|
37 |
free(array->elements);
|
38 |
free(array);
|
39 |
}
|
40 |
|
41 |
int array_increase_capacity(array_t* array)
|
42 |
{
|
43 |
assert(array);
|
44 |
|
45 |
size_t new_capacity = 10 * array->capacity;
|
46 |
void** new_elements = (void**)realloc( array->elements, new_capacity * sizeof(void*) );
|
47 |
if ( new_elements == NULL )
|
48 |
return -1;
|
49 |
|
50 |
array->capacity = new_capacity;
|
51 |
array->elements = new_elements;
|
52 |
|
53 |
return 0; // Success
|
54 |
}
|
55 |
|
56 |
int array_decrease_capacity(array_t* array)
|
57 |
{
|
58 |
assert(array);
|
59 |
|
60 |
size_t new_capacity = array->capacity / 10;
|
61 |
if ( new_capacity < 10 )
|
62 |
return 0;
|
63 |
|
64 |
void** new_elements = (void**)realloc( array->elements, new_capacity * sizeof(void*) );
|
65 |
if ( new_elements == NULL )
|
66 |
return -1;
|
67 |
|
68 |
array->capacity = new_capacity;
|
69 |
array->elements = new_elements;
|
70 |
|
71 |
return 0; // Success
|
72 |
}
|
73 |
|
74 |
-
size_t array_get_count(
|
75 |
{
|
76 |
assert(array);
|
77 |
|
78 |
-
|
|
|
|
|
|
|
79 |
}
|
80 |
|
81 |
void* array_get_element(array_t* array, size_t index)
|
82 |
{
|
83 |
assert(array);
|
84 |
assert( index < array_get_count(array) );
|
85 |
|
86 |
-
|
|
|
|
|
|
|
87 |
}
|
88 |
|
89 |
int array_append(array_t* array, void* element)
|
90 |
{
|
91 |
assert(array);
|
92 |
|
|
|
93 |
if ( array->count == array->capacity )
|
94 |
if ( array_increase_capacity(array) )
|
95 |
-
return -1;
|
96 |
|
97 |
assert( array->count < array->capacity );
|
98 |
array->elements[array->count++] = element;
|
|
|
99 |
return 0; // Success
|
100 |
}
|
101 |
|
102 |
-
size_t array_find_first(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
{
|
104 |
assert( array );
|
105 |
|
106 |
for ( size_t index = start_pos; index < array->count; ++index )
|
107 |
if ( array->elements[index] == element )
|
108 |
return index;
|
109 |
|
110 |
return array_not_found;
|
111 |
}
|
112 |
|
113 |
int array_remove_first(array_t* array, const void* element, size_t start_pos)
|
114 |
{
|
115 |
assert( array );
|
116 |
|
117 |
-
|
|
|
118 |
if ( index == array_not_found )
|
119 |
-
return -1;
|
120 |
|
121 |
for ( --array->count; index < array->count; ++index )
|
122 |
array->elements[index] = array->elements[index + 1];
|
123 |
if ( array->count == array->capacity / 10 )
|
124 |
array_decrease_capacity(array);
|
125 |
|
|
|
126 |
return 0; // Removed
|
127 |
}
|
1 |
#include <assert.h>
|
2 |
#include <stdlib.h>
|
3 |
+
#include <pthread.h>
|
4 |
|
5 |
#include "array.h"
|
6 |
|
7 |
|
8 |
typedef struct array
|
9 |
{
|
10 |
void** elements;
|
11 |
size_t capacity;
|
12 |
size_t count;
|
13 |
+
pthread_mutex_t mutex;
|
14 |
} array_t;
|
15 |
|
16 |
+
// Private declarations
|
17 |
+
size_t array_find_first_private(array_t* array, const void* element, size_t start_pos);
|
18 |
+
|
19 |
|
20 |
array_t* array_create(size_t capacity)
|
21 |
{
|
22 |
assert(capacity);
|
23 |
|
24 |
array_t* array = calloc(1, sizeof(array_t));
|
25 |
if ( array == NULL )
|
26 |
return NULL;
|
27 |
|
28 |
array->capacity = capacity;
|
29 |
array->count = 0;
|
30 |
|
31 |
+
pthread_mutex_init( &array->mutex, NULL );
|
32 |
+
|
33 |
array->elements = (void**)malloc( capacity * sizeof(void*) );
|
34 |
if ( array->elements == NULL )
|
35 |
+
return (void)free(array), NULL;
|
36 |
|
37 |
return array;
|
38 |
}
|
39 |
|
40 |
void array_destroy(array_t* array)
|
41 |
{
|
42 |
assert(array);
|
43 |
|
44 |
+
pthread_mutex_destroy(&array->mutex);
|
45 |
+
|
46 |
free(array->elements);
|
47 |
free(array);
|
48 |
}
|
49 |
|
50 |
int array_increase_capacity(array_t* array)
|
51 |
{
|
52 |
assert(array);
|
53 |
|
54 |
size_t new_capacity = 10 * array->capacity;
|
55 |
void** new_elements = (void**)realloc( array->elements, new_capacity * sizeof(void*) );
|
56 |
if ( new_elements == NULL )
|
57 |
return -1;
|
58 |
|
59 |
array->capacity = new_capacity;
|
60 |
array->elements = new_elements;
|
61 |
|
62 |
return 0; // Success
|
63 |
}
|
64 |
|
65 |
int array_decrease_capacity(array_t* array)
|
66 |
{
|
67 |
assert(array);
|
68 |
|
69 |
size_t new_capacity = array->capacity / 10;
|
70 |
if ( new_capacity < 10 )
|
71 |
return 0;
|
72 |
|
73 |
void** new_elements = (void**)realloc( array->elements, new_capacity * sizeof(void*) );
|
74 |
if ( new_elements == NULL )
|
75 |
return -1;
|
76 |
|
77 |
array->capacity = new_capacity;
|
78 |
array->elements = new_elements;
|
79 |
|
80 |
return 0; // Success
|
81 |
}
|
82 |
|
83 |
+
size_t array_get_count(array_t* array)
|
84 |
{
|
85 |
assert(array);
|
86 |
|
87 |
+
pthread_mutex_lock( &array->mutex );
|
88 |
+
size_t result = array->count;
|
89 |
+
pthread_mutex_unlock( &array->mutex );
|
90 |
+
return result;
|
91 |
}
|
92 |
|
93 |
void* array_get_element(array_t* array, size_t index)
|
94 |
{
|
95 |
assert(array);
|
96 |
assert( index < array_get_count(array) );
|
97 |
|
98 |
+
pthread_mutex_lock( &array->mutex );
|
99 |
+
void* result = array->elements[index];
|
100 |
+
pthread_mutex_unlock( &array->mutex );
|
101 |
+
return result;
|
102 |
}
|
103 |
|
104 |
int array_append(array_t* array, void* element)
|
105 |
{
|
106 |
assert(array);
|
107 |
|
108 |
+
pthread_mutex_lock( &array->mutex );
|
109 |
if ( array->count == array->capacity )
|
110 |
if ( array_increase_capacity(array) )
|
111 |
+
return (void)pthread_mutex_unlock( &array->mutex ), -1;
|
112 |
|
113 |
assert( array->count < array->capacity );
|
114 |
array->elements[array->count++] = element;
|
115 |
+
pthread_mutex_unlock( &array->mutex );
|
116 |
return 0; // Success
|
117 |
}
|
118 |
|
119 |
+
size_t array_find_first(array_t* array, const void* element, size_t start_pos)
|
120 |
+
{
|
121 |
+
assert( array );
|
122 |
+
|
123 |
+
pthread_mutex_lock( &array->mutex );
|
124 |
+
size_t result = array_find_first_private(array, element, start_pos);
|
125 |
+
pthread_mutex_unlock( &array->mutex );
|
126 |
+
return result;
|
127 |
+
}
|
128 |
+
|
129 |
+
size_t array_find_first_private(array_t* array, const void* element, size_t start_pos)
|
130 |
{
|
131 |
assert( array );
|
132 |
|
133 |
for ( size_t index = start_pos; index < array->count; ++index )
|
134 |
if ( array->elements[index] == element )
|
135 |
return index;
|
136 |
|
137 |
return array_not_found;
|
138 |
}
|
139 |
|
140 |
int array_remove_first(array_t* array, const void* element, size_t start_pos)
|
141 |
{
|
142 |
assert( array );
|
143 |
|
144 |
+
pthread_mutex_lock( &array->mutex );
|
145 |
+
size_t index = array_find_first_private(array, element, start_pos);
|
146 |
if ( index == array_not_found )
|
147 |
+
return (void)pthread_mutex_unlock( &array->mutex ), -1;
|
148 |
|
149 |
for ( --array->count; index < array->count; ++index )
|
150 |
array->elements[index] = array->elements[index + 1];
|
151 |
if ( array->count == array->capacity / 10 )
|
152 |
array_decrease_capacity(array);
|
153 |
|
154 |
+
pthread_mutex_unlock( &array->mutex );
|
155 |
return 0; // Removed
|
156 |
}
|