Skip to content

Commit 561142b

Browse files
committed
add more primitives to series
1 parent 68c0ad2 commit 561142b

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

source/mir/series.d

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,21 @@ struct Series(IndexIterator, SliceKind kind, size_t[] packs, Iterator)
344344
return this[$ - index.assumeSorted.upperBound!sp(moment).length .. $];
345345
}
346346

347+
/**
348+
Gets data for the index.
349+
Params:
350+
moment = index
351+
_default = default value is returned if the series does not contains the index.
352+
Returns:
353+
data that corresponds to the index or default value.
354+
*/
355+
auto ref get(Index, Value)(Index moment, Value _default) inout
356+
if (!is(Value : const(Exception)))
357+
{
358+
size_t idx = index.assumeSorted.lowerBound(moment).length;
359+
return idx < _data._lengths[0] && index[idx] == moment ? data[idx] : _default;
360+
}
361+
347362
/**
348363
Gets data for the index.
349364
Params:
@@ -353,14 +368,21 @@ struct Series(IndexIterator, SliceKind kind, size_t[] packs, Iterator)
353368
Throws:
354369
Exception if the series does not contains the index.
355370
*/
356-
auto ref get(Index)(Index moment, lazy Exception exc = null) inout
371+
auto ref get(Index)(Index moment) inout
372+
{
373+
static immutable defaultExc = new Exception(Unqual!Index.stringof ~ "-" ~ Unqual!(typeof(_data[size_t.init])).stringof ~ " series does not contain required index." );
374+
return get(moment, defaultExc);
375+
}
376+
377+
/// ditto
378+
auto ref get(Index)(Index moment, lazy const Exception exc) inout
357379
{
358380
size_t idx = index.assumeSorted.lowerBound(moment).length;
359381
if (idx < _data._lengths[0] && index[idx] == moment)
360382
{
361383
return data[idx];
362384
}
363-
throw exc ? exc : new Exception(Unqual!Index.stringof ~ "-" ~ Unqual!(typeof(_data[size_t.init])).stringof ~ " series does not contain required index." );
385+
throw exc;
364386
}
365387

366388
///

0 commit comments

Comments
 (0)