arraylist.c
Go to the documentation of this file.
1 /*
2  * $Id: arraylist.c,v 1.4 2006/01/26 02:16:28 mclark Exp $
3  *
4  * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
5  * Michael Clark <michael@metaparadigm.com>
6  *
7  * This library is free software; you can redistribute it and/or modify
8  * it under the terms of the MIT license. See COPYING for details.
9  *
10  */
11 
12 #include "config.h"
13 
14 #if STDC_HEADERS
15 # include <stdlib.h>
16 # include <string.h>
17 #endif /* STDC_HEADERS */
18 
19 #if HAVE_STRINGS_H
20 # include <strings.h>
21 #endif /* HAVE_STRINGS_H */
22 
23 #include "bits.h"
24 #include "arraylist.h"
25 
26 struct array_list*
28 {
29  struct array_list *this;
30 
31  if(!(this = calloc(1, sizeof(struct array_list)))) return NULL;
33  this->length = 0;
34  this->free_fn = free_fn;
35  if(!(this->array = calloc(sizeof(void*), (size_t) this->size))) {
36  free(this);
37  return NULL;
38  }
39  return this;
40 }
41 
42 extern void
44 {
45  int i;
46  for(i = 0; i < this->length; i++)
47  if(this->array[i]) this->free_fn(this->array[i]);
48  free(this->array);
49  free(this);
50 }
51 
52 void*
53 array_list_get_idx(struct array_list *this, int i)
54 {
55  if(i >= this->length) return NULL;
56  return this->array[i];
57 }
58 
59 static int array_list_expand_internal(struct array_list *this, int max)
60 {
61  void *t;
62  int new_size;
63 
64  if(max < this->size) return 0;
65  new_size = max(this->size << 1, max);
66  if(!(t = realloc(this->array, new_size*sizeof(void*)))) return -1;
67  this->array = t;
68  (void)memset(this->array + this->size, 0, (new_size-this->size)*sizeof(void*));
69  this->size = new_size;
70  return 0;
71 }
72 
73 int
74 array_list_put_idx(struct array_list *this, int idx, void *data)
75 {
76  if(array_list_expand_internal(this, idx)) return -1;
77  if(this->array[idx]) this->free_fn(this->array[idx]);
78  this->array[idx] = data;
79  if(this->length <= idx) this->length = idx + 1;
80  return 0;
81 }
82 
83 int
84 array_list_add(struct array_list *this, void *data)
85 {
86  return array_list_put_idx(this, this->length, data);
87 }
88 
89 int
91 {
92  return this->length;
93 }
void( array_list_free_fn)(void *data)
Definition: arraylist.h:17
static int array_list_expand_internal(struct array_list *this, int max)
Definition: arraylist.c:59
struct array_list * array_list_new(array_list_free_fn *free_fn)
Definition: arraylist.c:27
#define max(a, b)
Definition: bits.h:20
int array_list_put_idx(struct array_list *this, int idx, void *data)
Definition: arraylist.c:74
int size
Definition: arraylist.h:23
int array_list_add(struct array_list *this, void *data)
Definition: arraylist.c:84
array_list_free_fn * free_fn
Definition: arraylist.h:24
void array_list_free(struct array_list *this)
Definition: arraylist.c:43
void ** array
Definition: arraylist.h:21
void * array_list_get_idx(struct array_list *this, int i)
Definition: arraylist.c:53
int array_list_length(struct array_list *this)
Definition: arraylist.c:90
int length
Definition: arraylist.h:22
#define ARRAY_LIST_DEFAULT_SIZE
Definition: arraylist.h:15


csm
Author(s): Andrea Censi
autogenerated on Tue May 11 2021 02:18:23