Presentation is loading. Please wait.

Presentation is loading. Please wait.

3E-1 Collections in Squeak What are they? Kinds of collections. Basic Operations. Usage of Inheritance in the Collections Library. Roman numbers example.

Similar presentations


Presentation on theme: "3E-1 Collections in Squeak What are they? Kinds of collections. Basic Operations. Usage of Inheritance in the Collections Library. Roman numbers example."— Presentation transcript:

1 3E-1 Collections in Squeak What are they? Kinds of collections. Basic Operations. Usage of Inheritance in the Collections Library. Roman numbers example. The Stack Example:  Defining a new kind of collection.

2 3E-2 What are Collections? Collections provide the means for managing and manipulating groups of objects. Some kinds of collections:  Set: represents an unordered group of objects. Elements are added and removed by value.  Bag: similar to set, but allows repetitions  Dictionary: is also an unordered collection of elements, but insertions and removals require an explicit key.  Interval: represents a sequence of numbers in arithmetic progression, either ascending or descending.  List: is a group of objects having a specific linear ordering. Insertions and removals are done in the extremes.  Array: a fixed-size collections. Elements can not be inserted or removed, but they may be overwritten.  String: can be considered to be a special form of Array, where the elements must be characters. Collections can be converted into a different kind by the use of messages like asSet, asArray, etc.

3 3E-3 Classification of Collections The different kinds of Collections may be classified according to several attributes. Size  Fixed / Unbounded Ordering  Ordered / Unordered Access Method  By value / Indexed /Sequential/ key-based Mutability  Mutable / Immutalbe Duplications policy  Accept / filter out Heterogeneous  Any kind of objects / one kind of objects  Choose the right Collection by examining its attributes.

4 3E-4 4 Key Collection Classes in Squeak. Set SequenceableCollection* OrderedCollection ArrayedCollection* Object* Collection* Interval String Dictionary Array This is only a partial view Bag

5 3E-5 Some Collection Methods Are defined, redefined, optimized or forbidden (!) in subclasses Accessing size, capacity, at: anIndex, at: anIndex put: anElement Testing isEmpty, includes: anElement, contains: aBlock, occurrencesOf: anElement Adding add: anElement, addAll: aCollection Removing remove: anElement, remove: anElement ifAbsent: aBlock, removeAll: aCollection Enumerating do: aBlock, collect: aBlock, select: aBlock, reject: aBlock, detect: aBlock, detect: aBlock ifNone: aNoneBlock, inject: aValue into: aBinaryBlock Converting asBag, asSet, asOrderedCollection, asSortedCollection, asArray, asSortedCollection: aBlock Creation with: anElement, with:with:, with:with:with:, with:with:with:with:, withAll: aCollection

6 3E-6 Collections ’ Attributes NameCreationFixed Order? InsertionAccessRemoval MethodSize? MethodMethodMethod Setnew no no add: includes: remove: with: Dictionarynewnono at:put: at: removeKey: Interval n to: m yes yes none none none Ordered- new no yes addFirst: first removeFirst Collection addLast: remove: Arraynew: yes yes at:put:at: none with: String new: yes yes at:put: at: none Bagnewnonoadd:includes: with:

7 3E-7 Inserting an Element Indexed collections ( Dictionary, Array ) require an explicit key and a value, by using the method at:put: D := Dictionary new. D at:'father' put:'aba'; at:'mother' put:'ima'; at:'aunt' put:'doda' a Dictionary('aunt'->'doda' 'father'->'aba' 'mother'->'ima' ) Non-indexed collections require only a value, by using the method add: S := Set new. S add:'red'; add:'green'; add:'blue‘ a Set('green' 'blue‘' 'red') In OrderedCollection, values can be added in the beginning or end, using addFirst: and addLast: L := OrderedCollection new. L addLast: 'acharon'; addFirst: 'rishon' an OrderedCollection('rishon' 'acharon')

