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