Просмотр исходного кода

change CircularList to not reuse item instance but just manage the list cycle

devmil 4 лет назад
Родитель
Сommit
cf1aae2241
2 измененных файлов с 9 добавлено и 30 удалено
  1. 6 10
      lib/buffer/buffer.dart
  2. 3 20
      lib/utli/circular_list.dart

+ 6 - 10
lib/buffer/buffer.dart

@@ -13,12 +13,10 @@ class Buffer {
 
 
     lines = CircularList(
     lines = CircularList(
       terminal.maxLines,
       terminal.maxLines,
-      (BufferLine? line) {
-        line?.clear();
-        line?.ensure(terminal.viewWidth);
-      },
-      (int) => _newEmptyLine(),
     );
     );
+    for (int i = 0; i < terminal.viewHeight; i++) {
+      lines.push(_newEmptyLine());
+    }
   }
   }
 
 
   final Terminal terminal;
   final Terminal terminal;
@@ -274,7 +272,7 @@ class Buffer {
     // the cursor is not in the scrollable region
     // the cursor is not in the scrollable region
     if (_cursorY >= terminal.viewHeight - 1) {
     if (_cursorY >= terminal.viewHeight - 1) {
       // we are at the bottom so a new line is created.
       // we are at the bottom so a new line is created.
-      lines.addNew();
+      lines.push(_newEmptyLine());
 
 
       // keep viewport from moving if user is scrolling.
       // keep viewport from moving if user is scrolling.
       if (isUserScrolling) {
       if (isUserScrolling) {
@@ -424,7 +422,7 @@ class Buffer {
   void clear() {
   void clear() {
     lines.clear();
     lines.clear();
     for (int i = 0; i < terminal.viewHeight; i++) {
     for (int i = 0; i < terminal.viewHeight; i++) {
-      lines.addNew();
+      lines.push(_newEmptyLine());
     }
     }
   }
   }
 
 
@@ -453,8 +451,6 @@ class Buffer {
       final newLine = _newEmptyLine();
       final newLine = _newEmptyLine();
       lines.splice(index, 0, [newLine]);
       lines.splice(index, 0, [newLine]);
     } else {
     } else {
-      final bottom = convertViewLineToRawLine(marginBottom);
-
       final newLine = _newEmptyLine();
       final newLine = _newEmptyLine();
       lines.splice(_cursorY, 0, [newLine]);
       lines.splice(_cursorY, 0, [newLine]);
     }
     }
@@ -493,7 +489,7 @@ class Buffer {
       // Grow larger
       // Grow larger
       for (var i = 0; i < newHeight - terminal.viewHeight; i++) {
       for (var i = 0; i < newHeight - terminal.viewHeight; i++) {
         if (_cursorY < terminal.viewHeight - 1) {
         if (_cursorY < terminal.viewHeight - 1) {
-          lines.addNew();
+          lines.push(_newEmptyLine());
         } else {
         } else {
           _cursorY++;
           _cursorY++;
         }
         }

+ 3 - 20
lib/utli/circular_list.dart

@@ -1,19 +1,11 @@
-import 'package:quiver/iterables.dart';
-
 class CircularList<T> {
 class CircularList<T> {
   List<T?> _array;
   List<T?> _array;
   int _length = 0;
   int _length = 0;
   int _startIndex = 0;
   int _startIndex = 0;
 
 
   Function(int num)? onTrimmed;
   Function(int num)? onTrimmed;
-  Function(T? item) _prepareItem;
 
 
-  CircularList(int maxLength, Function(T? item) prepareItem,
-      [T? Function(int index)? initialValueGenerator])
-      : _array = (initialValueGenerator == null)
-            ? List<T?>.filled(maxLength, null)
-            : List<T?>.generate(maxLength, initialValueGenerator),
-        _prepareItem = prepareItem;
+  CircularList(int maxLength) : _array = List<T?>.filled(maxLength, null);
 
 
   // Gets the cyclic index for the specified regular index. The cyclic index can then be used on the
   // 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.
   // backing array to get the element associated with the regular index.
@@ -76,8 +68,8 @@ class CircularList<T> {
     _length = 0;
     _length = 0;
   }
   }
 
 
-  void addNew() {
-    _prepareItem(_array[_getCyclicIndex(_length)]);
+  void push(T value) {
+    _array[_getCyclicIndex(_length)] = value;
     if (_length == _array.length) {
     if (_length == _array.length) {
       _startIndex++;
       _startIndex++;
       if (_startIndex == _array.length) {
       if (_startIndex == _array.length) {
@@ -89,15 +81,6 @@ class CircularList<T> {
     }
     }
   }
   }
 
 
-  T? recycle() {
-    if (length != maxLength) {
-      throw Exception('Can only recycle when the buffer is full');
-    }
-    _startIndex = ++_startIndex & maxLength;
-
-    return _array[_getCyclicIndex(length - 1)];
-  }
-
   /// Removes and returns the last value on the list
   /// Removes and returns the last value on the list
   T pop() {
   T pop() {
     return _array[_getCyclicIndex(_length-- - 1)]!;
     return _array[_getCyclicIndex(_length-- - 1)]!;