lookup_table.dart 636 B

1234567891011121314151617181920212223242526272829303132
  1. /// Fixed-size list based lookup table, optimized for small positive integer
  2. /// keys.
  3. class FastLookupTable<T> {
  4. FastLookupTable(Map<int, T> data) {
  5. var maxIndex = data.keys.first;
  6. for (var key in data.keys) {
  7. if (key > maxIndex) {
  8. maxIndex = key;
  9. }
  10. }
  11. this._maxIndex = maxIndex;
  12. _table = List<T?>.filled(maxIndex + 1, null);
  13. for (var entry in data.entries) {
  14. _table[entry.key] = entry.value;
  15. }
  16. }
  17. late final List<T?> _table;
  18. late final int _maxIndex;
  19. T? operator [](int index) {
  20. if (index > _maxIndex) {
  21. return null;
  22. }
  23. return _table[index];
  24. }
  25. }