On Monday, 25 April 2022 at 05:17:28 UTC, Salih Dincer wrote:
On Monday, 25 April 2022 at 02:19:46 UTC, Ali Çehreli wrote:

This type violates a fundamental rule: Containers and ranges are separate concepts. Your List is a container, not a range. I changed your code by moving the range functions to a Range [...]

Dear Ali,

I implemented a linkedlist over classical logic (*_leaf and const *_root). Now seeing the ranges approach I tried it but without success. The following implementation can work with foreach() though; as long as you call goRoot() :)

```d
class List(dType) {
  struct Node
  {
    dType item;
    Node* next;
  }
  bool FIFO;
  private Node * _leaf;

  this(bool FIFO = true, dType item = dType.init) {
    this.FIFO = FIFO;
    if(FIFO) _leaf= new Node(item, null);
    _root = cast(const)_leaf;
  }

  /*
  auto opSlice() { return Range(_leaf); }
  static struct Range {//*/
    const Node * _root;

    auto empty()
    {
      return !_leaf;
    }

    auto popFront()
    {
      return _leaf = _leaf.next;
    }

    dType front(Node * node = null)
    {
      dType result;
      if(node)
      {
        result = (*node).item;
      } else result = (*_leaf).item;

      return result;
    }
  //}

  alias Next = popFront;
  alias pop = front;
  alias push = InsertBack;

  void InsertBack(dType item)
  {
    _leaf = new Node(item, _leaf);
  }

  void InsertFront(dType item)
  {
    (*_leaf).next = new Node(item, null);
    Next;
  }

  auto goRoot()
  {
    return _leaf = cast(Node*)_root.next;
  }
}

import std.stdio;

enum FIFO { False, True }
enum end = 20;
void main()
{
  int firstNumber = 10;
  auto FIFO_Stack = new List!int;
  with(FIFO_Stack)
  {
    do {
      InsertFront(firstNumber);
    } while(++firstNumber <= end);

    goRoot();

    do pop.writef!"  %s"; while(Next);
    writeln;
  }

  FIFO_Stack.goRoot();

  foreach(stack; FIFO_Stack) {
    stack.writeln;
  }
} /* OUTPUT:
  10  11  12  13  14  15  16  17  18  19  20
10
11
12
13
14
15
16
17
18
19
20
*/
```
SDB@79

I implemented an alternative goroot it's called re_init and next program works.
Still I don't understand what is really going on.
Some state is lost using a class and you have restore it.
And the state is not lost using a struct.

```
cat test.d
import std.stdio: write,writeln;
import std.range: empty,popFront,front;

    struct Node {
        int element;
        Node * next;
    }

    class List {
        Node * root=null;
        Node * walker=null;
        this(int[] AR){foreach(i ; AR)pushfront(i);}
        bool empty() const {return !walker;}
        void popFront() {walker=walker.next;}
        float front() const {return walker.element;}
        void pushfront(int element) {
                Node * newnode=new Node();
                newnode.element=element;
                newnode.next=root;
                root=newnode;
                re_init();
        }
        void re_init(){walker=root;}
    }//List

    void main(){
        List l=new List([3,2,1]);
        l.writeln();
        l.re_init();
        l.writeln();
    }
    ```

Reply via email to