microui  14.5.5
microui
ui_rect_collection.h
1 /*
2  * C
3  *
4  * Copyright 2023-2026 MicroEJ Corp. All rights reserved.
5  * MicroEJ Corp. PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */
7 
8 #ifndef UI_RECT_COLLECTION_H
9 #define UI_RECT_COLLECTION_H
10 
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14 
19 // --------------------------------------------------------------------------------
20 // Includes
21 // --------------------------------------------------------------------------------
22 
23 #include <assert.h>
24 
25 #include "ui_rect.h"
26 #include "ui_configuration.h"
27 
28 // --------------------------------------------------------------------------------
29 // Typedefs
30 // --------------------------------------------------------------------------------
31 
36 typedef struct ui_rect_collection_t {
37  ui_rect_t data[UI_RECT_COLLECTION_MAX_LENGTH];
38  size_t length;
40 
41 // --------------------------------------------------------------------------------
42 // Public functions
43 // --------------------------------------------------------------------------------
44 
50 static inline void UI_RECT_COLLECTION_clear(ui_rect_collection_t *collection) {
51  collection->length = 0;
52 }
53 
59 static inline void UI_RECT_COLLECTION_init(ui_rect_collection_t *collection) {
60  UI_RECT_COLLECTION_clear(collection);
61 }
62 
70 static inline size_t UI_RECT_COLLECTION_get_length(const ui_rect_collection_t *collection) {
71  return collection->length;
72 }
73 
81 static inline bool UI_RECT_COLLECTION_is_full(const ui_rect_collection_t *collection) {
82  return UI_RECT_COLLECTION_get_length(collection) >= UI_RECT_COLLECTION_MAX_LENGTH;
83 }
84 
92 static inline bool UI_RECT_COLLECTION_is_empty(const ui_rect_collection_t *collection) {
93  return collection->length == 0u;
94 }
95 
103 static inline ui_rect_t * UI_RECT_COLLECTION_get_last(ui_rect_collection_t *collection) {
104  return UI_RECT_COLLECTION_is_empty(collection) ? NULL : (collection->data + collection->length - 1u);
105 }
106 
114 static inline ui_rect_t * UI_RECT_COLLECTION_get_end(ui_rect_collection_t *collection) {
115  return collection->data + collection->length;
116 }
117 
125 static inline void UI_RECT_COLLECTION_add_rect(ui_rect_collection_t *collection, const ui_rect_t element) {
126  assert(!UI_RECT_COLLECTION_is_full(collection));
127  collection->data[collection->length] = element;
128  collection->length++;
129 }
130 
138 static inline void UI_RECT_COLLECTION_remove_rect(ui_rect_collection_t *collection, ui_rect_t *element) {
139  assert(!UI_RECT_COLLECTION_is_empty(collection));
140  assert(element >= collection->data);
141  assert(element < (collection->data + collection->length));
142  *element = *UI_RECT_COLLECTION_get_last(collection);
143  collection->length--;
144 }
145 
151 static inline void UI_RECT_COLLECTION_remove_empty_rects(ui_rect_collection_t *collection) {
152  // cppcheck-suppress [misra-c2012-14.2] do not increment i systematically
153  for (size_t i = 0; i < collection->length;) {
154  if (UI_RECT_is_empty(&collection->data[i])) {
155  UI_RECT_COLLECTION_remove_rect(collection, &collection->data[i]);
156  } else {
157  i++;
158  }
159  }
160 }
161 
162 // --------------------------------------------------------------------------------
163 // EOF
164 // --------------------------------------------------------------------------------
165 
166 #ifdef __cplusplus
167 }
168 #endif
169 
170 #endif // UI_RECT_COLLECTION_H
Exposes the ui_rect_collection_t type that handles unordered ui_rect_t collections.
MicroEJ MicroUI library low level API: enable some features according to the hardware capabilities.
#define UI_RECT_COLLECTION_MAX_LENGTH
Defines the number of rectangles that collections can contain.