-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroom.c
70 lines (53 loc) · 1.8 KB
/
room.c
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
#include "room.h"
#include <stdbool.h>
#include <string.h>
inline void add_with_duplicate(struct RoomBuffer *buf, Room *room) {
buf->rooms[buf->length++] = room;
}
// Need to pass a pointer so that we can update the length of the buffer
void add_no_duplicate(struct RoomBuffer *buff, Room *room) {
if (contains_room(*buff, room) < 0) add_with_duplicate(buff, room);
}
void concat_no_duplicate(struct RoomBuffer *dst, const struct RoomBuffer src) {
for (int i = 0; i < src.length; i++) {
add_no_duplicate(dst, src.rooms[i]);
}
}
void remove_if_present(struct RoomBuffer *buf, const Room *room) {
int i = contains_room(*buf, room);
while (i >= 0) {
memmove(buf->rooms + i, buf->rooms + i + 1,
sizeof(Room*) * (buf->length - i - 1));
buf->length--;
i = contains_room(*buf, room);
}
}
int contains_room(const struct RoomBuffer buff, const Room *room) {
for (int i = 0; i < buff.length; i++) {
if (buff.rooms[i] == room) return i;
}
return -1;
}
void room_buffer_copy(struct RoomBuffer *dst, const struct RoomBuffer src) {
memcpy(dst->rooms, src.rooms, src.length * sizeof(Room*));
dst->length = src.length;
}
void remove_duplicate_rooms(struct RoomBuffer *buf) {
// This algo is slightly cursed. It exploits buf and copy having the same
// underlying memory.
struct RoomBuffer copy;
for (int i = 0; i < buf->length - 1; i++) {
copy.rooms = buf->rooms + i + 1;
copy.length = buf->length - i - 1;;
remove_if_present(©, buf->rooms[i]);
buf->length = copy.length + i + 1;
}
}
struct RoomBuffer room_buffer_from(
const struct RoomBuffer buff,
Room **arr) {
struct RoomBuffer res;
res.rooms = arr;
room_buffer_copy(&res, buff);
return res;
}