Merge: lib/string: some cleaning, doc and unit tests
authorJean Privat <jean@pryen.org>
Fri, 4 Apr 2014 20:26:42 +0000 (16:26 -0400)
committerJean Privat <jean@pryen.org>
Fri, 4 Apr 2014 20:26:42 +0000 (16:26 -0400)
An important point is the generalization of services from
StringCharView to Sequences, and the privatization of StringCharView.

Closes #144
Closes #140
Pull-Request: #383
Reviewed-by: Lucas Bajolet <r4pass@hotmail.com>
Reviewed-by: Alexis Laferrière <alexis.laf@xymus.net>

1  2 
lib/standard/collection/abstract_collection.nit
lib/standard/collection/array.nit

@@@ -149,9 -149,6 +149,9 @@@ interface Iterator[E
  
        # Is there a current item ?
        fun is_ok: Bool is abstract
 +
 +      # Iterate over `self`
 +      fun iterator: Iterator[E] do return self
  end
  
  # A collection that contains only one item.
@@@ -308,25 -305,6 +308,25 @@@ interface Set[E: Object
                for e in self do res += res.hash
                return res
        end
 +
 +      # Returns the union of this set with the `other` set
 +      fun union(other: Set[E]): Set[E]
 +      do
 +              var nhs = new_set
 +              nhs.add_all self
 +              nhs.add_all other
 +              return nhs
 +      end
 +
 +      # Returns the intersection of this set with the `other` set
 +      fun intersection(other: Set[E]): Set[E]
 +      do
 +              var nhs = new_set
 +              for v in self do if other.has(v) then nhs.add(v)
 +              return nhs
 +      end
 +
 +      protected fun new_set: Set[E] is abstract
  end
  
  # MapRead are abstract associative collections: `key` -> `item`.
@@@ -529,22 -507,22 +529,22 @@@ en
  class MapKeysIterator[K: Object, V]
        super Iterator[K]
        # The original iterator
 -      var iterator: MapIterator[K, V]
 +      var original_iterator: MapIterator[K, V]
  
 -      redef fun is_ok do return self.iterator.is_ok
 -      redef fun next do self.iterator.next
 -      redef fun item do return self.iterator.key
 +      redef fun is_ok do return self.original_iterator.is_ok
 +      redef fun next do self.original_iterator.next
 +      redef fun item do return self.original_iterator.key
  end
  
  # Iterator on a 'values' point of view of a map
  class MapValuesIterator[K: Object, V]
        super Iterator[V]
        # The original iterator
 -      var iterator: MapIterator[K, V]
 +      var original_iterator: MapIterator[K, V]
  
 -      redef fun is_ok do return self.iterator.is_ok
 -      redef fun next do self.iterator.next
 -      redef fun item do return self.iterator.item
 +      redef fun is_ok do return self.original_iterator.is_ok
 +      redef fun next do self.original_iterator.next
 +      redef fun item do return self.original_iterator.item
  end
  
  # Sequences are indexed collections.
@@@ -653,8 -631,6 +653,6 @@@ interface SequenceRead[E
                return res
        end
  
-       redef fun iterator: IndexedIterator[E] is abstract
        # Two sequences are equals if they have the same items in the same order.
        #
        #     var a = new List[Int]
                for e in self do res += res.hash
                return res
        end
+       redef fun iterator: IndexedIterator[E] is abstract
+       # Gets a new Iterator starting at position `pos`
+       #
+       #     var iter = [10,20,30,40,50].iterator_from(2)
+       #     assert iter.to_a == [30, 40, 50]
+       fun iterator_from(pos: Int): IndexedIterator[E]
+       do
+               var res = iterator
+               while pos > 0 and res.is_ok do
+                       res.next
+                       pos -= 1
+               end
+               return res
+       end
+       # Gets an iterator starting at the end and going backwards
+       #
+       #     var reviter = [1,2,3].reverse_iterator
+       #     assert reviter.to_a == [3,2,1]
+       fun reverse_iterator: IndexedIterator[E] is abstract
+       # Gets an iterator on the chars of self starting from `pos`
+       #
+       #     var reviter = [10,20,30,40,50].reverse_iterator_from(2)
+       #     assert reviter.to_a == [30,20,10]
+       fun reverse_iterator_from(pos: Int): IndexedIterator[E]
+       do
+               var res = reverse_iterator
+               while pos > 0 and res.is_ok do
+                       res.next
+                       pos -= 1
+               end
+               return res
+       end
  end
  
  # Sequence are indexed collection.
@@@ -131,6 -131,7 +131,7 @@@ abstract class AbstractArrayRead[E
        end
  
        redef fun iterator: ArrayIterator[E] do return new ArrayIterator[E](self)
+       redef fun reverse_iterator do return new ArrayReverseIterator[E](self)
  end
  
  # Resizable one dimension array of objects.
@@@ -364,6 -365,20 +365,20 @@@ private class ArrayIterator[E
        var _array: AbstractArrayRead[E]
  end
  
+ private class ArrayReverseIterator[E]
+       super ArrayIterator[E]
+       redef fun is_ok do return _index >= 0
+       redef fun next do _index -= 1
+       init(a: AbstractArrayRead[E])
+       do
+               _array = a
+               _index = a.length - 1
+       end
+ end
  # Others collections ##########################################################
  
  # A set implemented with an Array.
@@@ -413,8 -428,6 +428,8 @@@ class ArraySet[E: Object
  
        # Create an empty set with a given capacity.
        init with_capacity(i: Int) do _array = new Array[E].with_capacity(i)
 +
 +      redef fun new_set do return new ArraySet[E]
  end
  
  # Iterators on sets implemented with arrays.