Skip to content

Commit ec86773

Browse files
committed
add Series.get
1 parent 81a2847 commit ec86773

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

source/mir/series.d

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ import std.traits;
4040
auto data0 = [1.0, 3, 4];
4141
auto series0 = index0.series(data0);
4242

43+
assert(series0.get(Date(2017, 03, 01)) == 3);
44+
4345
auto index1 = [
4446
Date(2017, 01, 01),
4547
Date(2017, 02, 01),
@@ -333,6 +335,26 @@ struct Series(IndexIterator, SliceKind kind, size_t[] packs, Iterator)
333335
return this[$ - index.assumeSorted.upperBound!sp(moment).length .. $];
334336
}
335337

338+
/**
339+
Gets data for the index.
340+
Params:
341+
moment = index
342+
exc = (lazy, optional) exception to throw if the series does not contains the index.
343+
Returns: data that corresponds to the index.
344+
Throws:
345+
Exception if the series does not contains the index.
346+
*/
347+
auto ref get(Index)(Index moment, lazy Exception exc = null)
348+
{
349+
size_t idx = index.assumeSorted.lowerBound(moment).length;
350+
if (idx < _data._lengths[0] && index[idx] == moment)
351+
{
352+
return data[idx];
353+
}
354+
static immutable e = new Exception(Index.stringof ~ "-" ~ typeof(data[idx]).stringof ~ " series does not contain required index." );
355+
throw exc ? exc : e;
356+
}
357+
336358
///
337359
bool contains(Index)(Index moment)
338360
{

0 commit comments

Comments
 (0)