| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE.md file. | |
| 4 | |
| 5 library immi_samples.sliding_window; | |
| 6 | |
| 7 import 'sequenced_presenter.dart'; | |
| 8 | |
| 9 import 'package:immi/dart/immi.dart'; | |
| 10 | |
| 11 // Export generated code for nodes in sliding_window.immi | |
| 12 import 'package:immi/dart/sliding_window.dart'; | |
| 13 export 'package:immi/dart/sliding_window.dart'; | |
| 14 | |
| 15 class _Window { | |
| 16 int startOffset = 0; | |
| 17 int windowOffset = 0; | |
| 18 int windowCount = 0; | |
| 19 | |
| 20 void update(int start, int end) { | |
| 21 int shift = start - startOffset; | |
| 22 int absShift = shift < 0 ? -shift : shift; | |
| 23 if (absShift > 0) { | |
| 24 if (absShift < windowCount) { | |
| 25 windowOffset = (windowOffset + shift) % windowCount; | |
| 26 if (windowOffset < 0) { | |
| 27 windowOffset += windowCount; | |
| 28 } | |
| 29 } else { | |
| 30 windowOffset = 0; | |
| 31 } | |
| 32 startOffset = start; | |
| 33 } | |
| 34 windowCount = end - start; | |
| 35 } | |
| 36 } | |
| 37 | |
| 38 class SlidingWindow<T extends Node> { | |
| 39 SequencedPresenter<T> _presenter; | |
| 40 | |
| 41 _Window _display = new _Window(); | |
| 42 int _start = 0; | |
| 43 int _end = 0; | |
| 44 int _minimumCount = 0; | |
| 45 int _maximumCount = -1; | |
| 46 | |
| 47 SlidingWindow(this._presenter); | |
| 48 | |
| 49 SlidingWindowNode present() { | |
| 50 _display.update(_start, _end); | |
| 51 List items = _presentWindow(); | |
| 52 return new SlidingWindowNode( | |
| 53 window: items, | |
| 54 startOffset: _display.startOffset, | |
| 55 windowOffset: _display.windowOffset, | |
| 56 minimumCount: _minimumCount, | |
| 57 maximumCount: _maximumCount, | |
| 58 display: _setDisplayRange, | |
| 59 toggle: _presenter.toggleAt); | |
| 60 } | |
| 61 | |
| 62 void _setDisplayRange(int start, int end) { | |
| 63 assert(start < end); | |
| 64 _start = start; | |
| 65 _end = end; | |
| 66 } | |
| 67 | |
| 68 List<Node> _presentWindow() { | |
| 69 int length = _display.windowCount; | |
| 70 List<Node> items = new List(length); | |
| 71 int startOffset = _display.startOffset; | |
| 72 int windowOffset = _display.windowOffset; | |
| 73 int i = 0; | |
| 74 for (; i < length; ++i) { | |
| 75 int index = startOffset + i; | |
| 76 int windowIndex = (windowOffset + i) % length; | |
| 77 Node item = _presenter.presentAt(index); | |
| 78 if (item == null) break; | |
| 79 items[windowIndex] = item; | |
| 80 } | |
| 81 if (startOffset + i > _minimumCount) { | |
| 82 _minimumCount = startOffset + i; | |
| 83 } | |
| 84 if (i == length) return items; | |
| 85 // If we have reached the last item re-adjust the display info. | |
| 86 _maximumCount = _minimumCount; | |
| 87 _display.windowOffset = 0; | |
| 88 _display.windowCount = i; | |
| 89 List<Node> lastItems = new List(i); | |
| 90 for (int j = 0; j < i; ++j) { | |
| 91 int windowIndex = (windowOffset + j) % length; | |
| 92 lastItems[j] = items[windowIndex]; | |
| 93 } | |
| 94 return lastItems; | |
| 95 } | |
| 96 } | |
| OLD | NEW |