8 3E-8 Removing an Element In indexed collections the removal method requires the key. D removeKey: 'aunt' a Dictionary('father'->'aba‘ 'mother'->'ima' ) In collections with fixed size ( Array and String ) elements can not be removed. In non-indexed collections the argument is the object to be removed. S remove: 'green' a Set ( 'blue' 'red' ) In an OrderedCollection, an element can be removed from the beginning ( removeFirst ) or by value ( remove: ). L removeFirst remove: 'acharon' an OrderedCollection()

9 3E-9 Accessing an Element In indexed collections the elements are accessed by key. 'SmallTalk' at: 6 $T The method keys returns the keys of an indexed collection. D keys a Set ( 'father' 'mother' ) In non-indexed collections we already have the value, hence the only question is whether it is in the collection. S includes: 'black' false The method includes: is defined for all collections. D keys includes: ‘father’ true

10 3E-10Converting  Send asSet, asBag, asSortedCollection etc. to convert between kinds of collections  Send keys, values to extract collections from dictionaries u Use various factory methods to build new kinds of collections from old lDictionary newFrom: {1->#a. 2->#b. 3->#c}

11 3E-11 Selecting Elements The method select: returns a collection containing all the elements that satisfy some condition. It receives a one-argument block that is evaluated for each element in the collection, returning true or false. The returned collection is of the same class as the receiver in case it is Set, List, and Array, and Array otherwise. #( 1 2 3 4 5 ) select: [ :i | ( i rem: 2 ) = 0 ] #( 2 4 ) '1234567890' select: [ :c | c > $5 ] '6789' The method reject: returns the complementary collection. #( 1 2 3 4 5 ) asSet reject: [ :i | ( i rem: 2 ) = 0 ] a Set ( 1 3 5 )

12 3E-12 Performing Computations The method do: allows a computation to be performed on every element in a collection. It also receives a one-argument block. B := [ :x | ( x rem: 2 ) = 0 ifTrue: [Transcript show: (x asString, ' is even!');cr] ifFalse:[Transcript show: (x asString, ' is odd!');cr]]. #(1 2 3 4 5) do: B 1 is odd! 2 is even! 3 is odd! 4 is even! 5 is odd!

13 3E-13 Collecting Results The method collect: is similar to do:, but it produces a new collection containing the results of the block evaluation for each element of the receiver collection. #( 1 2 3 4 5 ) collect: [ :i | i factorial ] #( 1 2 6 24 120 ) #( 1 2 3 4 5 ) collect: [ :j | j rem: 2 ] #( 1 0 1 0 1 ) D := Dictionary new. D at:0 put:'even'; at:1 put:'odd‘. #( 1 2 3 4 5 ) collect: [ :x | D at: ( x rem: 2 ) ] #( 'odd' 'even' 'odd' 'even' 'odd' ) factor := 1.1. grades := #(70 55 60 42) collect: [ :g | g * factor ] #(77.0 60.5 66.0 46.2)

14 3E-14 Accumulative Processing The method inject:into: is useful for processing all values of a collection and returning a single result. The first argument is the initial value, and the second is a two-parameter block that performs some computation. At each iteration the block receives the result of the previous computation and the next value in the collection. A := #(1 2 3 4 5). ( A inject:0 into: [:a :b| a + b ] ) / A size. 3 “ average of the values in the array ” A inject:0 into: [:x :y| x > y ifTrue:[x] ifFalse:[y]] 5 “ maximum value in the array ” A inject:0 into: [:i :j| ( j rem: 2 ) = 0 ifTrue: [ i + 1 ] ifFalse: [ i ] ] 2 “ number of even values in the array ”

15 3E-15 Implementation Examples Taken from Little Smalltalk, could be applied in Squeak Collection inject:into: inject: aValue into: aBlock | last | last <- aValue. self do: [:x | last <- aBlock value:last value:x ]. ^last Collection size size ^self inject: 0 into: [ :x :y | x + 1 ] Collection occurrencesOf: occurrencesOf: anObject ^self inject: 0 into: [ :x :y | ( y = anObject ) ifTrue: [ x + 1 ] ifFalse: [ x ] ]


Download ppt "3E-1 Collections in Squeak What are they? Kinds of collections. Basic Operations. Usage of Inheritance in the Collections Library. Roman numbers example."

Similar presentations


Ads by Google