Design Pattern Examples
Overview of object-oriented design patterns
Strategy_SortStrategy.c
Go to the documentation of this file.
1
6
7#include <stdlib.h>
8#include <string.h>
10
11
21static bool _Compare_Name(const EntryInformation* leftEntry, const EntryInformation* rightEntry)
22{
23 bool swapEntries = false;
24
25 if (leftEntry != NULL && rightEntry != NULL)
26 {
27 int result = strcmp(leftEntry->Name, rightEntry->Name);
28 swapEntries = result > 0;
29 }
30
31 return swapEntries;
32}
33
43static bool _Compare_Age(const EntryInformation* leftEntry, const EntryInformation* rightEntry)
44{
45 bool swapEntries = false;
46
47 if (leftEntry != NULL && rightEntry != NULL)
48 {
49 int result = leftEntry->Age - rightEntry->Age;
50 swapEntries = result > 0;
51 }
52
53 return swapEntries;
54}
55
65static bool _Compare_Height(const EntryInformation* leftEntry, const EntryInformation* rightEntry)
66{
67 bool swapEntries = false;
68
69 if (leftEntry != NULL && rightEntry != NULL)
70 {
71 int result = leftEntry->Height - rightEntry->Height;
72 swapEntries = result > 0;
73 }
74
75 return swapEntries;
76}
77
78//=============================================================================
79//=============================================================================
80
84typedef struct
85{
86 const char* name;
89
96{
97 { "ByName" , _Compare_Name },
98 { "ByAge" , _Compare_Age },
99 { "ByHeight", _Compare_Height }
100};
101
102//-----------------------------------------------------------------------------
103
112void SortStrategy_Initialize(SortStrategy* strategy, SortOptions sortOption, bool reversedSort)
113{
114 if (strategy != NULL)
115 {
116 if (sortOption >= 0 && sortOption < (int)sizeof(sortingStrategies) / sizeof(sortingStrategies[0]))
117 {
118 strategy->name = sortingStrategies[sortOption].name;
119 strategy->compareFunction = sortingStrategies[sortOption].compareFunction;
120 strategy->reversedSort = reversedSort;
121 }
122 }
123}
static bool _Compare_Height(const EntryInformation *leftEntry, const EntryInformation *rightEntry)
Compare the height fields of two EntryInformation objects to determine if the two entries need to be ...
void SortStrategy_Initialize(SortStrategy *strategy, SortOptions sortOption, bool reversedSort)
Initialize a SortStrategy object with the desired strategy.
static bool _Compare_Age(const EntryInformation *leftEntry, const EntryInformation *rightEntry)
Compare the age fields of two EntryInformation objects to determine if the two entries need to be swa...
static bool _Compare_Name(const EntryInformation *leftEntry, const EntryInformation *rightEntry)
Compare the name fields of two EntryInformation objects to determine if the two entries need to be sw...
static SortStrategyItem sortingStrategies[]
List of sorting strategies as indexed by a value from the SortOptions enumeration....
Declaration of the SortStrategy structure and the SortStrategy_Initialize() function as used in the S...
SortOptions
Represents the difference sorting strategies supported.
bool(* CompareFunction)(const EntryInformation *, const EntryInformation *)
Alias for a function that compares two EntryInformation objects to determine if the left comes before...
Represents an individual with a Name, Age, and Height.
int Age
Age of this individual, in years.
const char * Name
Name of this individual.
int Height
Height of this individual, in inches.
Represents the strategy to use for sorting EntryInformation objects.
bool reversedSort
True if to reverse the order of the sort.
const char * name
Name of the strategy (for display purposes)
CompareFunction compareFunction
Compare function that determines the order of two entries.
Represents a sorting strategy.
const char * name
Name of the sorting strategy.
CompareFunction compareFunction
compare function to use for the sorting strategy