Skip to content

Commit

Permalink
Apply Attila's review
Browse files Browse the repository at this point in the history
  • Loading branch information
mversche committed Nov 15, 2024
1 parent 5c863a8 commit 00ac540
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 51 deletions.
49 changes: 25 additions & 24 deletions groups/bdl/bdlcc/bdlcc_timequeue.h
Original file line number Diff line number Diff line change
Expand Up @@ -1025,22 +1025,24 @@ class TimeQueue {
bsls::TimeInterval *newMinTime = 0,
TimeQueueItem<DATA> *item = 0);

/// Remove all the items from this queue. Optionally specify a `buffer`
/// in which to load the removed items. The resultant items in the
/// `buffer` are ordered by increasing time interval; items of
/// equivalent time interval have arbitrary ordering. Note that the
/// allocator of the `buffer` vector is used to supply memory.
void removeAll(bsl::vector<TimeQueueItem<DATA> > *buffer = 0);
/// Remove all the items from this queue. Optionally specify a
/// `removedItems` vector in which to load the removed items. The
/// resultant items in the `buffer` are ordered by increasing time
/// interval; items of equivalent time interval have arbitrary ordering.
/// Note that the allocator of the `removedItems` vector is used to supply
/// memory.
void removeAll(bsl::vector<TimeQueueItem<DATA> > *removedItems = 0);

/// Remove all the items from this queue for which `predicate` returns
/// `true`. Optionally specify `newLength`, in which to load the number
/// of items remaining in this queue. Optionally specify `newMinTime`,
/// in which to load the lowest remaining time value in this queue.
/// Optionally specify a `buffer` in which to load the removed items.
void removeAll(const bsl::function<bool(const DATA&)>& predicate,
int *newLength = 0,
bsls::TimeInterval *newMinTime = 0,
bsl::vector<TimeQueueItem<DATA> > *buffer = 0);
/// `true`. Optionally specify `newLength`, in which to load the number of
/// items remaining in this queue. Optionally specify `newMinTime`, in
/// which to load the lowest remaining time value in this queue.
/// Optionally specify a vector of `removedItems` in which to load the
/// removed items.
void removeIf(const bsl::function<bool(const DATA&)>& predicate,
int *newLength = 0,
bsls::TimeInterval *newMinTime = 0,
bsl::vector<TimeQueueItem<DATA> > *removedItems = 0);

/// Update the time value of the item having the specified `handle` to
/// the specified `newTime` and optionally load into the optionally
Expand Down Expand Up @@ -1771,7 +1773,7 @@ int TimeQueue<DATA>::remove(typename TimeQueue<DATA>::Handle handle,
}

