Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7c08cc716bed178b844a0c8f7bf67f3a1fe566ed (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2017 protos software gmbh (http://www.protos.de).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * CONTRIBUTORS:
 * 		Jan Belle (initial contribution)
 *
 *******************************************************************************/

#include "etStaticDeque.h"

#include <string.h>

void etStaticDeque_construct(etStaticDeque* const self, void* memory, int memorySize, int objectSize) {
	self->size = 0;
	self->first = 0;
	self->memory = memory;
	self->maxSize = memorySize / objectSize;
	self->objectSize = objectSize;

	etStaticDeque_clear(self);
}

void etStaticDeque_clear(etStaticDeque* const self) {
	self->first = 0;
	self->size = 0;
}

void* etStaticDeque_get(const etStaticDeque* const self, int position) {
	/* TODO JB: Handle position out of bounds exception */
	return (void*) &self->memory[(self->first + position * self->objectSize) % (self->objectSize * self->maxSize)];
}

void etStaticDeque_push_front(etStaticDeque* const self, void* object) {
	/* TODO JB: Handle out of memory exception */
	if(self->size < self->maxSize) {
		++self->size;
		self->first = (self->first + (self->maxSize - 1) * self->objectSize) % (self->objectSize * self->maxSize);
		memcpy(etStaticDeque_front(self), object, self->objectSize);
	}
}

void etStaticDeque_push_back(etStaticDeque* const self, void* object) {
	/* TODO JB: Handle out of memory exception */
	if(self->size < self->maxSize) {
		++self->size;
		memcpy(etStaticDeque_back(self), object, self->objectSize);
	}
}

void etStaticDeque_pop_front(etStaticDeque* const self) {
	/* TODO JB: Handle deque empty exception */
	if(self->size > 0) {
		self->first = (self->first + self->objectSize) % (self->objectSize * self->maxSize);
		--self->size;
	}
}

void etStaticDeque_pop_back(etStaticDeque* const self) {
	/* TODO JB: Handle deque empty exception */
	if(self->size > 0) {
		--self->size;
	}
}

void* etStaticDeque_front(const etStaticDeque* const self) {
	return etStaticDeque_get(self, 0);
}

void* etStaticDeque_back(const etStaticDeque* const self) {
	return etStaticDeque_get(self, self->size - 1);
}

Back to the top