@@ -5,85 +5,6 @@ import scala.reflect.ClassTag
5
5
import annotation .unchecked .uncheckedVariance
6
6
import annotation .tailrec
7
7
8
- /** A strawman architecture for new collections. It contains some
9
- * example collection classes and methods with the intent to expose
10
- * some key issues. It would be good to compare this to odether
11
- * implementations of the same functionality, to get an idea of the
12
- * strengths and weaknesses of different collection architectures.
13
- *
14
- * For a test file, see tests/run/CollectionTests.scala.
15
- *
16
- * Strawman6 is like strawman5, and adds lazy lists (i.e. lazie streams), arrays
17
- * and some utilitity methods (take, tail, mkString, toArray). Also, systematically
18
- * uses builders for all strict collections.
19
- *
20
- * Types covered in this strawman:
21
- *
22
- * 1. Collection base types:
23
- *
24
- * IterableOnce, Iterable, Seq, LinearSeq, View, IndexedView
25
- *
26
- * 2. Collection creator base types:
27
- *
28
- * FromIterable, IterableFactory, Buildable, Builder
29
- *
30
- * 3. Types that bundle operations:
31
- *
32
- * IterableOps, IterableMonoTransforms, IterablePolyTransforms, IterableLike
33
- * SeqMonoTransforms, SeqLike
34
- *
35
- * 4. Concrete collection types:
36
- *
37
- * List, LazyList, ListBuffer, ArrayBuffer, ArrayBufferView, StringView, ArrayView
38
- *
39
- * 5. Decorators for existing types
40
- *
41
- * StringOps, ArrayOps
42
- *
43
- * 6. Related non collection types:
44
- *
45
- * Iterator, StringBuilder
46
- *
47
- * Operations covered in this strawman:
48
- *
49
- * 1. Abstract operations, or expected to be overridden:
50
- *
51
- * For iterables:
52
- *
53
- * iterator, fromIterable, fromIterableWithSameElemType, knownLength, className
54
- *
55
- * For sequences:
56
- *
57
- * apply, length
58
- *
59
- * For buildables:
60
- *
61
- * newBuilder
62
- *
63
- * For builders:
64
- *
65
- * +=, result
66
- *
67
- * 2. Utility methods, might be overridden for performance:
68
- *
69
- * Operations returning not necessarily a collection:
70
- *
71
- * foreach, foldLeft, foldRight, indexWhere, isEmpty, head, size, mkString
72
- *
73
- * Operations returning a collection of a fixed type constructor:
74
- *
75
- * view, to, toArray, copyToArray
76
- *
77
- * Type-preserving generic transforms:
78
- *
79
- * filter, partition, take, drop, tail, reverse
80
- *
81
- * Generic transforms returning collections of different element types:
82
- *
83
- * map, flatMap, ++, zip
84
- */
85
-
86
-
87
8
/* ------------ Base Traits -------------------------------- */
88
9
89
10
/** Iterator can be used only once */
@@ -676,165 +597,6 @@ case class ArrayView[A](xs: Array[A]) extends IndexedView[A] {
676
597
override def className = " ArrayView"
677
598
}
678
599
679
-
680
- /* ---------- Views -------------------------------------------------------*/
681
-
682
- /** Concrete collection type: View */
683
- trait View [+ A ] extends Iterable [A ] with IterableLike [A , View ] {
684
- override def view = this
685
-
686
- /** Avoid copying if source collection is already a view. */
687
- override def fromIterable [B ](c : Iterable [B ]): View [B ] = c match {
688
- case c : View [B ] => c
689
- case _ => View .fromIterator(c.iterator)
690
- }
691
- override def className = " View"
692
- }
693
-
694
- /** This object reifies operations on views as case classes */
695
- object View {
696
- def fromIterator [A ](it : => Iterator [A ]): View [A ] = new View [A ] {
697
- def iterator = it
698
- }
699
-
700
- /** The empty view */
701
- case object Empty extends View [Nothing ] {
702
- def iterator = Iterator .empty
703
- override def knownSize = 0
704
- }
705
-
706
- /** A view with given elements */
707
- case class Elems [A ](xs : A * ) extends View [A ] {
708
- def iterator = Iterator (xs : _* )
709
- override def knownSize = xs.length // should be: xs.knownSize, but A*'s are not sequences in this strawman.
710
- }
711
-
712
- /** A view that filters an underlying collection. */
713
- case class Filter [A ](val underlying : Iterable [A ], p : A => Boolean ) extends View [A ] {
714
- def iterator = underlying.iterator.filter(p)
715
- }
716
-
717
- /** A view that partitions an underlying collection into two views */
718
- case class Partition [A ](val underlying : Iterable [A ], p : A => Boolean ) {
719
-
720
- /** The view consisting of all elements of the underlying collection
721
- * that satisfy `p`.
722
- */
723
- val left = Partitioned (this , true )
724
-
725
- /** The view consisting of all elements of the underlying collection
726
- * that do not satisfy `p`.
727
- */
728
- val right = Partitioned (this , false )
729
- }
730
-
731
- /** A view representing one half of a partition. */
732
- case class Partitioned [A ](partition : Partition [A ], cond : Boolean ) extends View [A ] {
733
- def iterator = partition.underlying.iterator.filter(x => partition.p(x) == cond)
734
- }
735
-
736
- /** A view that drops leading elements of the underlying collection. */
737
- case class Drop [A ](underlying : Iterable [A ], n : Int ) extends View [A ] {
738
- def iterator = underlying.iterator.drop(n)
739
- protected val normN = n max 0
740
- override def knownSize =
741
- if (underlying.knownSize >= 0 ) (underlying.knownSize - normN) max 0 else - 1
742
- }
743
-
744
- /** A view that takes leading elements of the underlying collection. */
745
- case class Take [A ](underlying : Iterable [A ], n : Int ) extends View [A ] {
746
- def iterator = underlying.iterator.take(n)
747
- protected val normN = n max 0
748
- override def knownSize =
749
- if (underlying.knownSize >= 0 ) underlying.knownSize min normN else - 1
750
- }
751
-
752
- /** A view that maps elements of the underlying collection. */
753
- case class Map [A , B ](underlying : Iterable [A ], f : A => B ) extends View [B ] {
754
- def iterator = underlying.iterator.map(f)
755
- override def knownSize = underlying.knownSize
756
- }
757
-
758
- /** A view that flatmaps elements of the underlying collection. */
759
- case class FlatMap [A , B ](underlying : Iterable [A ], f : A => IterableOnce [B ]) extends View [B ] {
760
- def iterator = underlying.iterator.flatMap(f)
761
- }
762
-
763
- /** A view that concatenates elements of the underlying collection with the elements
764
- * of another collection or iterator.
765
- */
766
- case class Concat [A ](underlying : Iterable [A ], other : IterableOnce [A ]) extends View [A ] {
767
- def iterator = underlying.iterator ++ other
768
- override def knownSize = other match {
769
- case other : Iterable [_] if underlying.knownSize >= 0 && other.knownSize >= 0 =>
770
- underlying.knownSize + other.knownSize
771
- case _ =>
772
- - 1
773
- }
774
- }
775
-
776
- /** A view that zips elements of the underlying collection with the elements
777
- * of another collection or iterator.
778
- */
779
- case class Zip [A , B ](underlying : Iterable [A ], other : IterableOnce [B ]) extends View [(A , B )] {
780
- def iterator = underlying.iterator.zip(other)
781
- override def knownSize = other match {
782
- case other : Iterable [_] if underlying.knownSize >= 0 && other.knownSize >= 0 =>
783
- underlying.knownSize min other.knownSize
784
- case _ =>
785
- - 1
786
- }
787
- }
788
- }
789
-
790
- /** View defined in terms of indexing a range */
791
- trait IndexedView [+ A ] extends View [A ] with ArrayLike [A ] { self =>
792
-
793
- def iterator : Iterator [A ] = new Iterator [A ] {
794
- private var current = 0
795
- def hasNext = current < self.length
796
- def next : A = {
797
- val r = apply(current)
798
- current += 1
799
- r
800
- }
801
- }
802
-
803
- override def take (n : Int ): IndexedView [A ] = new IndexedView .Take (this , n)
804
- override def drop (n : Int ): IndexedView [A ] = new IndexedView .Drop (this , n)
805
- override def map [B ](f : A => B ): IndexedView [B ] = new IndexedView .Map (this , f)
806
- def reverse : IndexedView [A ] = new IndexedView .Reverse (this )
807
- }
808
-
809
- object IndexedView {
810
-
811
- class Take [A ](underlying : IndexedView [A ], n : Int )
812
- extends View .Take (underlying, n) with IndexedView [A ] {
813
- override def iterator = super .iterator // needed to avoid "conflicting overrides" error
814
- def length = underlying.length min normN
815
- def apply (i : Int ) = underlying.apply(i)
816
- }
817
-
818
- class Drop [A ](underlying : IndexedView [A ], n : Int )
819
- extends View .Take (underlying, n) with IndexedView [A ] {
820
- override def iterator = super .iterator
821
- def length = (underlying.length - normN) max 0
822
- def apply (i : Int ) = underlying.apply(i + normN)
823
- }
824
-
825
- class Map [A , B ](underlying : IndexedView [A ], f : A => B )
826
- extends View .Map (underlying, f) with IndexedView [B ] {
827
- override def iterator = super .iterator
828
- def length = underlying.length
829
- def apply (n : Int ) = f(underlying.apply(n))
830
- }
831
-
832
- case class Reverse [A ](underlying : IndexedView [A ]) extends IndexedView [A ] {
833
- def length = underlying.length
834
- def apply (i : Int ) = underlying.apply(length - 1 - i)
835
- }
836
- }
837
-
838
600
/* ---------- Iterators ---------------------------------------------------*/
839
601
840
602
/** A core Iterator class */
0 commit comments