How to implement Iterable<E> - dart

I am trying to port the Java code below to Dart and am puzzled about to do this.
In Java the Iterable interface is where clean with one method and to implement this is a snap.
How is this code best transformed to Dart?
/**
* Chess squares represented as a bitmap.
*/
public class ChessSquares implements Iterable<ChessSquare> {
private static class ChessSquaresIterator implements Iterator<ChessSquare> {
long bits;
int nextBit;
public ChessSquaresIterator(long bits) {
this.bits = bits;
nextBit = Long.numberOfTrailingZeros(bits);
}
#Override
public boolean hasNext() {
return (nextBit < 64);
}
#Override
public ChessSquare next() {
ChessSquare sq = ChessSquare.values()[nextBit];
bits = bits & ~sq.bit;
nextBit = Long.numberOfTrailingZeros(bits);
return sq;
}
#Override
public void remove() {
throw new UnsupportedOperationException();
}
}
#Override
public Iterator<ChessSquare> iterator() {
return new ChessSquaresIterator(bits);
}
...

By using IterableMixin you only need to implement the iterator-function.
class ChessSquares with IterableMixin<ChessSquare> {
#override
Iterator<ChessSquare> get iterator => new ChessSquaresIterator(bits);
...
}
Visit http://blog.sethladd.com/2013/03/first-look-at-dart-mixins.html for a short introduction on mixins.
The Iterator-interface is straight forward. You only have to implement the function moveNext and the getter current.

Soo I tried this which is kind of not what I want since I do not want to extend a base class.
/**
* Chess squares represented as a bitmap.
*/
class ChessSquares extends IterableBase<ChessSquare> {
Iterator<ChessSquare> get iterator {
return new ChessSquaresIterator(this);
}
...
}
class ChessSquaresIterator extends Iterator<ChessSquare> {
int _nextBit;
int64 _bits;
ChessSquare _current;
ChessSquaresIterator(ChessSquares squares) {
_bits = new int64.fromInt(squares._bits);
}
bool moveNext() {
_nextBit = _bits.numberOfTrailingZeros();
if (_nextBit < 64) {
_current = ChessSquare.values()[_nextBit];
_bits = _bits & ~_current.bit();
} else {
_current = null;
}
return _nextBit < 64;
}
E get current => _current;
}

Related

I want to know, in the code provided what does 'this' keyword referring to.?

/*
I know this keyword is used to refer to class members but i am unable to understand what does 'this'.isEmpty() referring to. Some class ? class method? Or some variable?
For example:
this.value = value;
I understand that here, this.value is referring to class variable value but not for previous occurence of 'this'.
*/
public class StackWithMin extends Stack< NodeWithMin > {
public void push(int value) {
int newMin = Math.min(value, min());
super.push(new NodeWithMin(value,newMin));
}
public int min() {
if(this.isEmpty()) {
return Integer.MAX_VALUE; // Error value
} else {
return peek().min;
}
}
}
class NodeWithMin {
public int value;
public int min;
public NodeWithMin(int value, int min) {
this.value = v;
this.min = min;
}
}
"this" here is object of the class StackWithMin which is extending Stack class from java.util. so StackWithMin is instance of Stack class.
this.isEmpty() which is method defined in Stack, here checks if the stack has any element or not. if it has zero element it returns true else false.
Hope it clears your doubt.

How to implement generic class spezialization in Dart

In Dart we can use generic classes [class]. We can also specialize those classes [class]. However at runtime the specialization is not used. (In C++ this is called template programming)
Example: The following code will result in the output
Hallo world
How are you
class MyClass<T> {
foo( print('Hallo world'); );
}
class MyClassInt implements MyClass<int> {
#override
foo( print('How are you'); );
}
main() {
MyClass<int> a = Myclass<int>();
MyClassInt b = MyClassInt();
a.foo();
b.foo();
}
How can the specialization (here type [int]) be done, that it is called at runtime, i.e.
main() {
MyClass<int> a = Myclass<int>();
a.foo();
}
should result in the outcome "How are you".
As mentioned by jamesdlin, Dart does not support specialization. But you can do something like this to make the illusion:
class MyClass<T> {
factory MyClass() {
if (T == int) {
return MyClassInt() as MyClass<T>;
} else {
return MyClass._();
}
}
// Hidden real constructor for MyClass
MyClass._();
void foo() {
print('Hallo world');
}
}
class MyClassInt implements MyClass<int> {
#override
void foo() {
print('How are you');
}
}
void main() {
final a = MyClass<int>();
final b = MyClassInt();
final c = MyClass<String>();
a.foo(); // How are you
b.foo(); // How are you
c.foo(); // Hallo world
}

Writing a size() method for a User-defined Stack Class

I'm writing a program that requires the length/size of a stack. Because I am not importing the Stack class (and I've made my own - see below) I don't know how to make a method that calculates the size of the stack and returns that integer value. Here's the Stack class so far:
public class Stack<String> implements StackInter<String>
{
public void push(String x)
{ // This method is written
}
public String pop()
{ // This method is written
}
public boolean isEmptyStack()
{ // This method is written
}
public String peek()
{ // This method is written
}
public int size()
{
// What goes in here!
}
}
This is where I want to use the size method
public class InfixCalculator
{
Stack<String> stack = new Stack<String>();
int size = stack.size();
}
Any suggestions would be greatly appreciated!
the pseudo code is somthing like this:
public class Stack<String> implements StackInter<String>
{
private size;
//constructor
public Stack<String>(){
size=0;
}
public void push(String x)
{ // This method is written
size++;
}
public String pop()
{ // This method is written
if(size > 0 )
size--;
}
public boolean isEmptyStack()
{ // This method is written
}
public String peek()
{ // This method is written
}
public int size()
{
int theSize=size;
return theSize;
}
}
after you apply your operations the size can be computed.
If your Stack is an array write
public int size() {
return array.length;
}

