InsertBefore in DList of structs

drug drug2004 at bk.ru
Mon Mar 4 10:03:21 UTC 2019


On 04.03.2019 11:14, r-const-dev wrote:
> I have a DList of structs, DataPoint, ordered by a struct field, time. 
> I'm trying to insert a new entry preserving order, so I'm trying to use 
> `until` to find the insertion point and `insertBefore` with the result.
> 
> struct DataPoint {
>      immutable ulong time;
>      ulong value;
> }
> 
> void main() {
>      DList!DataPoint dataPoints;
>      void incrementAt(ulong time) {
>          auto dataPoint = until!(dp => dp.time >= time)(dataPoints);
>          if (dataPoint.time == time) {
>              ++dataPoint.value;
>          } else {
>              dataPoints.insertBefore(dataPoint, DataPoint(time, 1));
>          }
>      }
> }
> 
> But I'm getting:
> 
> /dlang/dmd/linux/bin64/../../src/phobos/std/algorithm/searching.d(4898): 
> Error: template instance `onlineapp.main.incrementAt.Until!(__lambda2, 
> DList!(DataPoint), void)` does not match template declaration 
> Until(alias pred, Range, Sentinel) if (isInputRange!Range)
> onlineapp.d(14): Error: template instance 
> `onlineapp.main.incrementAt.until!((dp) => dp.time >= time, 
> DList!(DataPoint))` error instantiating

dataPoints itself isn't a range, you need to use dataPoints[]. Also
insertStable needs DList.Range, not Until!... one. You can do something 
like this https://run.dlang.io/is/A2vZjW


More information about the Digitalmars-d-learn mailing list