| 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 file. | |
| 4 | |
| 5 import 'node.dart' show | |
| 6 Node; | |
| 7 | |
| 8 class NodeStack { | |
| 9 List<Node> data = <Node>[]; | |
| 10 | |
| 11 int get size => data.length; | |
| 12 | |
| 13 // Stack interface. | |
| 14 void pushNode(Node node) { | |
| 15 data.add(node); | |
| 16 } | |
| 17 | |
| 18 Node popNode() { | |
| 19 return data.removeLast(); | |
| 20 } | |
| 21 | |
| 22 /// Returns the top of the stack or [null] if the stack is empty. | |
| 23 Node topNode() { | |
| 24 return data.isNotEmpty ? data.last : null; | |
| 25 } | |
| 26 } | |
| 27 | |
| 28 class Popper<T extends Node> { | |
| 29 NodeStack stack; | |
| 30 Popper(this.stack); | |
| 31 | |
| 32 T popNodeIfMatching() { | |
| 33 return (stack.topNode() is T) ? stack.popNode() : null; | |
| 34 } | |
| 35 | |
| 36 List<T> popNodesWhileMatching() { | |
| 37 List<T> result = <T>[]; | |
| 38 while (stack.topNode() is T) { | |
| 39 result.add(stack.popNode()); | |
| 40 } | |
| 41 return result; | |
| 42 } | |
| 43 | |
| 44 List<T> popNodes(int count) { | |
| 45 List<T> result = new List<T>(count); | |
| 46 assert(count <= stack.data.length); | |
| 47 int oldLength = stack.data.length; | |
| 48 int newLength = oldLength - count; | |
| 49 for (int i = newLength; i < oldLength; ++i) { | |
| 50 result[i - newLength] = stack.data[i]; | |
| 51 } | |
| 52 stack.data.length = newLength; | |
| 53 return result; | |
| 54 } | |
| 55 } | |
| OLD | NEW |