Override method in dart on fly (like JAVA)

Is there way to overriding method in Dart like JAVA, for example:
public class A {
public void handleLoad() {
}
}
And when overriding:
A a = new A() {
#Override
public void handleLoad() {
// do some code
}
};
No, Dart does not have anonymous classes. You have to create a class that extends A and instantiate it.
No but it much less useful in Dart because you can just reassign function:
typedef void PrintMsg(msg);
class Printer {
PrintMsg foo = (m) => print(m);
}
main() {
Printer p = new Printer()
..foo('Hello') // Hello
..foo = ((String msg) => print(msg.toUpperCase()))
..foo('Hello'); //HELLO
}
However you will need some extra boilerplate to access instance.
Use type Function:
class A {
final Function h
A(this.h);
void handleLoad(String loadResult) { h(loadResult); }
}
Or
class A {
final Function handleLoad;
A(this.handleLoad);
}
A a = new A((String loadResult){
//do smth.
});

a last in, first out (LIFO) abstract data type and data structure. Perhaps the most common use of stacks is to store

MyStack()
{
Vector<Integer> v=new Vector<Integer>(10,2);
}
void push(int n)
{
v.addElement(n);
}
void pop()
{
if(v.isEmpty())
System.out.println("Stack underflow!");
else
System.out.println(v.elementAt(0));
}
void display()
{
for(int i=0;i<v.size();i++)
System.out.print(v.elementAt(i) +" ");
}
}
class StackDemo
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
MyStack s=new MyStack();
int option=0;
do
{
System.out.println("1: Push\n2:Pop\n3:Display\n4:Quit");
System.out.println("Enter your option: ");
option=in.nextInt();
switch(option)
{
case 1:
{
System.out.println("Enter an integer:");
int n=in.nextInt();
s.push(n);break;
}
case 2:s.pop();break;
case 3:s.display();break;
}
}
while(option!=4);
}
}
// throws an error: variable v not found. Any help would be much appreciated.Thanks.
It looks like v is being created locally in your constructor instead of as a member of your class.
Try defining v as a class member and then simply assign it in your constructor.
class MyStack {
Vector<Integer> v;
public MyStack() {
v = new Vector<Integer>(10,2);
}
}
Or just assign it when you define it:
class MyStack {
Vector<Integer> v = new Vector<Integer>(10,2);
}
Check out the Java tutorial on class members.

Resources