smntc
an in-memory multimodal graph database
Loading...
Searching...
No Matches
Inventory.c
Go to the documentation of this file.
1#include <stdlib.h>
2#include <limits.h>
3#include "errors.h"
4
5struct Inventory {
6 unsigned int *ints;
7 unsigned int next;
8 unsigned int length;
9};
10
11struct Inventory *Inventory_construct(unsigned int length, int *error)
12{
13 struct Inventory *this = malloc(sizeof(struct Inventory));
14
15 if (0 == this) {
16 *error = ERROR_NO_MEMORY;
17 return 0;
18 }
19
20 this->length = length;
21
22 this->ints = malloc((unsigned long)sizeof(unsigned int) * this->length);
23
24 if (0 == this->ints) {
25 *error = ERROR_NO_MEMORY;
26 return 0;
27 }
28
29 this->next = 0;
30
31 return this;
32}
33
35{
36 if (0 != this) {
37 free(this->ints);
38 free(this);
39 }
40
41 return 0;
42}
43
44unsigned int Inventory_getNext(const struct Inventory *this)
45{
46 return this->next;
47}
48
49void Inventory_append(struct Inventory *this, unsigned int content, int *error)
50{
51 if (content < this->length && (content <= this->next || this->next == 0)) {
52
53 if (this->next == UINT_MAX) {
54 *error = ERROR_NO_SPACE;
55 return;
56 }
57
58 this->ints[this->next] = content;
59 this->next++;
60 return;
61 }
62
64}
65
66void Inventory_appendUnchecked(struct Inventory *this, unsigned int content, int *error)
67{
68 if (this->next == this->length) {
69 *error = ERROR_NO_SPACE;
70 return;
71 }
72
73 this->ints[this->next] = content;
74 this->next++;
75}
76
77void Inventory_update(struct Inventory *this, unsigned int path, unsigned int content,int *error)
78{
79 if (path >= this->length || path >= this->next) {
81 return;
82 }
83
84 if (content >= this->length || content >= this->next) {
86 return;
87 }
88
89 this->ints[path] = content;
90}
91
92unsigned int Inventory_read(const struct Inventory *this, unsigned int path, int *error)
93{
94 if (path >= this->length || path >= this->next) {
96 return 0;
97 }
98
99 return this->ints[path];
100}
101
unsigned int Inventory_read(const struct Inventory *this, unsigned int path, int *error)
Definition Inventory.c:92
void Inventory_update(struct Inventory *this, unsigned int path, unsigned int content, int *error)
Definition Inventory.c:77
struct Inventory * Inventory_construct(unsigned int length, int *error)
Definition Inventory.c:11
unsigned int Inventory_getNext(const struct Inventory *this)
Definition Inventory.c:44
struct Inventory * Inventory_destruct(struct Inventory *this)
Definition Inventory.c:34
void Inventory_append(struct Inventory *this, unsigned int content, int *error)
Definition Inventory.c:49
void Inventory_appendUnchecked(struct Inventory *this, unsigned int content, int *error)
Definition Inventory.c:66
#define ERROR_NO_MEMORY
Definition errors.h:5
#define ERROR_INVENTORY_CONTENT_OUT_OF_BOUNDS
Definition errors.h:8
#define ERROR_INVENTORY_PATH_OUT_OF_BOUNDS
Definition errors.h:7
#define ERROR_NO_SPACE
Definition errors.h:6
unsigned int * ints
Definition Inventory.c:6
unsigned int next
Definition Inventory.c:7
unsigned int length
Definition Inventory.c:8