summaryrefslogtreecommitdiff
path: root/src/library/scalax/collection/generic/covartest/OrderedIterableTemplate.scala
blob: f3c509887c65f83aedd18235538a627eef0a1d37 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2003-2009, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

// $Id: Iterable.scala 15188 2008-05-24 15:01:02Z stepancheg $


package scalax.collection.generic.covartest

import OrderedIterable._

import util.control.Breaks._

/** Ordered iterables are iterables where the `elements` method always returns elements in the same
 *  order (namely the order in which elements were appended to the iterable). In particular, one has
 *  for every two ordered iterables `xs` and `ys`:
 *
 *  `(xs ++ ys).elements = xs.elements ++ ys.elements
 */
trait OrderedIterableTemplate[+CC[+B] <: OrderedIterableTemplate[CC, B] with OrderedIterable[B], +A]
  extends IterableTemplate[CC, A] {

  /** The last element of this iterable.
   *
   *  @throws Predef.NoSuchElementException if the iterable is empty.
   *  @note  Might return different results for different runs, unless this iterable is ordered
   */
  def last: A = {
    var lst = head
    for (x <- this)
      lst = x
    lst
  }

  /** Returns as an option the last element of this iterable or
   *  <code>None</code> if iterable is empty.
   *
   *  @return the last element as an option.
   *  @note  Might return different results for different runs, unless this iterable is ordered
   */
  def lastOption: Option[A] = if (isEmpty) None else Some(last)

  /** An iterable consisting of all elements of this iterable except the last one.
   *  @throws Predef.UnsupportedOperationException if the stream is empty.
   *  @note  Might return different results for different runs, unless this iterable is ordered
   */
  def init: CC[A] = {
    if (isEmpty) throw new UnsupportedOperationException("empty.init")
    var lst = head
    val b = newBuilder[A]
    for (x <- this) {
      b += lst
      lst = x
    }
    b.result
  }

  /** Returns the rightmost <code>n</code> elements from this iterable.
   *
   *  @param n the number of elements to take
   *  @note  Might return different results for different runs, unless this iterable is ordered
   */
  def takeRight(n: Int): CC[A] = {
    val b = newBuilder[A]
    val lead = elements drop n
    var go = false
    for (x <- this) {
      if (go) b += x
      else if (lead.hasNext) lead.next
      else go = true
    }
    b.result
  }

  /** Returns the iterable wihtout its rightmost <code>n</code> elements.
   *
   *  @param n the number of elements to take
   *  @note  Might return different results for different runs, unless this iterable is ordered
   */
  def dropRight(n: Int): CC[A] = {
    val b = newBuilder[A]
    val lead = elements drop n
    breakable {
      for (x <- this) {
        if (!lead.hasNext) break
        lead.next
        b += x
      }
    }
    b.result
  }

  /** Returns the longest prefix of this iterable whose elements satisfy
   *  the predicate <code>p</code>.
   *
   *  @param p the test predicate.
   *  @note  Might return different results for different runs, unless this iterable is ordered
   */
  def takeWhile(p: A => Boolean): CC[A] = {
    val b = newBuilder[A]
    breakable {
      for (x <- this) {
        if (!p(x)) break
        b += x
      }
    }
    b.result
  }

  /** Returns the longest suffix of this iterable whose first element
   *  does not satisfy the predicate <code>p</code>.
   *
   *  @param p the test predicate.
   *  @note  Might return different results for different runs, unless this iterable is ordered
   */
  def dropWhile(p: A => Boolean): CC[A] = {
    val b = newBuilder[A]
    var go = false
    for (x <- this) {
      if (go) b += x
      else if (!p(x)) { go = true; b += x }
    }
    b.result
  }

 /** Returns a pair consisting of the longest prefix of the iterable whose
   *  elements all satisfy the given predicate, and the rest of the iterable.
   *
   *  @param p the test predicate
   *  @return  a pair consisting of the longest prefix of the iterable whose
   *           elements all satisfy <code>p</code>, and the rest of the iterable.
   *  @note  Might return different results for different runs, unless this iterable is ordered
   */
  def span(p: A => Boolean): (CC[A], CC[A]) = {
    val l, r = newBuilder[A]
    var toLeft = true
    for (x <- this) {
      toLeft = toLeft && p(x)
      (if (toLeft) l else r) += x
    }
    (l.result, r.result)
  }

  /** Checks if the other iterable object contains the same elements as this one.
   *
   *  @note will not terminate for infinite-sized iterables.
   *  @param that  the other iterable
   *  @return true, iff both iterables contain the same elements.
   *  @note  Might return different results for different runs, unless this iterable is ordered
   */
  def sameElements[B >: A](that: OrderedIterable[B]): Boolean = {
    val these = this.elements
    val those = that.elements
    while (these.hasNext && those.hasNext && these.next() == those.next()) {}
    !these.hasNext && !those.hasNext
  }
}