Skip to content

Commit 0cf2d4c

Browse files
committed
update clang
1 parent 1c44eb9 commit 0cf2d4c

File tree

3 files changed

+69
-11
lines changed

3 files changed

+69
-11
lines changed

c/arraylist/arraylist.c

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,59 @@
11
#include <stdlib.h>
22
#include <string.h>
33
#include <assert.h>
4-
54
#include "arraylist.h"
65

7-
const int DEFAULT_CAPACITY = 10;
6+
#define ARRAYLIST_DEAUFALT_SIZE 4
87

98
/**
109
* create a new empty arraylist
1110
*/
1211
ArrayList *listCreate() {
1312
ArrayList *list = malloc(sizeof(ArrayList));
13+
14+
list->dataStore = malloc(sizeof(void *) * ARRAYLIST_DEAUFALT_SIZE);
15+
assert(list->dataStore);
1416
list->theSize = 0;
15-
list->dataStore = malloc(sizeof(void *) * DEFAULT_CAPACITY);
17+
list->capacity = ARRAYLIST_DEAUFALT_SIZE;
1618

1719
list->size = listSize;
20+
list->get = listGet;
21+
list->set = listSet;
22+
list->append = listAppend;
1823

1924
return list;
2025
}
2126

27+
void listIsRealloc(ArrayList *list) {
28+
if (list->theSize == list->capacity) {
29+
unsigned int newCapa = list->capacity * 2;
30+
list->capacity = newCapa;
31+
list->dataStore = realloc(list->dataStore, sizeof(void *) * newCapa);
32+
assert(list->dataStore);
33+
}
34+
}
35+
2236
int listSize(ArrayList *list) {
2337
return list->theSize;
2438
}
2539

40+
void *listGet(ArrayList *list, unsigned int index) {
41+
assert(index < list->theSize);
42+
return list->dataStore[index];
43+
}
44+
45+
void listSet(ArrayList *list, unsigned int index, void *value) {
46+
assert(index < list->theSize);
47+
list->dataStore[index] = value;
48+
}
49+
50+
void listAppend(ArrayList *list, void *value) {
51+
listIsRealloc(list);
52+
list->dataStore[list->theSize++] = value;
53+
}
54+
55+
56+
57+
2658

2759

c/arraylist/arraylist.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
#define __ARRAYLIST_H__
33

44
typedef struct ArrayList {
5-
unsigned int theSize;
6-
void *dataStore;
5+
int theSize;
6+
int capacity;
7+
void **dataStore;
78

8-
int (*size)();
9-
//void *(*get)(ArrayList *, unsigned int);
10-
//void (*set)(ArrayList *, unsigned int, void *);
11-
//void (*append)(ArrayList *, void *);
9+
int (*size)(struct ArrayList *);
10+
void *(*get)(struct ArrayList *, unsigned int);
11+
void (*set)(struct ArrayList *, unsigned int, void *);
12+
void (*append)(struct ArrayList *, void *);
1213
//void (*insertAfter)(ArrayList *, unsigned int, void *);
1314
//void (*remove)(ArrayList *, unsigned int);
1415
//void (*clear)(ArrayList *);

c/arraylist/arraylist_test.c

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,33 @@
1+
#include <assert.h>
12
#include <stdio.h>
2-
33
#include "arraylist.c"
44

55
int main() {
66
ArrayList *list = listCreate();
7-
printf("list size: %d\n", list->size(list));
7+
assert(list->size(list) == 0);
8+
9+
int a = 1;
10+
list->append(list, &a);
11+
assert(list->size(list) == 1);
12+
13+
int item1 = *(int *)list->get(list, 0);
14+
assert(item1 == 1);
15+
16+
char c = 'c';
17+
list->append(list, &c);
18+
assert(list->size(list) == 2);
19+
20+
char item2 = *(char *)list->get(list, 1);
21+
assert(item2 == 'c');
22+
23+
int b = 2;
24+
list->set(list, 1, &b);
25+
assert(list->size(list) == 2);
26+
27+
int item2_new = *(int *)list->get(list, 1);
28+
assert(item2_new == 2);
29+
30+
printf("\n ArrayList test pass...\n\n");
31+
return 0;
832
}
33+

0 commit comments

Comments
 (0)