alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (DoubleLinkedList.scala)

This example Scala source code file (DoubleLinkedList.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

a, a, builder, builder, canbuildfrom, doublelinkedlist, doublelinkedlist, doublelinkedlistlike, genericcanbuildfrom, genericcompanion, linearseq, seqfactory, serializable, serialversionuid

The Scala DoubleLinkedList.scala source code

/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2003-2011, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */



package scala.collection
package mutable

import generic._

/** This class implements double linked lists where both the head (`elem`),
 *  the tail (`next`) and a reference to the previous node (`prev`) are mutable.
 *
 *  @author Matthias Zenger
 *  @author Martin Odersky
 *  @version 2.8
 *  @since   1
 *  
 *  @tparam A     the type of the elements contained in this double linked list.
 *  
 *  @define Coll DoubleLinkedList
 *  @define coll double linked list
 *  @define thatinfo the class of the returned collection. In the standard library configuration,
 *    `That` is always `DoubleLinkedList[B]` because an implicit of type `CanBuildFrom[DoubleLinkedList, B, DoubleLinkedList[B]]`
 *    is defined in object `DoubleLinkedList`.
 *  @define bfinfo an implicit value of class `CanBuildFrom` which determines the
 *    result class `That` from the current representation type `Repr`
 *    and the new element type `B`. This is usually the `canBuildFrom` value
 *    defined in object `DoubleLinkedList`.
 *  @define orderDependent 
 *  @define orderDependentFold
 *  @define mayNotTerminateInf
 *  @define willNotTerminateInf
 */
@SerialVersionUID(-8144992287952814767L)
class DoubleLinkedList[A]() extends LinearSeq[A]
                            with GenericTraversableTemplate[A, DoubleLinkedList]
                            with DoubleLinkedListLike[A, DoubleLinkedList[A]]
                            with Serializable {
  next = this

  /** Creates a node for the double linked list.
   *  
   *  @param elem    the element this node contains.
   *  @param next    the next node in the double linked list.
   */
  def this(elem: A, next: DoubleLinkedList[A]) {
    this()
    if (next != null) {
      this.elem = elem
      this.next = next
      this.next.prev = this
    }
  }

  override def companion: GenericCompanion[DoubleLinkedList] = DoubleLinkedList
}

/** $factoryInfo
 *  @define coll double linked list
 *  @define Coll DoubleLinkedList
 */
object DoubleLinkedList extends SeqFactory[DoubleLinkedList] {
  /** $genericCanBuildFromInfo */
  implicit def canBuildFrom[A]: CanBuildFrom[Coll, A, DoubleLinkedList[A]] = new GenericCanBuildFrom[A]

  def newBuilder[A]: Builder[A, DoubleLinkedList[A]] =
    new Builder[A, DoubleLinkedList[A]] {
      def emptyList() = new DoubleLinkedList[A]()
      var current = emptyList()

      def +=(elem: A): this.type = {
        if (current.isEmpty)
          current = new DoubleLinkedList(elem, emptyList())
        else
          current append new DoubleLinkedList(elem, emptyList())

        this
      }

      def clear(): Unit = current = emptyList()
      def result() = current
    }
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala DoubleLinkedList.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.