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

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Linked Lists Linear collections.
Concrete collections in Java library
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Computer Science 112 Fundamentals of Programming II Overview of Collections.
Stéphane Ducasse 1 Design Points - Subclassing vs Subtyping Stéphane Ducasse
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
Principles of Object-Oriented Software Development The language Smalltalk.
© Oscar Nierstrasz ST — Introduction ST 1.1 A Word about Primitives  For optimization, if a primitive fails, the code following is executed. 
Data Structures: Lists i206 Fall 2010 John Chuang Some slides adapted from Glenn Brookshear, Brian Hayes, or Marti Hearst.
13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development.
1 L41 Collections (1). 2 OBJECTIVES  What collections are.  To use class Arrays for array manipulations.  To use the collections framework (prepackaged.
PZ06BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ06BX - Introduction to Smalltalk Programming Language.
Data Structures Using Java1 Chapter 8 Search Algorithms.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 13 Object-Oriented Programming I am surprised.
Elementary Data Types Scalar Data Types Numerical Data Types Other
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
Structured Data Types and Encapsulation Mechanisms to create new data types: –Structured data Homogeneous: arrays, lists, sets, Non-homogeneous: records.
03G-1 Everything is an Object Examining builtin classes All these are classes in Little Smalltalk:  Object  Class  Method  Block  Boolean True False.
Squeak Collections The Squeak collection hierarchy. Some collection operators. Working with collections. For-loops.
Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses.
INTRODUCTION TO DATA STRUCTURE. Topics To Be Discussed………………………. Meaning of Data Structure Classification of Data Structure Data Structure Operations.
3. Standard Classes. © Oscar Nierstrasz ST — Standard Classes 3.2 Roadmap  Object  Numbers, Characters, Strings and Arrays  Variables  Blocks and.
The Collections Framework A Brief Introduction. Collections A collection is a structured group of objects –An array is a kind of collection –A Vector.
3. Standard Classes. Birds-eye view © Oscar Nierstrasz ST — Introduction 1.2 Reify everything — by reifying its entire implementation model, Smalltalk.
Stéphane Ducasse 1 Smalltalk in a Nutshell.
Stéphane Ducasse«ChapterNr».1 Basic Objects, Conditionals and Loops Booleans Basic Loops Overview of the Collection hierarchy— more than 80 classes: (Bag,
Data Structures Using Java1 Chapter 8 Search Algorithms.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Stacks and Queues Introduction to Computing Science and Programming I.
Beyond Lists: Other Data Structures CS303E: Elements of Computers and Programming.
111 © 2002, Cisco Systems, Inc. All rights reserved.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
File I/O ifstreams and ofstreams Sections 11.1 &
Centralized vs. Decentralized Interpreter Pattern Visitor Pattern.
Smalltalk (and Squeak) Aida Dungan and Rick Shreve.
13-1 Sets, Bags, and Tables Exam 1 due Friday, March 16 Wellesley College CS230 Lecture 13 Thursday, March 15 Handout #23.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
Object-oriented programming and design 1 Object Identity = vs. == copying objects Value Object ValueWithHistory.
Understanding Data Types and Collections Lesson 2.
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
Chapter 3 Collections. Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections.
CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections UTPA – Fall 2011.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
CS1020E Lab 4 (Stack and Queue)
3C-1 Identifiers Variables Literal Objects  Numbers  Characters  Strings Control Structure  Blocks  Conditionals  Loops Messages Defining New Classes.
1 Advanced Object Oriented Systems (CM0318) Lecture 8 (Last updated 15th February 2002)
3. Standard Classes. © Oscar Nierstrasz ST — Standard Classes 3.2 Roadmap  Object  Numbers, Characters, Strings and Arrays  Variables  Blocks and.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
Stéphane Ducasse 1 A Little Journey in the Smalltalk Syntax.
University of Limerick1 Collections The Collection Framework.
Python Data Structures By Greg Felber. Lists An ordered group of items Does not need to be the same type – Could put numbers, strings or donkeys in the.
1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.
Patterns Protect from Change Rule: if something is going to change, make it an object. Strategy: make algorithm an object so it can change State: make.
Fundamentals of Programming II Overview of Collections
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
Sequences and Iterators
The Class Concept Abstraction What is a class? Two parts of the class
Feedback from Assignment 1
Smalltalk – a Pure (and first) OOPL
Copyright 2000, Georgia Tech
CS 331 Principles of Programming Languages
Introduction to Smalltalk
Introduction to Smalltalk
Smalltalk on a Dime.
Presentation transcript:

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.

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.

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.

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

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

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:

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')

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()

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

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}

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. #( ) select: [ :i | ( i rem: 2 ) = 0 ] #( 2 4 ) ' ' select: [ :c | c > $5 ] '6789' The method reject: returns the complementary collection. #( ) asSet reject: [ :i | ( i rem: 2 ) = 0 ] a Set ( )

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]]. #( ) do: B 1 is odd! 2 is even! 3 is odd! 4 is even! 5 is odd!

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. #( ) collect: [ :i | i factorial ] #( ) #( ) collect: [ :j | j rem: 2 ] #( ) D := Dictionary new. D at:0 put:'even'; at:1 put:'odd‘. #( ) collect: [ :x | D at: ( x rem: 2 ) ] #( 'odd' 'even' 'odd' 'even' 'odd' ) factor := 1.1. grades := #( ) collect: [ :g | g * factor ] #( )

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 := #( ). ( 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 ”

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 ] ]