Files
smooth-app/packages/smooth_app/lib/helpers/collections_helper.dart
Edouard Marquez ca1fa29ea2 feat: Scanner improvements (#1781)
* All barcode detections are now computed in an Isolate

* Add a setStateSafe method

* Only accept 1D barcodes

* Between each decoding a window is passed

* Let's try to reduce a little bit the quality of the camera

* Improve a little bit the documentation

* Fix a typo

* Fix some typos

* Fixes issues highlighted by MonsieurTanuki in the PR

* Remove irrelevant `mount` check

* Fix wrong import
2022-05-11 16:49:58 +02:00

51 lines
1.2 KiB
Dart

import 'dart:collection';
import 'package:collection/collection.dart';
/// List of [num] with a max length of [_maxCapacity], where we can easily
/// compute the average value of all elements.
class AverageList<T extends num> with ListMixin<T> {
static const int _maxCapacity = 10;
final List<T> _elements = <T>[];
int average(int defaultValueIfEmpty) {
if (_elements.isEmpty) {
return defaultValueIfEmpty;
} else {
return _elements.average.floor();
}
}
@override
int get length => _elements.length;
@override
T operator [](int index) => throw UnsupportedError(
'Please only use the "add" method',
);
@override
void operator []=(int index, T value) {
if (index > _maxCapacity) {
throw UnsupportedError('The index is above the capacity!');
} else {
_elements[index] = value;
}
}
@override
void add(T element) {
// The first element is always the latest added
_elements.insert(0, element);
if (_elements.length >= _maxCapacity) {
_elements.removeLast();
}
}
@override
set length(int newLength) {
throw UnimplementedError('This list has a fixed size of $_maxCapacity');
}
}