seekIndex method

int? seekIndex(
  1. List<MapEntry<String, DynamicMap>> sorted,
  2. DynamicMap data
)

The position where data enters is retrieved by searching from sorted according to the sort setting of filters.

If no sort information is set or data is empty, Null is returned.

sortedからfiltersのソート設定に従って探索しdataが入る位置を取得します。

ソート情報が設定されていなかったりdataが空の場合はNullが返されます。

final query = const ModelQuery(
  "aaaa/bbbb",
  orderBy: "count",
  order: ModelQueryOrder.asc,
);

final index = query.seekIndex(
  [
    MapEntry("dddd", {"count": 1, "text": "a"}),
    MapEntry("dddd", {"count": 4, "text": "a"}),
    MapEntry("dddd", {"count": 8, "text": "a"}),
    MapEntry("dddd", {"count": 10, "text": "a"}),
  ],
  {"count": 5, "text": "a"},
); // 2
final index = query.seekIndex(
  [
    MapEntry("dddd", {"count": 1, "text": "a"}),
    MapEntry("dddd", {"count": 4, "text": "a"}),
    MapEntry("dddd", {"count": 8, "text": "a"}),
    MapEntry("dddd", {"count": 10, "text": "a"}),
  ],
  {"count": 15, "text": "a"},
); // 4
final index = query.seekIndex(
  [
    MapEntry("dddd", {"count": 1, "text": "a"}),
    MapEntry("dddd", {"count": 4, "text": "a"}),
    MapEntry("dddd", {"count": 8, "text": "a"}),
    MapEntry("dddd", {"count": 10, "text": "a"}),
  ],
  {"count": -1, "text": "a"},
); // 0

Implementation

int? seekIndex(
  List<MapEntry<String, DynamicMap>> sorted,
  DynamicMap data,
) {
  if (data.isEmpty) {
    return null;
  }
  final order = filters.firstWhereOrNull((item) =>
      item.type == ModelQueryFilterType.orderByAsc ||
      item.type == ModelQueryFilterType.orderByDesc);
  if (order == null) {
    return null;
  }
  switch (order.type) {
    case ModelQueryFilterType.orderByAsc:
      final key = order.key;
      if (key.isEmpty) {
        return null;
      }
      final value = data[key];
      if (value == null) {
        return sorted.length;
      }
      for (var i = 0; i < sorted.length; i++) {
        final p = i - 1;
        if (i == 0) {
          if (sorted[i].value[key] == null) {
            continue;
          }
          final a = _compare(value, sorted[i].value[key]);
          if (a <= 0) {
            return i;
          }
        } else {
          if (sorted[i].value[key] == null || sorted[p].value[key] == null) {
            continue;
          }
          final a = _compare(value, sorted[i].value[key]);
          final b = _compare(value, sorted[p].value[key]);
          if (a <= 0 && b > 0) {
            return i;
          }
        }
      }
      return sorted.length;
    case ModelQueryFilterType.orderByDesc:
      final key = order.key;
      if (key.isEmpty) {
        return null;
      }
      final value = data[key];
      if (value == null) {
        return sorted.length;
      }
      for (var i = 0; i < sorted.length; i++) {
        final p = i - 1;
        if (i == 0) {
          if (sorted[i].value[key] == null) {
            continue;
          }
          final a = _compare(value, sorted[i].value[key]);
          if (a >= 0) {
            return i;
          }
        } else {
          if (sorted[i].value[key] == null || sorted[p].value[key] == null) {
            continue;
          }
          final a = _compare(value, sorted[i].value[key]);
          final b = _compare(value, sorted[p].value[key]);
          if (a >= 0 && b < 0) {
            return i;
          }
        }
      }
      return sorted.length;
    default:
      return null;
  }
}