Changeset c2d50c8 in mainline for uspace/lib/c/generic/adt/dyn_array.c


Ignore:
Timestamp:
2019-08-06T18:29:48Z (5 years ago)
Author:
Matthieu Riolo <matthieu.riolo@…>
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)
Message:

dyn_array: Add concat and clear_range operations

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/adt/dyn_array.c

    r73f7c4e rc2d50c8  
    6868}
    6969
    70 /** Remove item at give position, shift rest of array */
     70/** Remove item at given position, shift rest of array */
    7171void dyn_array_remove(dyn_array_t *da, size_t index)
    7272{
     
    8181{
    8282        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 */
     91void 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 */
     109int 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;
    83124}
    84125
Note: See TracChangeset for help on using the changeset viewer.