template <class DATA>
void TimeQueue<DATA>::removeAll(bsl::vector<TimeQueueItem<DATA> > *buffer)
void TimeQueue<DATA>::removeAll(bsl::vector<TimeQueueItem<DATA> > *removedItems)
{
bslmt::LockGuard<bslmt::Mutex> lock(&d_mutex);
MapIter it = d_map.begin();
Expand All @@ -1782,10 +1784,9 @@ void TimeQueue<DATA>::removeAll(bsl::vector<TimeQueueItem<DATA> > *buffer)
Node *const last = first->d_prev_p;
Node *node = first;


do {
if (buffer) {
buffer->push_back(TimeQueueItem<DATA>(
if (removedItems) {
removedItems->push_back(TimeQueueItem<DATA>(
it->first,
node->d_data.object(),
node->d_index,
Expand All @@ -1810,16 +1811,16 @@ void TimeQueue<DATA>::removeAll(bsl::vector<TimeQueueItem<DATA> > *buffer)
}

template <class DATA>
void TimeQueue<DATA>::removeAll(
void TimeQueue<DATA>::removeIf(
const bsl::function<bool(const DATA&)>& predicate,
int *newLength,
bsls::TimeInterval *newMinTime,
bsl::vector<TimeQueueItem<DATA> > *buffer)
bsl::vector<TimeQueueItem<DATA> > *removedItems)
{
bslmt::LockGuard<bslmt::Mutex> lock(&d_mutex);
MapIter it = d_map.begin();

Node *freeNodeList = 0;
MapIter it = d_map.begin();
Node *freeNodeList = 0;
while (d_map.end() != it) {
// We cache the next iterator, in case we erase this element because
// its linked list of nodes is empty.
Expand All @@ -1837,8 +1838,8 @@ void TimeQueue<DATA>::removeAll(

if (predicate(node->d_data.object())) {
// The predicate is `true`, this element should be erased.
if (buffer) {
buffer->push_back(TimeQueueItem<DATA>(
if (removedItems) {
removedItems->push_back(TimeQueueItem<DATA>(
it->first,
node->d_data.object(),
node->d_index,
Expand Down
54 changes: 27 additions & 27 deletions groups/bdl/bdlcc/bdlcc_timequeue.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ using namespace bsl;
// [7 ] void popLE(const bsls::TimeInterval& time,...
// [4 ] int remove(Handle timeId, bsls::TimeInterval *newMinTime=0,...
// [5 ] void removeAll(bsl::vector<bdlcc::TimeQueueItem<DATA> > *buf=0);
// [16] void removeAll(const bsl::function& predicate, ...);
// [16] void removeIf(const bsl::function& predicate, ...);
// [3 ] int add(const bsls::TimeInterval& time, const DATA& data, ...
// [3 ] int add(const bdlcc::TimeQueueItem<DATA> &item, int *isNewTop=0...
// [8 ] int update(int handle, const bsls::TimeInterval &newTime,...
Expand Down Expand Up @@ -134,8 +134,8 @@ void aSsErT(int c, const char *s, int i)
#define ASSERT_SAFE_FAIL(EXPR) BSLS_ASSERTTEST_ASSERT_SAFE_FAIL(EXPR)
#define ASSERT_PASS(EXPR) BSLS_ASSERTTEST_ASSERT_PASS(EXPR)
#define ASSERT_FAIL(EXPR) BSLS_ASSERTTEST_ASSERT_FAIL(EXPR)
#define ASSERT_OPT_PASS(EXPR) BSLS_ASSERTTEST_ASSERT_OPT_PASS(EXPR)
#define ASSERT_OPT_FAIL(EXPR) BSLS_ASSERTTEST_ASSERT_OPT_FAIL(EXPR)
#define ASSERT_OPT_PASS(EXPR) BSLS_ASSERTTEST_ASSERT_OPT_PASS(EXPR)
#define ASSERT_OPT_FAIL(EXPR) BSLS_ASSERTTEST_ASSERT_OPT_FAIL(EXPR)


// ============================================================================
Expand Down Expand Up @@ -1591,10 +1591,10 @@ int main(int argc, char *argv[])
} break;
case 16: {
// --------------------------------------------------------------------
// TEST REMOVEALL MANIPULATOR W/ PREDICATE
// TEST REMOVEIF MANIPULATOR W/ PREDICATE
//
// Concerns:
// 1. `removeAll` removes only those items for which the supplied
// 1. `removeIf` removes only those items for which the supplied
// predicate function is `true`.
//
// 2. That if `newLength` is supplied, it is loaded with the new length.
Expand All @@ -1606,34 +1606,34 @@ int main(int argc, char *argv[])
// elements.
//
// 5. That if an optional argument is not supplied, the output argument
// is ignored and the `removeAll` function behaves as expected.
// is ignored and the `removeIf` function behaves as expected.
//
// 5. That after calling `removeAll` the time queue is left in a valid
// 5. That after calling `removeIf` the time queue is left in a valid
// state.
//
// Plan:
// 1. Test the `gg` function using a series of hand chosen input strings.
//
// 2. Using a table based testing approach, using `gg`, initialize
// a `TimeQueue<char>` into a series of states, and use `removeAll`
// a `TimeQueue<char>` into a series of states, and use `removeIf`
// to remove all lower case characters. Verify the removed items
// and resulting time queue state with expectations. (C1-4)
//
// 3. Call `removeAll` with different sets of optional arguments, and
// 3. Call `removeIf` with different sets of optional arguments, and
// verify its basic behavior does not change. (C-5)
//
// 4. Using a table based testing approach, using `gg`, initialize
// a `TimeQueue<char>` into a series of states, use `removeAll`
// a `TimeQueue<char>` into a series of states, use `removeIf`
// to remove lower case letters, then use use `gg` again to
// add additional elements. Verify the resulting state is
// the expected state. (C-5).
//
// Testing:
// void removeAll(const bsl::function& predicate, ...);
// void removeIf(const bsl::function& predicate, ...);
// --------------------------------------------------------------------

if (verbose) cout << endl
<< "Testing `removeAll` manipulator" << endl
<< "Testing `removeIf` manipulator" << endl
<< "===============================" << endl;

using namespace TIMEQUEUE_TEST_CASE_16;
Expand All @@ -1643,7 +1643,7 @@ int main(int argc, char *argv[])

cout << "\tTesting `gg` generator langauge." << endl;
{
typedef bdlcc::TimeQueue<char> Obj;
typedef bdlcc::TimeQueue<char> Obj;
typedef bsl::vector<bdlcc::TimeQueueItem<char> > Contents;

// Testing valid inputs
Expand Down Expand Up @@ -1698,18 +1698,18 @@ int main(int argc, char *argv[])
}
}

cout << "\tTesting `removeAll` removes correct items." << endl;
cout << "\tTesting `removeIf` removes correct items." << endl;
{
struct {
int d_lineNum;
int d_line;

const char *d_ggInput; // A `gg` expression for the
// initial queue
const char *d_ggInput; // A `gg` generator expression for
// the initial queue

const char *d_removed; // Sequence of characters removed
// (ordered by time)

const char *d_remaining; // sequence of characters ramining
const char *d_remaining; // sequence of characters remaining
// (ordered by time)
} DATA[] = {
// Note that we will use `isLower` as the predicate for
Expand All @@ -1729,7 +1729,7 @@ int main(int argc, char *argv[])
const int NUM_VALUES = sizeof DATA / sizeof *DATA;

for (int i = 0; i < NUM_VALUES; ++i) {
const int LINE = DATA[i].d_lineNum;
const int LINE = DATA[i].d_line;
const char *INPUT = DATA[i].d_ggInput;
const char *REMOVED = DATA[i].d_removed;
const char *REMAINING = DATA[i].d_remaining;
Expand All @@ -1746,7 +1746,7 @@ int main(int argc, char *argv[])
bsl::vector<bdlcc::TimeQueueItem<char> > removed, remaining;

// Call the function under test.
mX.removeAll(&bdlb::CharType::isLower,
mX.removeIf(&bdlb::CharType::isLower,
&newLength,
&newMinTime,
&removed);
Expand Down Expand Up @@ -1786,7 +1786,7 @@ int main(int argc, char *argv[])
}
}

cout << "\tTesting behavior for default optional parameters." << endl;
cout << "\tTesting behavior for default optional parameters." << endl;

{
const char *INPUT = "1a 2b 3C 4d 5E 6g";
Expand All @@ -1803,7 +1803,7 @@ int main(int argc, char *argv[])

bsl::vector<bdlcc::TimeQueueItem<char> > remain;
gg(&mX, INPUT);
mX.removeAll(&bdlb::CharType::isLower);
mX.removeIf(&bdlb::CharType::isLower);

bsls::TimeInterval minTime;
ASSERTV(0 == X.minTime(&minTime));
Expand All @@ -1829,7 +1829,7 @@ int main(int argc, char *argv[])

bsl::vector<bdlcc::TimeQueueItem<char> > remain;
gg(&mX, INPUT);
mX.removeAll(&bdlb::CharType::isLower, &newLength);
mX.removeIf(&bdlb::CharType::isLower, &newLength);

bsls::TimeInterval minTime;
ASSERTV(0 == X.minTime(&minTime));
Expand Down Expand Up @@ -1857,7 +1857,7 @@ int main(int argc, char *argv[])

bsl::vector<bdlcc::TimeQueueItem<char> > remain;
gg(&mX, INPUT);
mX.removeAll(&bdlb::CharType::isLower, &newLength, &minTime);
mX.removeIf(&bdlb::CharType::isLower, &newLength, &minTime);


ASSERTV(NEW_LENGTH, newLength, NEW_LENGTH == newLength);
Expand All @@ -1878,7 +1878,7 @@ int main(int argc, char *argv[])

}

cout << "\tTesting `add` afer `removeAll`." << endl;
cout << "\tTesting `add` afer `removeIf`." << endl;
{
struct {
int d_lineNum;
Expand Down Expand Up @@ -1918,10 +1918,10 @@ int main(int argc, char *argv[])

bsl::vector<bdlcc::TimeQueueItem<char> > remaining;

// Add initial items, call removeAll, then insert additional items.
// Add initial items, call removeIf, then insert additional items.

gg(&mX, INPUT_1);
mX.removeAll(&bdlb::CharType::isLower);
mX.removeIf(&bdlb::CharType::isLower);
gg(&mX, INPUT_2);

mX.removeAll(&remaining);
Expand Down

0 comments on commit 00ac540

Please sign in to comment.