Browse Source

separate find last index into its own function

1.0.0
Georg Hopp 11 years ago
parent
commit
49c7f8be2c
  1. 1
      include/tr/dynarray.h
  2. 2
      src/dynarray/Makefile.am
  3. 37
      src/dynarray/find_last_index.c
  4. 5
      src/dynarray/put.c

1
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)])

2
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

37
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 <http://www.gnu.org/licenses/>.
*/
#include <stdarg.h>
#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:

5
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;
}

Loading…
Cancel
Save