Stack - scala implementation / performance issue

I looked at scala-lang notes on various data structures and their performance characteristics.

I noticed that it immutable.Stackhas C (Const.) Complexity for both adding and adding, while the stack mutable.Stackhas C complexity for adding and L (linear) complexity for adding. It surprised me a little.

I believe that β€œadding” simply means push()to the top of the stack. And since the difficulties for adding and adding are different, does this mean that the β€œadd” actually puts something at the bottom of the stack? Why does it work better (C for mutable) than adding (L for mutable)? And also, how can I even report to the stack? I do not see any method suitable for this in scaladoc .

EDIT.

As @ Łukasz noted in the comments, you can add and add to the stack with +:and operators :+. The question remains, though - why is adding working better (faster) than adding to the stack? Should I add from below and not push up?

+4
source share
1

, , . , push Stack , :+ , :+ SeqLike, ,

List ::, . List L, Stack

:

def push[B >: A](elem: B): Stack[B] = new Stack(elem :: elems)

mutable :

def push(elem: A): this.type = { elems = elem :: elems; this }

, Stack 2.11

P.S. 2.12, , , 2.11

P.P.S. Stack, , , , Stack , , L append column

+2

Source: https://habr.com/ru/post/1626203/


All Articles