-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.h
85 lines (80 loc) · 1.94 KB
/
list.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
// Created by Amin Rezaei on 12/26/18.
//
#ifndef KVSTORE_LIST_H
#define KVSTORE_LIST_H
#include "dynamic.h"
#include "syntax.h"
#include <stdlib.h>
typedef struct List_t{
Dynamic* data;
int size;
} List;
List list_init();
void list_append(List*,Dynamic*);
void list_appendv(List*,Dynamic);
int list_remove(List*,Dynamic*);
int list_remove_index(List*,int);
int list_removev(List*,Dynamic);
int list_count(List*,Dynamic*);
int list_countv(List*,Dynamic);
Dynamic* list_get(List*,int);
List list_init(){
List l = {};
return l;
}
void list_append(List* list,Dynamic* d){
if(list->size == 0){
list->data = (Dynamic*) malloc(sizeof(Dynamic));
list->size = 1;
list->data[0] = *d;
return;
}
list->size++;
list->data = (Dynamic*) realloc(list->data,list->size * sizeof(Dynamic));
list->data[list->size-1] = *d;
}
void list_appendv(List* list,Dynamic d){
list_append(list,&d);
}
int list_remove_index(List* list,int idx){
range(j,idx,list->size-1){
list->data[$j] = list->data[$j+1];
}
list->size -= 1;
list->data = realloc(list->data,list->size * sizeof(Dynamic));
return 1;
}
int list_remove(List* list,Dynamic* d){
int x = 0;
range(i,0,list->size){
if(dynamic_compare(&list->data[$i],d)==0){
range(j,$i,list->size-1){
list->data[$j] = list->data[$j+1];
}
x++;
}
}
list->size -= x;
list->data = realloc(list->data,list->size * sizeof(Dynamic));
return x;
}
int list_removev(List* list,Dynamic d){
return list_remove(list,&d);
}
int list_count(List* list,Dynamic* d) {
int x = 0;
range(i, 0, list->size) {
if (dynamic_compare(&list->data[$i], d) == 0) {
x++;
}
}
return x;
}
int list_countv(List* list,Dynamic d) {
return list_count(list,&d);
}
Dynamic* list_get(List* list,int index) {
return &list->data[index];
}
#endif //KVSTORE_LIST_H