Changeset c2d50c8 in mainline
- Timestamp:
- 2019-08-06T18:29:48Z (6 years ago)
- Children:
- 72c8f77
- Parents:
- 73f7c4e
- git-author:
- Michal Koutný <xm.koutny+hos@…> (2015-05-26 16:12:03)
- git-committer:
- Matthieu Riolo <matthieu.riolo@…> (2019-08-06 18:29:48)
- Location:
- uspace/lib/c
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/adt/dyn_array.c
r73f7c4e rc2d50c8 68 68 } 69 69 70 /** Remove item at give position, shift rest of array */70 /** Remove item at given position, shift rest of array */ 71 71 void dyn_array_remove(dyn_array_t *da, size_t index) 72 72 { … … 81 81 { 82 82 da->size = 0; 83 } 84 85 /** Clear subsequence of array 86 * 87 * @param[in/out] da 88 * @param[in] begin index of first item to remove 89 * @param[in] end index behind last item to remove 90 */ 91 void dyn_array_clear_range(dyn_array_t *da, size_t begin, size_t end) 92 { 93 assert(begin < da->size); 94 assert(end <= da->size); 95 96 _dyn_array_unshift(da, begin, end - begin); 97 int rc = dyn_array_reserve(da, da->size); 98 assert(rc == EOK); 99 } 100 101 /** Concatenate two arrays 102 * 103 * @param[in/out] da1 first array and concatenated output 104 * @param[in] da2 second array, it is untouched 105 * 106 * @return EOK on success 107 * @return ENOMEM when allocation fails 108 */ 109 int dyn_array_concat(dyn_array_t *da1, dyn_array_t *da2) 110 { 111 assert(da1->_item_size == da2->_item_size); 112 113 int rc = dyn_array_reserve(da1, da1->size + da2->size); 114 if (rc != EOK) { 115 return rc; 116 } 117 void *dst = da1->_data + da1->size * da1->_item_size; 118 void *src = da2->_data; 119 size_t size = da1->_item_size * da2->size; 120 memcpy(dst, src, size); 121 da1->size += da2->size; 122 123 return EOK; 83 124 } 84 125 -
uspace/lib/c/include/adt/dyn_array.h
r73f7c4e rc2d50c8 128 128 extern void dyn_array_remove(dyn_array_t *, size_t); 129 129 void dyn_array_clear(dyn_array_t *); 130 extern int dyn_array_reserve(dyn_array_t *, size_t); 130 void dyn_array_clear_range(dyn_array_t *, size_t, size_t); 131 extern int dyn_array_concat(dyn_array_t *, dyn_array_t *); 132 extern int dyn_array_reserve(dyn_array_t *, size_t); 131 133 132 134 extern void _dyn_array_initialize(dyn_array_t *, size_t); -
uspace/lib/c/test/dyn_array.c
r73f7c4e rc2d50c8 136 136 } 137 137 138 PCUT_TEST(clear_range_middle) { 139 dyn_array_append(&da, data_t, 10); 140 dyn_array_append(&da, data_t, 11); 141 dyn_array_append(&da, data_t, 12); 142 dyn_array_append(&da, data_t, 99); 143 144 dyn_array_clear_range(&da, 1, 3); 145 PCUT_ASSERT_INT_EQUALS(2, da.size); 146 PCUT_ASSERT_INT_EQUALS(10, dyn_array_at(&da, data_t, 0)); 147 PCUT_ASSERT_INT_EQUALS(99, dyn_array_at(&da, data_t, 1)); 148 } 149 150 PCUT_TEST(clear_range_begin) { 151 dyn_array_append(&da, data_t, 10); 152 dyn_array_append(&da, data_t, 11); 153 dyn_array_append(&da, data_t, 12); 154 dyn_array_append(&da, data_t, 99); 155 156 dyn_array_clear_range(&da, 0, 2); 157 PCUT_ASSERT_INT_EQUALS(2, da.size); 158 PCUT_ASSERT_INT_EQUALS(12, dyn_array_at(&da, data_t, 0)); 159 PCUT_ASSERT_INT_EQUALS(99, dyn_array_at(&da, data_t, 1)); 160 } 161 162 PCUT_TEST(clear_range_end) { 163 dyn_array_append(&da, data_t, 10); 164 dyn_array_append(&da, data_t, 11); 165 dyn_array_append(&da, data_t, 12); 166 dyn_array_append(&da, data_t, 99); 167 168 dyn_array_clear_range(&da, 2, 4); 169 PCUT_ASSERT_INT_EQUALS(2, da.size); 170 PCUT_ASSERT_INT_EQUALS(10, dyn_array_at(&da, data_t, 0)); 171 PCUT_ASSERT_INT_EQUALS(11, dyn_array_at(&da, data_t, 1)); 172 } 173 174 PCUT_TEST(clear_range_empty) { 175 dyn_array_append(&da, data_t, 10); 176 dyn_array_append(&da, data_t, 99); 177 178 dyn_array_clear_range(&da, 0, 0); 179 PCUT_ASSERT_INT_EQUALS(2, da.size); 180 PCUT_ASSERT_INT_EQUALS(10, dyn_array_at(&da, data_t, 0)); 181 PCUT_ASSERT_INT_EQUALS(99, dyn_array_at(&da, data_t, 1)); 182 } 183 184 PCUT_TEST(concat_simple) { 185 dyn_array_append(&da, data_t, 10); 186 dyn_array_append(&da, data_t, 99); 187 188 dyn_array_t da2; 189 dyn_array_initialize(&da2, data_t); 190 dyn_array_append(&da2, data_t, 30); 191 dyn_array_append(&da2, data_t, 31); 192 193 dyn_array_concat(&da, &da2); 194 PCUT_ASSERT_INT_EQUALS(4, da.size); 195 PCUT_ASSERT_INT_EQUALS(2, da2.size); 196 197 PCUT_ASSERT_INT_EQUALS(10, dyn_array_at(&da, data_t, 0)); 198 PCUT_ASSERT_INT_EQUALS(99, dyn_array_at(&da, data_t, 1)); 199 PCUT_ASSERT_INT_EQUALS(30, dyn_array_at(&da, data_t, 2)); 200 PCUT_ASSERT_INT_EQUALS(31, dyn_array_at(&da, data_t, 3)); 201 202 dyn_array_destroy(&da2); 203 } 204 PCUT_TEST(concat_self) { 205 dyn_array_append(&da, data_t, 10); 206 dyn_array_append(&da, data_t, 99); 207 208 dyn_array_concat(&da, &da); 209 PCUT_ASSERT_INT_EQUALS(4, da.size); 210 211 PCUT_ASSERT_INT_EQUALS(10, dyn_array_at(&da, data_t, 0)); 212 PCUT_ASSERT_INT_EQUALS(99, dyn_array_at(&da, data_t, 1)); 213 PCUT_ASSERT_INT_EQUALS(10, dyn_array_at(&da, data_t, 2)); 214 PCUT_ASSERT_INT_EQUALS(99, dyn_array_at(&da, data_t, 3)); 215 } 216 138 217 PCUT_EXPORT(dyn_array);
Note:
See TracChangeset
for help on using the changeset viewer.