Procházet zdrojové kódy

Merge branch 'feature/improve_line_manipulation_performance' into circularlist

xuty před 4 roky
rodič
revize
6e79973b4a
3 změnil soubory, kde provedl 20 přidání a 29 odebrání
  1. 3 3
      lib/buffer/buffer.dart
  2. 1 1
      lib/buffer/buffer_line.dart
  3. 16 25
      lib/utli/circular_list.dart

+ 3 - 3
lib/buffer/buffer.dart

@@ -480,9 +480,9 @@ class Buffer {
 
   void resize(int newWidth, int newHeight) {
     if (newWidth > terminal.viewWidth) {
-      lines.forEach((item, index) {
-        item?.ensure(newWidth);
-      }, true);
+      for (var line in lines) {
+        line.ensure(newWidth);
+      }
     }
 
     if (newHeight > terminal.viewHeight) {

+ 1 - 1
lib/buffer/buffer_line.dart

@@ -85,7 +85,7 @@ class BufferLine {
   }
 
   void clear() {
-    removeRange(0, (_cells.lengthInBytes / _cellSize).floor());
+    removeRange(0, _cells.lengthInBytes ~/ _cellSize);
   }
 
   void erase(Cursor cursor, int start, int end) {

+ 16 - 25
lib/utli/circular_list.dart

@@ -1,12 +1,12 @@
-class CircularList<T> {
-  List<T?> _array;
-  int _length = 0;
-  int _startIndex = 0;
-
-  Function(int num)? onTrimmed;
+import 'dart:collection';
 
+class CircularList<T> with ListMixin<T> {
   CircularList(int maxLength) : _array = List<T?>.filled(maxLength, null);
 
+  late List<T?> _array;
+  var _length = 0;
+  var _startIndex = 0;
+
   // Gets the cyclic index for the specified regular index. The cyclic index can then be used on the
   // backing array to get the element associated with the regular index.
   int _getCyclicIndex(int index) {
@@ -48,15 +48,15 @@ class CircularList<T> {
     _length = value;
   }
 
-  void forEach(
-    void Function(T? item, int index) callback, [
-    bool includeBuffer = false,
-  ]) {
-    final len = includeBuffer ? _array.length : _length;
-    for (int i = 0; i < len; i++) {
-      callback(_array[_getCyclicIndex(i)], i);
-    }
-  }
+  // void forEach(
+  //   void Function(T? item, int index) callback, [
+  //   bool includeBuffer = false,
+  // ]) {
+  //   final len = includeBuffer ? _array.length : _length;
+  //   for (int i = 0; i < len; i++) {
+  //     callback(_array[_getCyclicIndex(i)], i);
+  //   }
+  // }
 
   T operator [](int index) {
     if (index >= length) {
@@ -86,7 +86,6 @@ class CircularList<T> {
       if (_startIndex == _array.length) {
         _startIndex = 0;
       }
-      onTrimmed?.call(1);
     } else {
       _length++;
     }
@@ -137,7 +136,6 @@ class CircularList<T> {
       final countToTrim = _length + items.length - _array.length;
       _startIndex += countToTrim;
       length = _array.length;
-      onTrimmed?.call(countToTrim);
     } else {
       _length += items.length;
     }
@@ -145,11 +143,9 @@ class CircularList<T> {
 
   void trimStart(int count) {
     if (count > _length) count = _length;
-
-    // TODO: perhaps bug in original code, this does not clamp the value of startIndex
     _startIndex += count;
+    _startIndex %= _array.length;
     _length -= count;
-    onTrimmed?.call(count);
   }
 
   void shiftElements(int start, int count, int offset) {
@@ -168,7 +164,6 @@ class CircularList<T> {
         while (_length > _array.length) {
           length--;
           _startIndex++;
-          onTrimmed?.call(1);
         }
       }
     } else {
@@ -179,8 +174,4 @@ class CircularList<T> {
   }
 
   bool get isFull => length == maxLength;
-
-  List<T> toList() {
-    return List<T>.generate(length, (index) => this[index]!);
-  }
 }