Design Pattern Examples
Overview of object-oriented design patterns
Iterator_Exercise.c
Go to the documentation of this file.
1
6
7#include <stdbool.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11
12#include "Iterator_Iterators.h"
13#include "Iterator_Exercise.h"
14
15//=============================================================================
16//=============================================================================
17
18
19//=============================================================================
20//=============================================================================
21
31// ! [Using Iterator in C]
33{
34 printf("\nIterator Exercise\n");
35
36 // For this example, the class already has built into it the data
37 // to be iterated over.
38
39 printf(" Iterating over keys only:\n");
40 KeyIterator keyIterator;
41 Iterator_GetKeys(&keyIterator);
42 while (Iterator_NextKey(&keyIterator))
43 {
44 printf(" %s\n", keyIterator.key);
45 }
46
47 printf(" Iterating over values only:\n");
48 ValueIterator valueIterator;
49 Iterator_GetValues(&valueIterator);
50 while (Iterator_NextValue(&valueIterator))
51 {
52 printf(" %s\n", valueIterator.value);
53 }
54
55 printf(" Iterating over all items:\n");
56 ItemIterator itemIterator;
57 Iterator_GetItems(&itemIterator);
58 while (Iterator_NextItem(&itemIterator))
59 {
60 printf(" %s = %s\n", itemIterator.item.key, itemIterator.item.value);
61 }
62
63 printf(" Done.\n");
64}
65// ! [Using Iterator in C]
void Iterator_Exercise(void)
Example of using the Iterator Pattern.
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.
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(),...
Declaration of the Iterator_Exercise() function as used in the Iterator Pattern.
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...
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.
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 ...