pthreads/{array1 → array_reentrant}/array.c RENAMED
@@ -1,95 +1,127 @@
1
  #include <assert.h>
2
  #include <stdlib.h>
3
 
4
  #include "array.h"
5
 
6
- static array_t array_elements = NULL;
7
- static size_t array_capacity = 0;
8
- static size_t array_count = 0;
9
 
10
- array_t array_create(size_t capacity)
 
 
 
 
 
 
 
 
11
  {
12
  assert(capacity);
13
- array_capacity = capacity;
14
- array_count = 0;
15
- return array_elements = (void**)malloc( capacity * sizeof(void*) );
 
 
 
 
 
 
 
 
 
 
16
  }
17
 
18
- void array_destroy(array_t array)
19
  {
 
 
 
20
  free(array);
21
  }
22
 
23
- int array_increase_capacity(array_t array)
24
  {
25
- size_t new_capacity = 10 * array_capacity;
26
- array_t new_elements = (void**)realloc( array, new_capacity * sizeof(void*) );
 
 
27
  if ( new_elements == NULL )
28
  return -1;
29
 
30
- array_capacity = new_capacity;
31
- array_elements = new_elements;
32
 
33
  return 0; // Success
34
  }
35
 
36
- int array_decrease_capacity(array_t array)
37
  {
38
- size_t new_capacity = array_capacity / 10;
 
 
39
  if ( new_capacity < 10 )
40
  return 0;
41
 
42
- array_t new_elements = (void**)realloc( array, new_capacity * sizeof(void*) );
43
  if ( new_elements == NULL )
44
  return -1;
45
 
46
- array_capacity = new_capacity;
47
- array_elements = new_elements;
48
 
49
  return 0; // Success
50
  }
51
 
52
- size_t array_get_count(const array_t array)
53
  {
54
- (void)array;
55
- return array_count;
 
56
  }
57
 
58
- void* array_get_element(array_t array, size_t index)
59
  {
 
60
  assert( index < array_get_count(array) );
61
- return array[index];
 
62
  }
63
 
64
- int array_append(array_t array, void* element)
65
  {
66
- if ( array_count == array_capacity )
67
- if ( ! array_increase_capacity(array) )
 
 
68
  return -1;
69
 
70
- array[array_count++] = element;
 
71
  return 0; // Success
72
  }
73
 
74
- size_t array_find_first(const array_t array, const void* element, size_t start_pos)
75
  {
76
- for ( size_t index = start_pos; index < array_count; ++index )
77
- if ( array[index] == element )
 
 
78
  return index;
79
 
80
  return array_not_found;
81
  }
82
 
83
- int array_remove_first(array_t array, const void* element, size_t start_pos)
84
  {
 
 
85
  size_t index = array_find_first(array, element, start_pos);
86
  if ( index == array_not_found )
87
  return -1;
88
 
89
- for ( --array_count; index < array_count; ++index )
90
- array_elements[index] = array_elements[index + 1];
91
- if ( array_count == array_capacity / 10 )
92
  array_decrease_capacity(array);
93
 
94
  return 0; // Removed
95
  }
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(const array_t* array)
75
  {
76
+ assert(array);
77
+
78
+ return array->count;
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
+ return array->elements[index];
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(const array_t* array, const void* element, size_t start_pos)
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
  size_t index = array_find_first(array, element, start_pos);
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
  }