Commit 9b18e8b25acc576c89ce7d7a5fde9d1d4f92461a

Authored by Georg Hopp
1 parent 8b6600be

Add iterable interface and make TR_List and derivated classed iterable

... ... @@ -7,4 +7,5 @@ nobase_include_HEADERS = trdata.h \
7 7 tr/tree.h \
8 8 tr/dynarray.h \
9 9 tr/heap.h \
10   - tr/interface/hashable.h
  10 + tr/interface/hashable.h \
  11 + tr/interface/iterable.h
... ...
  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 +#ifndef __TR_INTERFACE_ITERABLE_H__
  24 +#define __TR_INTERFACE_ITERABLE_H__
  25 +
  26 +#include "trbase.h"
  27 +
  28 +typedef void * (* fptr_TR_iterableCurrent)(void *);
  29 +typedef void (* fptr_TR_iterableNext)(void *);
  30 +typedef void (* fptr_TR_iterableRewind)(void *);
  31 +typedef int (* fptr_TR_iterableValid)(void *);
  32 +
  33 +TR_INTERFACE(TR_Iterable) {
  34 + TR_IFID;
  35 + fptr_TR_iterableCurrent current;
  36 + fptr_TR_iterableNext next;
  37 + fptr_TR_iterableRewind rewind;
  38 + fptr_TR_iterableValid valid;
  39 +};
  40 +
  41 +#define TR_iterableForeach(this) \
  42 + TR_iterableRewind((this)); \
  43 + for (; TR_iterableValid((this)); TR_iterableNext((this)))
  44 +
  45 +extern void * TR_iterableCurrent(void *);
  46 +extern void TR_iterableNext(void *);
  47 +extern void TR_iterableRewind(void *);
  48 +extern int TR_iterableValid(void *);
  49 +
  50 +#endif // __TR_INTERFACE_ITERABLE_H__
  51 +
  52 +// vim: set ts=4 sw=4:
... ...
... ... @@ -37,6 +37,9 @@ TR_CLASS(TR_List) {
37 37 size_t end;
38 38 void ** data;
39 39 int free_msgs;
  40 +
  41 + // for iterable interface
  42 + size_t current;
40 43 };
41 44 TR_INSTANCE_INIT(TR_List);
42 45 TR_CLASSVARS_DECL(TR_List) {};
... ...
... ... @@ -10,6 +10,7 @@
10 10 #include "tr/dynarray.h"
11 11 #include "tr/heap.h"
12 12 #include "tr/interface/hashable.h"
  13 +#include "tr/interface/iterable.h"
13 14
14 15 #endif // __TR_DATA_H__
15 16
... ...
... ... @@ -12,9 +12,11 @@ TRDATALIBS = cbuf/libcbuf.la \
12 12 dynarray/libdynarray.la \
13 13 heap/libheap.la
14 14
  15 +ITERABLE = interface/iterable.c
  16 +
15 17 lib_LTLIBRARIES = libtrdata.la
16 18
17   -libtrdata_la_SOURCES =
  19 +libtrdata_la_SOURCES = $(ITERABLE)
18 20 libtrdata_la_CFLAGS = $(AM_CFLAGS)
19 21 libtrdata_la_LIBADD = $(TRDATALIBS)
20 22 libtrdata_la_LDFLAGS = -version-info 0:0:0 $(AM_LDFLAGS)
... ...
  1 +/**
  2 + * \file
  3 + *
  4 + * \author Georg Hopp
  5 + *
  6 + * \copyright
  7 + * Copyright © 2012 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 "trbase.h"
  24 +#include "tr/interface/iterable.h"
  25 +
  26 +TR_CREATE_INTERFACE(TR_Iterable, 4);
  27 +
  28 +void *
  29 +TR_iterableCurrent(void * iterable)
  30 +{
  31 + void * ret;
  32 + TR_RETCALL(iterable, TR_Iterable, current, ret);
  33 + return ret;
  34 +}
  35 +
  36 +void
  37 +TR_iterableNext(void * iterable)
  38 +{
  39 + TR_CALL(iterable, TR_Iterable, next);
  40 +}
  41 +
  42 +void
  43 +TR_iterableRewind(void * iterable)
  44 +{
  45 + TR_CALL(iterable, TR_Iterable, rewind);
  46 +}
  47 +
  48 +int
  49 +TR_iterableValid(void * iterable)
  50 +{
  51 + int ret;
  52 + TR_RETCALL(iterable, TR_Iterable, valid, ret);
  53 + return ret;
  54 +}
  55 +
  56 +// vim: set ts=4 sw=4:
... ...
... ... @@ -24,6 +24,7 @@
24 24
25 25 #include "trbase.h"
26 26 #include "tr/list.h"
  27 +#include "tr/interface/iterable.h"
27 28
28 29 static
29 30 int
... ... @@ -33,7 +34,7 @@ listCtor(void * _this, va_list * params)
33 34
34 35 this->data = (void **)TR_malloc(32 * sizeof(void *));
35 36 this->size = TR_getUsableSize(this->data) / sizeof(void *);
36   - this->start = this->end = 0;
  37 + this->start = this->end = this->current = 0;
37 38 this->free_msgs = 1;
38 39
39 40 return 0;
... ... @@ -60,7 +61,40 @@ listDtor(void * _this)
60 61 TR_MEM_FREE(this->data);
61 62 }
62 63
  64 +static
  65 +void *
  66 +listCurrent(void * _this)
  67 +{
  68 + TR_List this = _this;
  69 + return this->data[this->current];
  70 +}
  71 +
  72 +static
  73 +void
  74 +listNext(void * _this)
  75 +{
  76 + TR_List this = _this;
  77 + this->current = this->current + 1 == this->size ? 0 : this->current + 1;
  78 +}
  79 +
  80 +static
  81 +void
  82 +listRewind(void * _this)
  83 +{
  84 + TR_List this = _this;
  85 + this->current = this->start;
  86 +}
  87 +
  88 +static
  89 +int
  90 +listValid(void * _this)
  91 +{
  92 + TR_List this = _this;
  93 + return this->current != this->end;
  94 +}
  95 +
63 96 TR_INIT_IFACE(TR_Class, listCtor, listDtor, NULL);
64   -TR_CREATE_CLASS(TR_List, NULL, NULL, TR_IF(TR_Class));
  97 +TR_INIT_IFACE(TR_Iterable, listCurrent, listNext, listRewind, listValid);
  98 +TR_CREATE_CLASS(TR_List, NULL, NULL, TR_IF(TR_Class), TR_IF(TR_Iterable));
65 99
66 100 // vim: set ts=4 sw=4:
... ...
Please register or login to post a comment