From 49c7f8be2cf6dfa5bded68a1c08e9c225aaef9dc Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Sun, 26 Oct 2014 06:21:38 +0000 Subject: [PATCH] separate find last index into its own function --- include/tr/dynarray.h | 1 + src/dynarray/Makefile.am | 2 +- src/dynarray/find_last_index.c | 37 ++++++++++++++++++++++++++++++++++ src/dynarray/put.c | 5 ++--- 4 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 src/dynarray/find_last_index.c diff --git a/include/tr/dynarray.h b/include/tr/dynarray.h index 094c493..a5db29b 100644 --- a/include/tr/dynarray.h +++ b/include/tr/dynarray.h @@ -36,6 +36,7 @@ TR_CLASSVARS_DECL(TR_Dynarray) {}; size_t TR_darrPut(TR_Dynarray, const void *); void TR_darrPutAt(TR_Dynarray, const void *, size_t); +size_t TR_darrFindLastIndex(TR_Dynarray); #define TR_darrGet(this, idx) ((this)->data[(idx)]) diff --git a/src/dynarray/Makefile.am b/src/dynarray/Makefile.am index f094ba7..ad60394 100644 --- a/src/dynarray/Makefile.am +++ b/src/dynarray/Makefile.am @@ -4,7 +4,7 @@ AUTOMAKE_OPTIONS = subdir-objects AM_CFLAGS += -I../../include/ -std=c99 -DREENTRANT -lpthread AM_LDFLAGS += -lpthread -DYNARRAY = dynarray.c put.c +DYNARRAY = dynarray.c put.c find_last_index.c noinst_LTLIBRARIES = libdynarray.la diff --git a/src/dynarray/find_last_index.c b/src/dynarray/find_last_index.c new file mode 100644 index 0000000..1815586 --- /dev/null +++ b/src/dynarray/find_last_index.c @@ -0,0 +1,37 @@ +/** + * \file + * + * \author Georg Hopp + * + * \copyright + * Copyright © 2014 Georg Hopp + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include + +#include "trbase.h" +#include "tr/dynarray.h" + +size_t +TR_darrFindLastIndex(TR_Dynarray this) +{ + size_t i = this->size; + + while (this->data && ! this->data[--i]); + return this->data && this->data[i] ? i+1 : i; +} + +// vim: set ts=4 sw=4: diff --git a/src/dynarray/put.c b/src/dynarray/put.c index e5d9355..27e4af4 100644 --- a/src/dynarray/put.c +++ b/src/dynarray/put.c @@ -42,10 +42,9 @@ _darrResize(TR_Dynarray this, size_t index) size_t TR_darrPut(TR_Dynarray this, const void * data) { - size_t i = this->size; + size_t i = TR_darrFindLastIndex(this); - while (this->data && ! this->data[--i]); - TR_darrPutAt(this, data, this->data && this->data[i] ? i+1 : i); + TR_darrPutAt(this, data, i); return i; }