3D-ICE 3.0.0
powers_queue.c
1/******************************************************************************
2 * This file is part of 3D-ICE, version 3.1.0 . *
3 * *
4 * 3D-ICE is free software: you can redistribute it and/or modify it under *
5 * the terms of the GNU General Public License as published by the Free *
6 * Software Foundation, either version 3 of the License, or any later *
7 * version. *
8 * *
9 * 3D-ICE is distributed in the hope that it will be useful, but WITHOUT *
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for *
12 * more details. *
13 * *
14 * You should have received a copy of the GNU General Public License along *
15 * with 3D-ICE. If not, see <http://www.gnu.org/licenses/>. *
16 * *
17 * Copyright (C) 2021 *
18 * Embedded Systems Laboratory - Ecole Polytechnique Federale de Lausanne *
19 * All Rights Reserved. *
20 * *
21 * Authors: Arvind Sridhar Alessandro Vincenzi *
22 * Giseong Bak Martino Ruggiero *
23 * Thomas Brunschwiler Eder Zulian *
24 * Federico Terraneo Darong Huang *
25 * Luis Costero Marina Zapater *
26 * David Atienza *
27 * *
28 * For any comment, suggestion or request about 3D-ICE, please register and *
29 * write to the mailing list (see http://listes.epfl.ch/doc.cgi?liste=3d-ice) *
30 * Any usage of 3D-ICE for research, commercial or other purposes must be *
31 * properly acknowledged in the resulting products or publications. *
32 * *
33 * EPFL-STI-IEL-ESL Mail : 3d-ice@listes.epfl.ch *
34 * Batiment ELG, ELG 130 (SUBSCRIPTION IS NECESSARY) *
35 * Station 11 *
36 * 1015 Lausanne, Switzerland Url : http://esl.epfl.ch/3d-ice *
37 ******************************************************************************/
38
39#include <stdlib.h> // For the memory functions malloc/calloc/free
40
41#include "powers_queue.h"
42
43/******************************************************************************/
44
46{
47 pqueue->Capacity = (Quantity_t) 0u ;
48 pqueue->Memory = NULL ;
49 pqueue->Size = (Quantity_t) 0u ;
50 pqueue->End = (Quantity_t) 0u ;
51 pqueue->Start = (Quantity_t) 0u ;
52}
53
54/******************************************************************************/
55
57{
58 if (pqueue == NULL) return ;
59
60 powers_queue_destroy (pqueue) ;
61
62 pqueue->Memory = (Power_t *) calloc (capacity, sizeof (Power_t)) ;
63
64 if (pqueue->Memory == NULL)
65 {
66 fprintf (stderr, "Malloc power queue error\n") ;
67
68 return ;
69 }
70
71 pqueue->Capacity = capacity ;
72}
73
74/******************************************************************************/
75
77{
78 if (pqueue->Memory != NULL)
79
80 free (pqueue->Memory) ;
81
82 powers_queue_init (pqueue) ;
83}
84
85/******************************************************************************/
86
88{
89 if (dst->Capacity < src->Capacity || dst->Memory == NULL)
90 {
92 powers_queue_init (dst) ;
93 powers_queue_build (dst, src->Capacity) ;
94 }
95 else
96 {
97 dst->Start = 0u ;
98 dst->End = 0u ;
99 dst->Size = 0u ;
100 }
101
102 if (is_empty_powers_queue (src) == true)
103
104 return ;
105
106 Quantity_t tocopy = src->Size ;
107 Quantity_t index = src->Start ;
108
109 while (tocopy-- > 0)
110 {
111 put_into_powers_queue (dst, src->Memory [index]) ;
112
113 index = (index + 1) % src->Capacity ;
114 }
115}
116
117/******************************************************************************/
118
120{
121 PowersQueue_t *pqueue = (PowersQueue_t *) malloc (sizeof(PowersQueue_t)) ;
122
123 if (pqueue != NULL)
124
125 powers_queue_init (pqueue) ;
126
127 return pqueue ;
128}
129
130/******************************************************************************/
131
133{
134 if (pqueue == NULL)
135
136 return NULL ;
137
139
140 if (newpq != NULL)
141
142 powers_queue_copy (newpq, pqueue) ;
143
144 return newpq ;
145}
146
147/******************************************************************************/
148
150{
151 if (pqueue == NULL)
152
153 return ;
154
155 powers_queue_destroy (pqueue) ;
156
157 free (pqueue) ;
158}
159
160/******************************************************************************/
161
163(
164 PowersQueue_t *pqueue,
165 FILE *stream,
166 String_t prefix
167)
168{
169 fprintf (stream, "%s ", prefix) ;
170
171 if (is_empty_powers_queue (pqueue))
172
173 return ;
174
175 Quantity_t toprint = pqueue->Size ;
176 Quantity_t index = pqueue->Start ;
177
178 while (toprint-- > 1)
179 {
180 fprintf (stream, "%.3f, ", pqueue->Memory [index]) ;
181
182 index = (index + 1) % pqueue->Capacity ;
183 }
184
185 fprintf (stream, "%.3f ", pqueue->Memory [index]) ;
186}
187
188/******************************************************************************/
189
191{
192 return (pqueue->Size == 0) ;
193}
194
195/******************************************************************************/
196
198{
199 return (pqueue->Size == pqueue->Capacity) ;
200}
201
202/******************************************************************************/
203
205{
206 if (pqueue->Memory == NULL)
207 {
208 fprintf (stderr, "ERROR: put into not-built powers queue\n") ;
209
210 return ;
211 }
212
213 if (is_full_powers_queue (pqueue))
214 {
215 PowersQueue_t pq ;
216
217 powers_queue_init (&pq) ;
218
219 powers_queue_build (&pq, pqueue->Capacity * (Quantity_t) 2) ;
220
221 powers_queue_copy (&pq, pqueue) ;
222
223 Power_t *tmp = pq.Memory ;
224 pq.Memory = pqueue->Memory ;
225 pqueue->Memory = tmp ;
226
227 pqueue->Capacity = pq.Capacity ;
228 pqueue->Size = pq.Size ;
229 pqueue->Start = pq.Start ;
230 pqueue->End = pq.End ;
231
233 }
234
235 pqueue->Memory [pqueue->End] = power ;
236
237 pqueue->End = (pqueue->End + 1) % pqueue->Capacity ;
238
239 pqueue->Size++ ;
240}
241
242/******************************************************************************/
243
245{
246 if (pqueue->Memory == NULL)
247 {
248 fprintf (stderr, "ERROR: get from not-built powers queue\n") ;
249
250 return 0.0 ;
251 }
252
253 if ( is_empty_powers_queue(pqueue) )
254 {
255 fprintf (stderr, "ERROR: get from empty power queue\n") ;
256
257 return 0.0 ;
258 }
259
260 Power_t power = pqueue->Memory [pqueue->Start] ;
261
262 pqueue->Start = (pqueue->Start + 1) % pqueue->Capacity ;
263
264 pqueue->Size-- ;
265
266 return power ;
267}
268
269/******************************************************************************/
void powers_queue_copy(PowersQueue_t *dst, PowersQueue_t *src)
Definition: powers_queue.c:87
PowersQueue_t * powers_queue_calloc(void)
Definition: powers_queue.c:119
PowersQueue_t * powers_queue_clone(PowersQueue_t *pqueue)
Definition: powers_queue.c:132
bool is_full_powers_queue(PowersQueue_t *pqueue)
Definition: powers_queue.c:197
bool is_empty_powers_queue(PowersQueue_t *pqueue)
Definition: powers_queue.c:190
void powers_queue_print(PowersQueue_t *pqueue, FILE *stream, String_t prefix)
Definition: powers_queue.c:163
void powers_queue_init(PowersQueue_t *pqueue)
Definition: powers_queue.c:45
void powers_queue_build(PowersQueue_t *pqueue, Quantity_t capacity)
Definition: powers_queue.c:56
void powers_queue_free(PowersQueue_t *pqueue)
Definition: powers_queue.c:149
Power_t get_from_powers_queue(PowersQueue_t *pqueue)
Definition: powers_queue.c:244
void powers_queue_destroy(PowersQueue_t *pqueue)
Definition: powers_queue.c:76
void put_into_powers_queue(PowersQueue_t *pqueue, Power_t power)
Definition: powers_queue.c:204
char * String_t
Definition: string_t.h:55
A First In - First Out circular queue to store power values.
Definition: powers_queue.h:63
Power_t * Memory
Definition: powers_queue.h:70
Quantity_t Size
Definition: powers_queue.h:74
Quantity_t End
Definition: powers_queue.h:78
Quantity_t Start
Definition: powers_queue.h:82
Quantity_t Capacity
Definition: powers_queue.h:66
uint32_t Quantity_t
Definition: types.h:59
double Power_t
Definition: types.h:83