Commit 49c7f8be2cf6dfa5bded68a1c08e9c225aaef9dc

Authored by Georg Hopp
1 parent 054fab8b

separate find last index into its own function

... ... @@ -36,6 +36,7 @@ TR_CLASSVARS_DECL(TR_Dynarray) {};
36 36
37 37 size_t TR_darrPut(TR_Dynarray, const void *);
38 38 void TR_darrPutAt(TR_Dynarray, const void *, size_t);
  39 +size_t TR_darrFindLastIndex(TR_Dynarray);
39 40
40 41 #define TR_darrGet(this, idx) ((this)->data[(idx)])
41 42
... ...
... ... @@ -4,7 +4,7 @@ AUTOMAKE_OPTIONS = subdir-objects
4 4 AM_CFLAGS += -I../../include/ -std=c99 -DREENTRANT -lpthread
5 5 AM_LDFLAGS += -lpthread
6 6
7   -DYNARRAY = dynarray.c put.c
  7 +DYNARRAY = dynarray.c put.c find_last_index.c
8 8
9 9 noinst_LTLIBRARIES = libdynarray.la
10 10
... ...
  1 +/**
  2 + * \file
  3 + *
  4 + * \author Georg Hopp
  5 + *
  6 + * \copyright
  7 + * Copyright © 2014 Georg Hopp
  8 + *
  9 + * This program is free software: you can redistribute it and/or modify
  10 + * it under the terms of the GNU General Public License as published by
  11 + * the Free Software Foundation, either version 3 of the License, or
  12 + * (at your option) any later version.
  13 + *
  14 + * This program is distributed in the hope that it will be useful,
  15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17 + * GNU General Public License for more details.
  18 + *
  19 + * You should have received a copy of the GNU General Public License
  20 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21 + */
  22 +
  23 +#include <stdarg.h>
  24 +
  25 +#include "trbase.h"
  26 +#include "tr/dynarray.h"
  27 +
  28 +size_t
  29 +TR_darrFindLastIndex(TR_Dynarray this)
  30 +{
  31 + size_t i = this->size;
  32 +
  33 + while (this->data && ! this->data[--i]);
  34 + return this->data && this->data[i] ? i+1 : i;
  35 +}
  36 +
  37 +// vim: set ts=4 sw=4:
... ...
... ... @@ -42,10 +42,9 @@ _darrResize(TR_Dynarray this, size_t index)
42 42 size_t
43 43 TR_darrPut(TR_Dynarray this, const void * data)
44 44 {
45   - size_t i = this->size;
  45 + size_t i = TR_darrFindLastIndex(this);
46 46
47   - while (this->data && ! this->data[--i]);
48   - TR_darrPutAt(this, data, this->data && this->data[i] ? i+1 : i);
  47 + TR_darrPutAt(this, data, i);
49 48
50 49 return i;
51 50 }
... ...
Please register or login to post a comment