Design Pattern Examples
Overview of object-oriented design patterns
Iterator_Iterators.c
Go to the documentation of this file.
1
7
9
10#include <stdlib.h>
11
12
17static const char* _keys[] = { "One" , "Two" , "Three" };
18
23static const char* _values[] = { "Value 1", "Value 2", "Value 3" };
24
25
26//=============================================================================
27//=============================================================================
28
30// Iterator_GetItems()
33{
34 if (iterator != NULL)
35 {
36 iterator->item.key = NULL;
37 iterator->item.value = NULL;
38 iterator->iterator = 0; // The iterator field is the index into the data.
39 }
40}
41
43// Iterator_GetItems()
46{
47 if (iterator != NULL)
48 {
49 iterator->key = NULL;
50 iterator->iterator = 0; // The iterator field is the index into the data.
51 }
52}
53
55// Iterator_GetItems()
58{
59 if (iterator != NULL)
60 {
61 iterator->value = NULL;
62 iterator->iterator = 0; // The iterator field is the index into the data.
63 }
64}
65
67// Iterator_GetItems()
70{
71 bool retrievedItem = false;
72
73 if (iterator != NULL)
74 {
75 size_t index = iterator->iterator;
76 size_t numKeys = sizeof(_keys) / sizeof(_keys[0]);
77
78 if (index < numKeys)
79 {
80 iterator->item.key = _keys[index];
81 iterator->item.value = _values[index];
82 index++;
83 iterator->iterator = index;
84 retrievedItem = true;
85 }
86 else
87 {
88 iterator->item.key = NULL;
89 iterator->item.value = NULL;
90 }
91 }
92
93 return retrievedItem;
94}
95
97// Iterator_GetItems()
100{
101 bool retrievedKey = false;
102
103 if (iterator != NULL)
104 {
105 size_t index = iterator->iterator;
106 size_t numKeys = sizeof(_keys) / sizeof(_keys[0]);
107
108 if (index < numKeys)
109 {
110 iterator->key = _keys[index];
111 index++;
112 iterator->iterator = index;
113 retrievedKey = true;
114 }
115 else
116 {
117 iterator->key = NULL;
118 }
119 }
120
121 return retrievedKey;
122}
123
125// Iterator_GetItems()
128{
129 bool retrievedValue = false;
130
131 if (iterator != NULL)
132 {
133 size_t index = iterator->iterator;
134 size_t numValues = sizeof(_values) / sizeof(_values[0]);
135
136 if (index < numValues)
137 {
138 iterator->value = _values[index];
139 index++;
140 iterator->iterator = index;
141 retrievedValue = true;
142 }
143 else
144 {
145 iterator->value = NULL;
146 }
147 }
148
149 return retrievedValue;
150}
151
static const char * _values[]
A list of values as example data. The number of values must match the number of keys in the _keys lis...
void Iterator_GetItems(ItemIterator *iterator)
Retrieve an iterator over the whole items in the internal data structure.
void Iterator_GetValues(ValueIterator *iterator)
Retrieve an iterator over the values in the internal data structure.
void Iterator_GetKeys(KeyIterator *iterator)
Retrieve an iterator over the keys in the internal data structure.
static const char * _keys[]
A list of keys as example data. The number of keys must match the number of values in the _values lis...
bool Iterator_NextValue(ValueIterator *iterator)
Retrieve the next value (const char*) from the iterator.
bool Iterator_NextKey(KeyIterator *iterator)
Retrieve the next key (const char*) from the iterator.
bool Iterator_NextItem(ItemIterator *iterator)
Retrieve the next whole item (ItemPair) from the iterator.
Declaration of the ItemPair structure, along with the iterator functions, Iterator_GetItems(),...
Represents an iterator that retrieves whole items.
ItemPair item
The key/value pair pointed to by this iterator. item.key and item.value are NULL if Iterator_NextItem...
size_t iterator
The (opaque) iterator value tracking progress through the data.
const char * value
The value part of the item.
const char * key
The key part of the item.
Represents an iterator that retrieves the key of each item.
size_t iterator
The (opaque) iterator value tracking progress through the data.
const char * key
The key pointed to by this iterator. key is NULL if Iterator_NextKey() has not yet been called or the...
Represents an iterator that retrieves the value of each item.
const char * value
The value pointed to by this iterator. value is NULL if Iterator_NextValue() has not yet been called ...
size_t iterator
The (opaque) iterator value tracking progress through the data.