Skip to content

Commit

Permalink
v1.0.11
Browse files Browse the repository at this point in the history
- Improved performance: `toUint8List32Reversed` and `reverseBytes`.
- Added `BigInt.thousands`.
  • Loading branch information
gmpassos committed Jan 3, 2022
1 parent 2c38e8a commit 1046210
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.0.11

- Improved performance: `toUint8List32Reversed` and `reverseBytes`.
- Added `BigInt.thousands`.

## 1.0.10

- Optimize `Statistics` to allow computation of big numbers without overflow issues.
Expand Down
94 changes: 92 additions & 2 deletions lib/src/statistics_extension_num.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,12 @@ extension IntExtension on int {
}
/// Same as [toUint8List32], but with bytes in reversed order.
Uint8List toUint8List32Reversed() => toUint8List32().reverseBytes();
Uint8List toUint8List32Reversed() {
var bs = Uint8List(4);
var data = bs.asByteData();
data.setUint32(0, this, Endian.little);
return bs;
}
/// Same as [toUint8List64], but with bytes in reversed order.
Uint8List toUint8List64Reversed() => toUint8List64().reverseBytes();
Expand Down Expand Up @@ -1160,6 +1165,8 @@ extension BigIntExtension on BigInt {
Uint8List toUint8List32() => toInt().toUint8List32();
Uint8List toUint8List64() => toInt().toUint8List64();
String get thousands => toInt().thousands;
}
/// Numeric extension for [String].
Expand Down Expand Up @@ -1263,7 +1270,90 @@ extension Uint8ListExtension on Uint8List {
String toStringUTF8() => dart_convert.utf8.decode(this);
/// Returns `this` instance in a reversed order.
Uint8List reverseBytes() => Uint8List.fromList(reversed.toList());
Uint8List reverseBytes() {
switch (length) {
case 1:
{
var bs = Uint8List(1);
bs[0] = this[0];
return bs;
}
case 2:
{
var bs = Uint8List(2);
bs[0] = this[1];
bs[1] = this[0];
return bs;
}
case 3:
{
var bs = Uint8List(3);
bs[0] = this[2];
bs[1] = this[1];
bs[2] = this[0];
return bs;
}
case 4:
{
var bs = Uint8List(4);
bs[0] = this[3];
bs[1] = this[2];
bs[2] = this[1];
bs[3] = this[0];
return bs;
}
case 5:
{
var bs = Uint8List(5);
bs[0] = this[4];
bs[1] = this[3];
bs[2] = this[2];
bs[3] = this[1];
bs[4] = this[0];
return bs;
}
case 6:
{
var bs = Uint8List(6);
bs[0] = this[5];
bs[1] = this[4];
bs[2] = this[3];
bs[3] = this[2];
bs[4] = this[1];
bs[5] = this[0];
return bs;
}
case 7:
{
var bs = Uint8List(7);
bs[0] = this[6];
bs[1] = this[5];
bs[2] = this[4];
bs[3] = this[3];
bs[4] = this[2];
bs[5] = this[1];
bs[6] = this[0];
return bs;
}
case 8:
{
var bs = Uint8List(8);
bs[0] = this[7];
bs[1] = this[6];
bs[2] = this[5];
bs[3] = this[4];
bs[4] = this[3];
bs[5] = this[2];
bs[6] = this[1];
bs[7] = this[0];
return bs;
}
default:
{
return Uint8List.fromList(reversed.toList());
}
}
}
/// Converts `this` bytes to HEX.
String toHex({Endian endian = Endian.big}) {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: statistics
description: Statistics package for easy and efficient data manipulation with many built-in mathematical functions and tools.
version: 1.0.10
version: 1.0.11
homepage: https://github.com/gmpassos/statistics

environment:
Expand Down
49 changes: 47 additions & 2 deletions test/statistics_extension_num_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ void main() {
expect((-123).thousands, equals('-123'));
expect((-1123).thousands, equals('-1,123'));
expect((-1123456).thousands, equals('-1,123,456'));

expect(123.toBigInt().thousands, equals('123'));
expect(1123.toBigInt().thousands, equals('1,123'));
expect(1123456.toBigInt().thousands, equals('1,123,456'));

expect((-123).toBigInt().thousands, equals('-123'));
expect((-1123).toBigInt().thousands, equals('-1,123'));
expect((-1123456).toBigInt().thousands, equals('-1,123,456'));
});

test('toUint8List32/64', () {
Expand Down Expand Up @@ -418,6 +426,11 @@ void main() {
expect(_asNum(10).naturalExponent, equals(22026.465794806718));
});

test('toBigInt', () {
expect(10.0.toBigInt(), equals(10.toBigInt()));
expect(12.3.toBigInt(), equals(12.toBigInt()));
});

test('toIntsList', () {
expect(<double>[].toIntsList(), isEmpty);
expect(<double>[].toIntsList(), isEmpty);
Expand Down Expand Up @@ -988,8 +1001,40 @@ void main() {
expect(Uint8List.fromList([1, 10, 20, 30]).bytesHashCode(),
equals(1176475097));

expect(Uint8List.fromList([1, 10, 20, 30]).reverseBytes(),
equals([30, 20, 10, 1]));
{
expect(Uint8List.fromList([10]).reverseBytes(), equals([10]));

expect(Uint8List.fromList([10, 20]).reverseBytes(), equals([20, 10]));

expect(Uint8List.fromList([10, 20, 30]).reverseBytes(),
equals([30, 20, 10]));

expect(Uint8List.fromList([10, 20, 30, 40]).reverseBytes(),
equals([40, 30, 20, 10]));

expect(Uint8List.fromList([10, 20, 30, 40, 50]).reverseBytes(),
equals([50, 40, 30, 20, 10]));

expect(Uint8List.fromList([10, 20, 30, 40, 50, 60]).reverseBytes(),
equals([60, 50, 40, 30, 20, 10]));

expect(Uint8List.fromList([10, 20, 30, 40, 50, 60, 70]).reverseBytes(),
equals([70, 60, 50, 40, 30, 20, 10]));

expect(
Uint8List.fromList([10, 20, 30, 40, 50, 60, 70, 80]).reverseBytes(),
equals([80, 70, 60, 50, 40, 30, 20, 10]));

expect(
Uint8List.fromList([10, 20, 30, 40, 50, 60, 70, 80, 90])
.reverseBytes(),
equals([90, 80, 70, 60, 50, 40, 30, 20, 10]));

expect(
Uint8List.fromList([10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
.reverseBytes(),
equals([100, 90, 80, 70, 60, 50, 40, 30, 20, 10]));
}

expect(Uint8List.fromList([65, 66, 67, 68]).toStringLatin1(),
equals('ABCD'));
Expand Down
10 changes: 9 additions & 1 deletion test/statistics_tools_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ void main() {

expect(chronometer.isStarted, isFalse);
expect(chronometer.isFinished, isFalse);
expect(chronometer.elapsedTimeMs, equals(0));

chronometer.start();

Expand Down Expand Up @@ -264,11 +265,18 @@ void main() {

chronometer2.start();
await Future.delayed(Duration(milliseconds: 100));
chronometer2.stop(operations: 1);

expect(chronometer2.elapsedTimeMs >= 100, isTrue);
expect(chronometer2.elapsedTimeMs < 200, isTrue);

chronometer2.stop(operations: 1);

var elapsedTime = chronometer2.elapsedTimeMs;

await Future.delayed(Duration(milliseconds: 100));

expect(chronometer2.elapsedTimeMs, equals(elapsedTime));

expect(chronometer2.elapsedTimeSec >= 0.1, isTrue);
expect(chronometer2.elapsedTimeSec < 0.2, isTrue);

Expand Down

0 comments on commit 1046210

Please sign in to comment.