Skip to content

Commit 73faca6

Browse files
authored
Feature/erase item from queue (#3)
Support erasing items in the queue
1 parent 151b817 commit 73faca6

3 files changed

Lines changed: 979 additions & 1 deletion

File tree

src/static_queue.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,78 @@ int32_t staticQueueClear(staticQueue_t* queue)
123123
queue->tail = queue->first_item;
124124
return STATIC_QUEUE_SUCCESS;
125125
}
126+
127+
int32_t staticQueueErase(staticQueue_t* queue, staticQueueItem_t* item)
128+
{
129+
// Check if the item is active
130+
if (!item->active) {
131+
return STATIC_QUEUE_EMPTY;
132+
}
133+
134+
// Mark the item as inactive
135+
item->active = false;
136+
137+
// Special case: if this was the only item in the queue
138+
if (queue->tail == queue->head->last) {
139+
// Queue is now empty, reset pointers
140+
queue->head = queue->first_item;
141+
queue->tail = queue->first_item;
142+
return STATIC_QUEUE_SUCCESS;
143+
}
144+
145+
// If erasing the tail item (oldest item), just move tail to next
146+
if (item == queue->tail) {
147+
queue->tail = queue->tail->next;
148+
149+
// Skip over any remaining inactive items at tail
150+
while (queue->tail != queue->head && !queue->tail->active) {
151+
queue->tail = queue->tail->next;
152+
}
153+
154+
// Check if tail caught up to head with exactly one active item
155+
if (queue->tail == queue->head && queue->tail->active) {
156+
// Move head forward to maintain tail != head invariant for single item
157+
queue->head = queue->head->next;
158+
}
159+
160+
return STATIC_QUEUE_SUCCESS;
161+
}
162+
163+
// If erasing the item just before head (newest item), move head backward
164+
if (item->next == queue->head) {
165+
queue->head = item;
166+
167+
// Check if head caught up to tail with exactly one active item
168+
if (queue->tail == queue->head && queue->tail->active) {
169+
// Move head forward to maintain tail != head invariant for single item
170+
queue->head = queue->head->next;
171+
}
172+
173+
return STATIC_QUEUE_SUCCESS;
174+
}
175+
176+
// For items in the middle: remove from current position and relink immediately before tail
177+
178+
// Step 1: Remove item from its current position in the list
179+
staticQueueItem_t* prev_item = item->last;
180+
staticQueueItem_t* next_item = item->next;
181+
182+
prev_item->next = next_item;
183+
next_item->last = prev_item;
184+
185+
// Step 2: Reinsert the item immediately before tail
186+
staticQueueItem_t* before_tail = queue->tail->last;
187+
188+
// Insert: before_tail <-> item <-> tail
189+
before_tail->next = item;
190+
item->last = before_tail;
191+
item->next = queue->tail;
192+
queue->tail->last = item;
193+
194+
// Step 3: If queue was full, move head to point to the erased item (now inactive)
195+
if (queue->head == queue->tail && queue->head->active) {
196+
queue->head = item;
197+
}
198+
199+
return STATIC_QUEUE_SUCCESS;
200+
}

src/static_queue.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,14 @@ int32_t staticQueuePeak(staticQueue_t* queue, staticQueueItem_t** peak_item);
151151
*/
152152
int32_t staticQueueClear(staticQueue_t* queue);
153153

154+
/**
155+
* Erase a specific item from the queue
156+
* Input: Queue instance
157+
* Input: Pointer to the item to erase
158+
* Returns: queueErr_t
159+
*/
160+
int32_t staticQueueErase(staticQueue_t* queue, staticQueueItem_t* item);
161+
154162
/**
155163
* Check it the queue is full
156164
* Input: Queue instance

0 commit comments

Comments
 (0)