/
ContainerUtils.h
executable file
·157 lines (123 loc) · 4.8 KB
/
ContainerUtils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**************************************************************************
StringUtils.h - assorted container utility functions
Credits:
- By Paul-Michael Agapow, 2003, Dept. Biology, University College
London, London WC1E 6BT, UK.
- <mail://p.agapow@ucl.ac.uk> <http://www.bio.ic.ac.uk/evolve/>
About:
- Some templated functions for use with most containers.
- StringsUtils has some specialisations of these.
Changes:
- 00.12.8: created from portions of StringsUtils..
To Do:
-
**************************************************************************/
#ifndef CONTAINERUTILS_H
#define CONTAINERUTILS_H
#include "Sbl.h"
#include <algorithm>
SBL_NAMESPACE_START
// *** CONSTANTS, DEFINES & PROTOTYPES
#pragma mark Constants
// *** MEMBERSHIP FUNCTIONS **********************************************/
#pragma mark -
template <typename TERMITER, typename TARGETITER>
bool
isMemberOf
(TERMITER iStartTerms, TERMITER iStopTerms, TARGETITER iStartTarget,
TARGETITER iStopTarget)
//: can any of terms in the first container be found in the second?
// This only tells you whether any of the search terms are in the target,
// not where they are or how many times they occur. Use the raw STL
// algorithms for that.
{
// Generalized to non-string containers, the design of this fxn deserves
// some explanation. If either the iterator or the search term are passed
// in via references (which you would do if you wanted to pass out the
// position of the found term or just save time copying) then you can't
// make calls like this: "isMemberOf ('c', myStr.begin(), myStr.end());"
// as neither the first iterator nor the searchterm can be modified.
// TO DO: should this be const?
// for each value in the search terms container
for (; iStartTerms != iStopTerms; iStartTerms++)
{
// see if you can find it in the target container
if (std::find (iStartTarget, iStopTarget, *iStartTerms) != iStopTarget)
return true;
}
return false;
}
template <typename X, typename TARGETITER>
bool
isMemberOf
(X iSearchTerm, TARGETITER iStartTarget, TARGETITER iStopTarget)
//: can the search term be found in the target container?
// See notes on isMemberOf (iter1, iter1, iter2, iter2).
{
if (std::find (iStartTarget, iStopTarget, iSearchTerm) == iStopTarget)
return false;
else
return true;
}
template <typename TERMITER, typename TARGETITER>
bool
isSubsetOf
(TERMITER iStartTerms, TERMITER iStopTerms, TARGETITER iStartTarget,
TARGETITER iStopTarget)
//: can all of terms in the first container be found in the second?
// This only tells you whether all of the search terms are in the target,
// not where they are or how many times they occur.
{
// for each value in the search terms container
for (; iStartTerms != iStopTerms; iStartTerms++)
{
// see if you can find it in the target container
if (std::find (iStartTarget, iStopTarget, *iStartTerms) == iStopTarget)
return false;
}
return true;
}
// *** DEPRECIATED, DEBUG, DEVELOPMENT ***********************************/
#pragma mark -
#if 0
// To run test function, set to 1.
// Note this may generate a "function has no prototype" warning.
void testContainerUtils ()
//: Simply a test suite for the functions within this module.
{
DBG_MSG ("*** Testing container utils ...");
DBG_MSG ("");
DBG_MSG ("* Testing membership functions:");
int theArr1[] = {1, 2, 3};
int theArr2[] = {3};
int theArr3[] = {4, 6, 2};
DBG_MSG ("Array 1 is {1, 2, 3}");
DBG_MSG ("Array 2 is {3}");
DBG_MSG ("Array 3 is {4, 6, 2}");
bool theResult[6];
theResult[0] = isMemberOf (theArr1, theArr1+3, theArr2, theArr2+1);
theResult[1] = isMemberOf (theArr1, theArr1+3, theArr3, theArr3+3);
theResult[2] = isMemberOf (theArr2, theArr2+1, theArr1, theArr1+3);
theResult[3] = isMemberOf (theArr2, theArr2+1, theArr3, theArr3+3);
theResult[4] = isMemberOf (theArr3, theArr3+3, theArr1, theArr1+3);
theResult[5] = isMemberOf (theArr3, theArr3+3, theArr2, theArr2+1);
DBG_MSG ("The seq membership results are:");
DBG_MSG (" is array 1 in array 2 (expect t): " << theResult[0]);
DBG_MSG (" is array 1 in array 3 (expect t): " << theResult[1]);
DBG_MSG (" is array 2 in array 1 (expect t): " << theResult[2]);
DBG_MSG (" is array 2 in array 3 (expect f): " << theResult[3]);
DBG_MSG (" is array 3 in array 1 (expect t): " << theResult[4]);
DBG_MSG (" is array 3 in array 2 (expect f): " << theResult[5]);
DBG_MSG ("The single membership results are:");
DBG_MSG (" is 2 in array 1 (expect t): " << isMemberOf (2,
theArr1, theArr1+3));
DBG_MSG (" is 2 in array 2 (expect f): " << isMemberOf (2,
theArr2, theArr2+1));
DBG_MSG (" is 2 in array 3 (expect t): " << isMemberOf (2,
theArr3, theArr3+3));
DBG_MSG ("Finished testing container utils.");
}
#endif
SBL_NAMESPACE_STOP
#endif
// *** END ***************************************************************/