Chapter 7 Sets The problem w e will consider in this c - - PDF document

chapter 7 sets the problem w e will consider in this c
SMART_READER_LITE
LIVE PREVIEW

Chapter 7 Sets The problem w e will consider in this c - - PDF document

Chapter 7 Sets The problem w e will consider in this c hapter is the implemen tation of a data structure that corresp onds roughly to the notion of a in mathematics. Our purp ose is not to explain set the


slide-1
SLIDE 1 Chapter 7 Sets The problem w e will consider in this c hapter is the implemen tation
  • f
a data structure that corresp
  • nds
roughly to the notion
  • f
a set in mathematics. Our purp
  • se
is not to explain the concept
  • f
a set (w e assume the reader is already familiar with the idea) but to further explore some
  • f
the tec hniques that can b e used in the represen tation
  • f
common data structures. F
  • r
this c hapter w e will dene a set to b e an unordered collection
  • f
  • b
jects
  • f
the same t yp e, with no v alue app earing more than
  • nce
in the set. In
  • rder
to simplify
  • ur
presen ta- tion, w e will consider
  • nly
three
  • p
erations
  • n
sets. The action
  • f
addition incorp
  • rates
a new elemen t in to the set, while inclusion is a test used to determine if an elemen t is already presen t in the set. Finally , a union
  • f
  • ne
set with a second is formed b y adding all the elemen ts from the second set that do not app ear already in the rst. The exercises at the end
  • f
the c hapter explore the implemen tation
  • f
v arious
  • ther
set
  • p
erations. Unlik e the c hapter
  • n
Lists (Chapter 4), whic h dev elop ed a single implemen tation
  • f
a data structure that made use
  • f
facilities from m ultiple dieren t paradigms, in this c hapter w e will consider four dieren t implemen tation approac hes, eac h more-or-less pure in their
  • wn
paradigm. Tw
  • f
the approac hes will mak e use
  • f
  • b
ject-orien ted programming, and t w
  • f
the approac hes will use functional tec hniques. The presen tation
  • f
dieren t imple- men tations p ermits the reader to explore the range
  • f
p
  • ten
tial solution tec hniques, and to more clearly see some
  • f
the adv an tages and disadv an tages
  • f
the dieren t metho ds. 7.1 Ob ject-Orien ted Sets Ob ject-orien ted programming is in large part a paradigm that emphasizes the reuse
  • f
existing soft w are abstractions in the dev elopmen t
  • f
new soft w are comp
  • nen
ts. W e sa w this in the previous c hapter, where w e constructed in an
  • b
ject-orien ted fashion the Table data t yp e
  • n
top
  • f
the List abstraction from Chapter 4. In the rst part
  • f
this c hapter w e 133
slide-2
SLIDE 2 134 CHAPTER 7. SETS will
  • nce
again illustrate ho w this is accomplished b y building the
  • b
ject-orien ted v ersions
  • f
  • ur
set data abstraction b y making use
  • f
the List data structure. There are t w
  • primary
mec hanisms a v ailable for soft w are reuse using
  • b
ject-orien ted programming, and b
  • th
are applicable in the dev elopmen t
  • f
a set data abstraction. In the rst
  • f
the follo wing t w
  • sections
w e illustrate soft w are reuse using the idea
  • f
c
  • mp
  • sition,
while in the second section w e dev elop an en tirely dieren t approac h using the tec hnique
  • f
inheritanc e. 7.1.1 Implemen ting Sets using Comp
  • sition
Y
  • u
will recall that an
  • b
ject is simply an encapsulation
  • f
state (data v alues) and b eha vior. When using comp
  • sition
to reuse an existing data abstraction in the dev elopmen t
  • f
a new data t yp e, a p
  • rtion
  • f
the state
  • f
the new data structure is simply an instance
  • f
the existing structure. This is illustrated in Figure 7.1, where the data t yp e Set con tains a eld
  • f
t yp e List. In a similar fashion, in Chapter 6 w e constructed the Table data structure whic h held v alues in a eld
  • f
t yp e List. Op erations used to manipulate the new structure are implemen ted b y making use
  • f
the existing
  • p
erations pro vided for the earlier data t yp e. F
  • r
example, the implemen tation
  • f
the includes
  • p
eration for
  • ur
set data structure simply in v
  • k
es the similarly-named function already dened for lists. Notice that b
  • th
the includes test and the addition function c hec k to mak e sure the list data eld has b een initialized b efore they p erform their
  • p
eration, and initialize the data eld to hold a newly created empt y list if needed. The addition
  • f
a new elemen t to a set rst c hec ks to see if the v alue is already con tained in the collection. If it is already in the list it is not added, since
  • ur
denition
  • f
a set stipulates that eac h elemen t app ears
  • nly
  • nce
in the collection. Only if the elemen t is not already in the set is it added to the list. The
  • p
eration
  • f
union is made dicult b y the necessit y
  • f
lo
  • ping
  • v
er the elemen ts in the second set. Since this is will undoubtedly also b e an
  • p
eration users
  • f
the set data abstraction will wish to p erform, w e mak e a v ailable the function named items that w as used with the list abstraction to p erform iteration. The items function simply in v
  • k
es the functions with the same name in the list class. Figure 7.2 illustrates the use
  • f
  • ur
set data abstraction. An arra y literal is dened to hold the names
  • f
dieren t t yp es
  • f
animals. Tw
  • sets
are dened,
  • ne
con taining animals that bark and
  • ne
con taining animals that are found in a zo
  • .
A lo
  • p
then prin ts
  • ut
information concerning eac h t yp e
  • f
animal. The
  • utput
  • f
the program w
  • uld
b e as follo ws: a cat is a non-zoo animal that does not bark a dog is a non-zoo animal that barks a seal is a zoo animal that barks a lion is a zoo animal that does not bark a horse is a non-zoo animal that does not bark
slide-3
SLIDE 3 7.1. OBJECT-ORIENTED SETS 135 class Set [T : equality]; var values : List[T]; f hold data v alues in a list g function add (newValue : T); begin f add new v alue to set, b y adding to list g if
  • defined(values)
then values := List[T](); if
  • values.includes
(ne wV alu e) then values.add(newVa lue ); end; function includes (value : T)->boolean; begin f see if v alue is held b y set g return defined(values) & values.includes(v al ue) ; end; function items(byRef val : T)->relation; begin f iterate
  • v
er v alues from set g return defined(values) & values.items(val) ; end; function unionWith(secon dSe t : Set[T]); var f add all elemen ts from second set g val : T; begin for secondSet.items( val ) do add(val); end; end; Figure 7.1: Sets Constructed using Comp
  • sition
slide-4
SLIDE 4 136 CHAPTER 7. SETS const animals := ["cat", "dog", "seal", "lion", "horse"]; var barkingAnimals : set[string]; zooAnimals : set[string]; name : string; begin barkingAnimals := set[string](); f mak e an empt y set g zooAnimals := set[string](); barkingAnimals.ad d( "do g" ); barkingAnimals.ad d( "se al "); zooAnimals.add("s ea l") ; zooAnimals.add("l io n") ; zooAnimals.add("l io n") ; f second add has no eect g for animals.items(n ame ) do begin print("a "); print(name); if zooAnimals.includ es( na me) then print(" is a zoo animal") else print(" is a non-zoo animal"); if barkingAnimals.in clu de s(n am e) then print(" that barks\n") else print(" that does not bark\n"); end; end; Figure 7.2: An Example Program using Ob ject-Orien ted Sets
slide-5
SLIDE 5 7.1. OBJECT-ORIENTED SETS 137 class Set [T : equality]
  • f
List[T]; function add (newValue : T); begin f add
  • nly
if not already in list g if
  • includes(newVal
ue) then List[T].add(self , newValue); end; function unionWith(secon dSe t : Set[T]); var val : T; begin f add all elemen ts from second set g for secondSet.items( val ) do add(val); end; end; Figure 7.3: A Set dened using Inheritance 7.1.2 Using Inheritance An en tirely dieren t mec hanism for soft w are reuse in
  • b
ject-orien ted programming is the concept
  • f
inheritance. Using inheritance, a new class can b e declared to b e a sub class,
  • r
child class
  • f
an existing class. By doing so, all data areas and functions asso ciated with the
  • riginal
class are automatically asso ciated with the new data abstraction. The new class can in addition dene new data v alues
  • r
new functions. The new class can also
  • verride
functions in the
  • riginal
class, b y simply dening new functions with the same name. These p
  • ssibilities
are illustrated in Figure 7.3, whic h implemen ts a dieren t v ersion
  • f
the set data abstraction. By naming the class List follo wing the k eyw
  • rd
  • f
w e indicate that the new abstraction is an extension,
  • r
a renemen t,
  • f
the earlier class. This means that all
  • p
erations asso ciated with lists (Figure 4.1, page 63) are immediately applicable to sets as w ell. Notice that the class do es not dene an y new data elds. Instead, the data elds dened in the List class will b e used to main tain the set elemen ts. Similarly , functions dened in the paren t class can b e used without an y further eort. In particular, this means that no w
  • rk
is required to implemen t the inclusion test, since the list data structure already pro vides a function named includes that p erforms exactly the task w e desire. The addition
  • f
an elemen t to a set,
  • n
the
  • ther
hand, has a sligh tly dieren t meaning than the addition
  • f
a v alue to a list. Th us, the implemen tation
  • f
the addition function m ust sup ersede,
  • r
  • v
erride, the function with the same name found in the list class. Con trast the denition
  • f
the add function sho wn in Figure 7.3 with the similarly named function dened for lists (Figure 4.2, page 68). The function for sets rst c hec ks to see if the elemen t is in the collection,
  • nly
adding the new elemen t if it is not presen t in the set. If it is not
slide-6
SLIDE 6 138 CHAPTER 7. SETS y et presen t in the list, then w e w an t to in v
  • k
e the add function from the class List. 1 Simply sa ying self.add(...) w
  • uld
not w
  • rk,
since the compiler w
  • uld
in terpret that construct as a recursiv e call
  • n
the
  • v
erridden add function, and the result w
  • uld
b e an innite execution lo
  • p.
The solution to this problem in v
  • lv
es a more general mec hanism in the Leda language. When a class name is used in an expression that references a function dened within the class, as in List[T].add a transformation is applied to generate a new function
  • ut
  • f
the named function. This new function tak es
  • ne
more argumen t than the
  • riginal.
Th us, the add function, whic h in the class List required
  • nly
a single argumen t, is con v erted in to a new function that requires t w
  • argumen
ts. The additional argumen t is used to sp ecify whic h list is b eing manipulated. In eect, it is as if the expression giv en ab
  • v
e had b een translated in to the follo wing function: function [T : equality] (aList : List[T], newValue : T); begin aList.add(newVa lue ); end This transformation is applied in the add function, whic h con v erts the
  • ne-argumen
t function from the paren t class in to a t w
  • -argumen
t function, whic h is then immediately in v
  • k
ed. The tec hnique, ho w ev er, can in general b e applied to an y function dened within a class. A subtle p
  • in
t to note in relation to this transformation is the fact that the rst argumen t to the function b eing generated is declared as a List, but in fact is passed a v alue
  • f
class Set (namely , the v alue referred to b y the pseudo-v ariable self from within the Set class). This is p ermitted, since the class Set inherits from class List, and th us can in all reasonable situations b e used as a substitute for class List. W e will explore this issue in more detail later in Chapter 10. The metho d for p erforming union in the Set class formed using inheritance uses exactly the same tec hnique as the function written for the class using comp
  • sition.
Note, ho w ev er, that since the items function is inherited b y the Set class, it do es not need to b e rep eated in the class declaration, as w e w ere required to do with the rst tec hnique. A programmer using the set abstraction migh t w an t to use inheritance
  • ne
more time in
  • rder
to a v
  • id
the rep eated use
  • f
fully qualied names. T
  • do
this, the programmer creates a new class with no data areas and no b eha vior, as follo ws: 1 The question
  • f
whether the add function in the class Set m ust in v
  • k
e the add function in the class List is tied to a fundamen tal dierence b et w een the so-called \American" sc ho
  • l
  • f
  • b
ject-orien ted languages v ersus the so-called \Scandina vian" sc ho
  • l.
The references at the end
  • f
this c hapter con tain further p
  • in
ters to discussions
  • f
this distinction.
slide-7
SLIDE 7 7.1. OBJECT-ORIENTED SETS 139 class stringSet
  • f
set[string] f no data, no new b eha vior g end; The v ariables barkingAnimals and zooAnimals in Figure 7.2 can then b e dened as instances
  • f
stringSet. Similarly the constructor for stringSet can b e in v
  • k
ed, whic h simply constructs an instance
  • f
the class Set[string]. In all
  • ther
resp ects the example program sho wn in Figure 7.2 remains unc hanged, since exactly the same syn tax is used with b
  • th
forms
  • f
sets, and exactly the same
  • utput
will b e generated b y eac h. The is-a relation Giv en the existence
  • f
these t w
  • dieren
t soft w are reuse mec hanisms it is natural to ask under what conditions it is more appropriate to use inheritance and under what conditions it is b etter to use comp
  • sition.
While it is dicult to pro vide a hard-and-fast rule that is applicable in all situations, a simple heuristic is useful in answ ering this question in the large ma jorit y
  • f
cases. This heuristic is commonly kno wn as the \is-a" test. T
  • apply
the \is-a" test, form an English sen tence b y placing the name
  • f
the c hild class rst, follo w ed b y the w
  • rds
\is a", and lastly the name
  • f
the paren t class. If the resulting assertion app ears to \sound righ t", then inheritance is lik ely the prop er tec hnique to emplo y . If the sen tence seems to sound a little
  • dd,
then inheritance is lik ely not the prop er tec hnique to use. Let us apply the is-a heuristic to the presen t case. Consider the sen tence: A Set is a List. It is lik ely that this assertion do es not seem en tirely accurate. F
  • r
example, there are man y features
  • f
a set that do not seem to b e necessarily applicable to lists. A set is unordered, and con tains
  • nly
unique elemen ts, to name just t w
  • .
On the
  • ther
hand, the sen tence A Set can b e implemen ted using a List. mak es m uc h more sense. This simple test seems to indicate, then, that comp
  • sition,
rather than inheritance, is the b etter implemen tation tec hnique for constructing sets
  • ut
  • f
lists. W e will return to a discussion
  • f
the is-a test when w e discuss co- and con tra v ariance in
  • b
ject
  • rien
ted languages, in Chapter 10. 7.1.3 Con trasting Comp
  • sition
and Inheritance Ha ving illustrated t w
  • dieren
t mec hanisms for soft w are reuse, and ha ving seen that they are b
  • th
applicable to the implemen tation
  • f
sets, w e are no w in a p
  • sition
to commen t
  • n
some
  • f
the adv an tages and disadv an tages
  • f
the t w
  • approac
hes. The mec hanism
  • f
comp
  • sition
is the simpler
  • f
the t w
  • tec
hniques. The adv an tage
  • f
comp
  • sition
is that it more clearly indicates exactly what
  • p
erations can b e p erformed
  • n
slide-8
SLIDE 8 140 CHAPTER 7. SETS a particular data structure. Lo
  • king
at the declaration sho wn in Figure 7.1, it is clear that the
  • nly
  • p
erations pro vided for the Set data t yp e are addition, the inclusion test, and set union. This is true regardless
  • f
what
  • p
erations are dened for lists. Using inheritance,
  • n
the
  • ther
hand, the
  • p
erations
  • f
the new data abstraction are a sup erset
  • f
the
  • p
erations pro vided for the
  • riginal
data structure
  • n
whic h the new
  • b
ject is built. Th us, for the programmer to kno w exactly what
  • p
erations are legal for the new structure it is necessary to examine the declaration for the
  • riginal.
An examination
  • f
the declaration sho wn in Figure 7.3, for example, do es not immediately indicate that the includes test can legally b e applied to sets. It is
  • nly
b y examining the declaration for the earlier data abstraction (in this case, the List class dened in Figure 4.1, page 63), that the en tire set
  • f
legal
  • p
erations can b e ascertained. The
  • bserv
ation that for the programmer to understand a class constructed using inheritance it is
  • ften
necessary to ip bac k and forth b et w een t w
  • (or
more) class declarations has b een lab eled the \y
  • -y
  • "
problem. (See bibliograph y section for references). Ho w ev er, the brevit y
  • f
data abstractions constructed using the abstraction mec hanism is, in another ligh t, an adv an tage. Using inheritance it is not necessary to write an y co de to access the functionalit y pro vided b y the class
  • n
whic h the new structure is built. F
  • r
this reason implemen tations using inheritance are almost alw a ys, as in the presen t case, considerably shorter in co de length than implemen tations constructed using comp
  • sition,
while at the same time
  • ften
pro viding greater functionalit y . F
  • r
example, the inheritance implemen tation not
  • nly
mak es a v ailable the includes test for sets, but also the functions remove and
  • nEach.
Finally , data structures implemen ted using inheritance tend to ha v e a v ery small ad- v an tage in execution time
  • v
er those constructed using comp
  • sition,
since
  • ne
additional function call is a v
  • ided.
(The preceding statemen t is true
  • f
Leda, but should not b e con- strued to b e true for all programming languages. Some languages, suc h as C++, ha v e a feature that p ermits function calls to b e replaced directly b y their asso ciated function b
  • d-
ies. In these languages using this mec hanism the
  • v
erhead
  • f
the additional function call can sometimes b e eliminated.) The b
  • ttom
line, ho w ev er, is that b
  • th
tec hniques are v ery useful, and an
  • b
ject-orien ted programmer should b e familiar with either form. 7.2 Implemen ting Sets using Characteristic F unctions While it ma y b e natural to rst think ab
  • ut
represen ting sets as data, there are man y examples in b
  • th
computer theory and soft w are engineering where sets are more easily represen ted as actions; that is, as functions. F
  • r
example, formal languages are consider to b e c haracterized either b y a description
  • r
b y a recognition function. A set can b e dened equally w ell b y a regular expression: ( a ( bc | cb ) )* d
slide-9
SLIDE 9 7.2. IMPLEMENTING SETS USING CHARA CTERISTIC FUNCTIONS 141
  • r
b y a nite automata: m m m m m m S F
  • @
@ @ @ @ R
  • @
@ @ @ @ R
  • a
c b a b c d The t w
  • forms
are usually considered to b e equiv alen t. That is, the nite automata returns true if and
  • nly
if the input matc hes the set description. In a similar manner, in the C standard library there is a function used to tell whether a c haracter v alue represen ts an upp er case letter. In set terms, w e can think
  • f
this as testing whether the c haracter is in the set
  • f
upp er case letters. But
  • p
erationally the function simply p erforms a calculation whic h returns true if the argumen t happ ens to b e lo cated within a giv en range: int isUpperCase(cha r c) f return c >= A && c <= Z; g In b
  • th
these cases the underlying idea is the same. Namely , to c haracterize a set it is sucien t to dene a function whic h return true if the argumen t is con tained in the set, and false
  • therwise.
Suc h a function is kno wn as a char acteristic function for the set. In this rst section
  • ur
c haracteristic functions will exactly matc h this basic idea. That is, the c haracteristic functions will tak e as argumen t a v alue, and return a b
  • lean
result that is true if the argumen t is con tained in the set and false
  • therwise.
W e will see in the second section that this rather direct approac h ma y ha v e certain disadv an tages, and these disadv an tages can b e
  • v
ercome b y using a sligh tly more indirect represen tation. T
  • implemen
t
  • ur
three example set
  • p
erations w e will pro vide a quartet
  • f
utilit y func- tions, named emptySet, setAdd, setIncludes and setUnion. The rst tak es no argumen ts, and simply generates a new empt y set. Eac h
  • f
the latter three will tak e t w
  • argumen
ts. The rst argumen t in all cases is the c haracteristic function represen ting the set, while the second argumen t represen ts either an individual set elemen t (in the case
  • f
addition
  • r
inclu- sion)
  • r
the c haracteristic function asso ciated with another set (in the case
  • f
union). The
slide-10
SLIDE 10 142 CHAPTER 7. SETS rst and third functions, setAdd and setUnion, will return a new c haracteristic function that represen ts the set with the addition
  • f
the new elemen t (or elemen ts). The second function (setIncludes) returns a b
  • lean
v alue whic h indicates whether
  • r
not the second argumen t is con tained in the set. Both functions are generic, and th us m ust b e qualied b y an argumen t represen ting the t yp e
  • f
  • b
ject held in the set. T
  • see
ho w these
  • p
erations migh t b e used, consider the program sho wn in Figure 7.4, whic h is a rewriting
  • f
the test program used in the earlier Figure 7.2. Changes include the follo wing:
  • A
t yp e denition denes the name stringSet to b e a synon ym for a function that tak es a string as argumen t and returns a b
  • lean
v alue as result.
  • The
t w
  • sets
are no w declared simply as instances
  • f
the t yp e stringSet.
  • Adding
v alues to the set is accomplished b y in v
  • king
the function setAdd, whic h returns a new c haracteristic function v alue. Although w e could ha v e written the statemen t that initializes the set as a series
  • f
assignmen ts, it is more c haracteristic
  • f
functional programming to generate the set v alue
  • nce,
using the result
  • f
  • ne
addition as argumen t to the next.
  • T
  • test
to see if a v alue is con tained in a set w e simply in v
  • k
e the c haracteristic function
  • n
the v alue. The function returns true if it is in the set, and false
  • therwise.
Figures 7.5 and 7.6 illustrate the implemen tation
  • f
the set manipulation functions. The empt y set function simply returns a new c haracteristic function whic h answ ers false to ev ery query . Th us, no elemen ts are con tained in an empt y set. The implemen tation
  • f
the setAdd function is more complex. The function rst c hec ks to see if the elemen t b eing added is already presen t in the set, b y simply in v
  • king
the c haracteristic function with the new v alue as argumen t. If it is in the set then there is no reason to add it again, so the curren t c haracteristic function is returned. If,
  • n
the
  • ther
hand, the elemen t is not in the set, then a new c haracteristic function is generated. The new function captures and retains the en vironmen t in whic h it w as dened. Then, when in v
  • k
ed, the v alue
  • f
the v ariable named addedValue will b e the v alue it held when the function w as created. The new function tak es as argumen t a set elemen t, and compares the argumen t to the captured quan tit y in the v ariable addedValue. If they are equal the result true is returned,
  • therwise
the existing c haracteristic function, whic h w as also captured when the function w as dened, is in v
  • k
ed to see if the argumen t is presen t in the set. This new c haracteristic function is then returned as the result
  • f
the setAdd
  • p
eration. The setUnion function (Figure 7.6) is similar. A new function is created that tak es as argumen t a v alue, and returns true if the v alue is found in either the rst set
  • r
in the second set. An in teresting feature to note in relation to the set union function is the w a y in whic h the co de mirrors the denition
  • f
a union. A v alue is in the union if either it is in the rst set,
  • r
it is in the second set. Th us, it is not surprising that the logical
  • r
  • p
erator
slide-11
SLIDE 11 7.2. IMPLEMENTING SETS USING CHARA CTERISTIC FUNCTIONS 143 type stringSet : function(string)- >bo
  • l
ea n; var animals : array[string]; barkingAnimals : stringSet; zooAnimals : stringSet; name : string; begin animals := ["cat", "dog", "seal", "lion", "horse"]; barkingAnimals := setAdd[string]( setAdd[string]( emp ty Set [s tri ng ]( ), "dog"), "seal"); zooAnimals := setAdd[string]( setAdd[string]( emp ty Set [s tri ng ]( ), "seal"), "lion"); for animals.items(n ame ) do begin print("a "); print(name); if zooAnimals(name) then print(" is a zoo animal") else print(" is a non-zoo animal"); if barkingAnimals(na me) then print(" that barks\n") else print(" that does not bark\n"); end; end; Figure 7.4: An Example Program using Characteristic F unctions
slide-12
SLIDE 12 144 CHAPTER 7. SETS f return a new empt y set g function emptySet [X : equality] ()-> function(X)->bool ea n; begin return function (v : X)->boolean; begin f alw a ys just sa y no g return false; end; end; f return c haracteristic function for set con taining new elemen t g function setAdd [X : equality] (theFun : function(X)->bool ea n, addedValue : X)->function(X)- >b
  • lea
n; begin f if already in the set then do not add again g if setIncludes[X] (theFun, addedValue) then return theFun; f return a new c haracteristic function g return function(newVal : X)->boolean; begin if (newVal = addedValue) then return true; return theFun(newVal); end; end; Figure 7.5: Implemen tation
  • f
Sets using Characteristic F unctions
slide-13
SLIDE 13 7.2. IMPLEMENTING SETS USING CHARA CTERISTIC FUNCTIONS 145 f see if elemen t is con tained in set c haracterized b y function g function setIncludes [X : equality] (theFun : function(X)->bool ea n, value : X)
  • >
boolean; begin f simply return the c haracteristic function v alue g return theFun(value); end; f return c haracteristic function represen t union
  • f
t w
  • sets
g function setUnion [X : equality] (firstSet, secondSet : function(X)->boo le an)
  • >
fu nct io n(X )- >bo
  • l
ea n; begin return function(newVal : X)->boolean; begin f true if either in rst
  • r
second g return firstSet(newVal) j secondSet(newVal) ; end; end; Figure 7.6: Set Union with Characteristic F unctions app ears at the heart
  • f
the function sho wn in Figure 7.6. What is surprising, p erhaps, is that no where do es an
  • r
app ear in the equiv alen t function constructed in the
  • b
ject-
  • rien
ted v ersion (Figure 7.3). The
  • b
ject-orien ted algorithm describ es how a union can b e constructed, whereas the function v ersion is more declarativ e, describing what a union represen ts. F
  • r
completeness w e ha v e also giv en in Figure 7.6 a denition
  • f
the function setIncludes; although it simply turns around and in v
  • k
es the rst argumen t using the second argumen t as v alue. Th us, in practice, there w
  • uld
b e little reason to use the includes function with this represen tation. This is not true
  • f
the represen tation to b e studied in the next section. T
  • motiv
ate the need for a dieren t represen tation, consider that a dicult problem to
  • v
ercome using sets represen ted b y c haracteristic functions is the task
  • f
iteration. Imagine, for example, the formation
  • f
the union
  • f
the sets
  • f
zo
  • animals
and barking animals, follo w ed b y the prin ting
  • f
the elemen ts con tained in the new set. This can b e accomplished, but
  • nly
b y explicitly iterating
  • v
er al l animals and testing eac h
  • ne
for inclusion in the new set: thirdSet := setUnion[string] (zooAnimals, barkingAnimals); print("union
  • f
barking animals and zoo animals is: "); for animals.items(n ame ) do if setIncludes[strin g] (thirdSet, name) then begin
slide-14
SLIDE 14 146 CHAPTER 7. SETS print(name); print(" "); end; print("\n"); In the next section w e will consider a dieren t represen tation that simplies this problem. 7.2.1 Characteristic F unctional Argumen ts Using the set implemen tation tec hnique illustrated in Figure 7.5, it is easy to determine if an y particular elemen t is con tained in a set;
  • ne
merely executes the c haracteristic function. If, ho w ev er, w e do not ha v e a particular elemen t in mind, but merely wish to determine whic h elemen ts are presen t in the set, then the task is m uc h more dicult. If the p
  • ssible
range
  • f
elemen ts is relativ ely small, then w e can simply lo
  • p
  • v
er eac h p
  • ten
tial v alue and use the inclusion test. This w as the tec hnique emplo y ed b y the example program sho wn in Figure 7.4. Supp
  • se,
  • n
the
  • ther
hand, that the range
  • f
v alues is v ery large, for example the set
  • f
in teger n um b ers. W e w
  • uld
certainly not w an t to lo
  • p
  • v
er all in tegers to see whic h v alues w ere con tained in the set. 2 T
  • solv
e this problem, w e will prop
  • se
a sligh tly dieren t represen tation for a set. W e will con tin ue to represen t a set b y a c haracteristic function; ho w ev er, instead
  • f
the function returning a b
  • lean
v alue, w e will tak e as argumen t to the c haracteristic func- tion another function as argumen t, and simply apply the function argumen t to eac h v alue con tained in the set. Th us, the c haracteristic function is p erforming the same task as the function
  • nEach
pro vided for the List abstraction. T
  • form
a new empt y set w e in v
  • k
e the function emptySet, sho wn in Figure 7.7. The v alue returned is a function whic h, when pro vided with an action to p erform
  • n
ev ery elemen t, simply do es nothing. The setIncludes function, also sho wn in Figure 7.7, p erhaps b etter illustrates the w a y in whic h this represen tation
  • f
functions is utilized. The purp
  • se
  • f
the function is to test whether a sp ecic v alue is found in the set b eing c haracterized b y the rst argumen t. T
  • do
this, the set is passed a new function, generated directly b y a function expression. This function will b e then in v
  • k
ed
  • n
eac h elemen t
  • f
the set. When in v
  • k
ed, the function compares the elemen t v alue against the test v alue whic h w as passed to the setIncludes function. If an y elemen t matc hes, then the v ariable result is set to true. If result remains false after execution, it can
  • nly
b e b ecause the tested v alue is not con tained in the set. The action
  • f
the setAdd function (Figure 7.8) is also dieren t in the new represen tation than the similarly named function in the previous form ulation. As b efore, the setAdd function rst tests to see if a v alue is already con tained in a collection. If so, then no further action is necessary and the existing c haracteristic function is returned. If,
  • n
the
  • ther
2 Exercise 11 in v estigates some
  • f
the diculties caused b y this limitation in the implemen tation tec hnique
  • f
Figure 7.5.
slide-15
SLIDE 15 7.2. IMPLEMENTING SETS USING CHARA CTERISTIC FUNCTIONS 147 f return a new empt y set g function emptySet [X : equality] ()->function(func ti
  • n(
X) ); begin return function(action :function(X)); begin f do nothing g ; end; end; f see if elemen t is con tained in set c haracterized b y function g function setIncludes [X : equality] (theFun : function(function (X )), value : X)->boolean; var result : boolean; begin result := false; f test eac h v alue in turn g f if an y matc h argumen t then set the result ag g theFun(function(t es t : X); begin if test = value then result := true; end); f return the nal v alue
  • f
the result ag g return result; end; Figure 7.7: Creation
  • f
empt y set and set inclusion test using c haracteristic functionals
slide-16
SLIDE 16 148 CHAPTER 7. SETS f return c haracteristic function
  • f
set con taining elemen t g function setAdd [X : equality] (theFun : function(function (X )), value : X)->function(func tio n( X) ); begin f if v alue is already in set then do not add g if setIncludes[X](t heF un , value) then return theFun; f return a new c haracteristic function g return function (action: function(X)); begin f rst execute action
  • f
new v alue g action(value); f then execute
  • n
remainder
  • f
the v alues g if defined(theFun) then theFun(action); end; end; f return c haracteristic function for set with elemen t remo v ed g function setRemove [X : equality] (theFun : function(function (X )), value : X)->function(func tio n( X) ); begin f if v alue is not in set then do not remo v e it g if
  • setIncludes[X](th
eF un, value) then return theFun; f return new c haracteristic functiong return function (action : function(X)); begin if defined(theFun) then theFun(function(t est Va l : X); begin if testVal <> value then action(testVal); end); end; end; Figure 7.8: Implemen tation
  • f
Set Addition and Set Remo v al using Characteristic F unc- tionals
slide-17
SLIDE 17 7.2. IMPLEMENTING SETS USING CHARA CTERISTIC FUNCTIONS 149 f return c haracteristic function represen ting union
  • f
t w
  • sets
g function setUnion [X : equality] (firstSet, secondSet : function(functio n( X)) )- > function(function (X) ); begin return function(action: function(X)); begin f execute action
  • n
elemen ts in rst set g firstSet(action) ; f and
  • nly
  • n
elemen ts in second set g f that w ere not in rst set g secondSet(functi
  • n(
va lue : X); begin if setIncludes[X](f irs tS et, value) then action(value); end); end; end; Figure 7.9: Implemen tation
  • f
Set Union hand, the v alue is not already in the set then a new c haracteristic function m ust b e created. This new function tak es as argumen t a function, named action, whic h describ es the action to b e p erformed
  • n
eac h elemen t
  • f
the set. When in v
  • k
ed, the action will b e p erformed using as argumen t the new v alue
  • f
the set. T
  • p
erform the action
  • n
the previous v alues
  • f
the set the action function is passed as argumen t to the previous c haracteristic function, whic h w as captured when the new c haracteristic function w as constructed. The dev elopmen t
  • f
a function to remo v e an elemen t from a set will serv e as a v ehicle to further illustrate the manipulation
  • f
data structures implemen ted using the c haracteristic function tec hnique. The setRemove function is sho wn in Figure 7.8. If the giv en v alue is not presen t in the set, then there is no need to remo v e it and th us the c haracteristic function giv en as argumen t can b e returned. Otherwise, a new c haracteristic function is generated. This new function tak es as argumen t an action to b e p erformed, but passes to the existing c haracteristic function a function that
  • nly
applies this action if the argumen t fails to matc h the v alue that has b een remo v ed. Th us, the remo v ed v alue will nev er b e executed with the giv en action, and it will eectiv ely b e eliminated from the set. Finally , the implemen tation
  • f
set union (Figure 7.9) is a tour-de-for c e in the use
  • f
this represen tation. The task
  • f
forming a union is made complicated b y the requiremen t that v alues m ust app ear in a set
  • nly
  • nce.
If a v alue app ears in b
  • th
p
  • rtions
  • f
a set union, the revised c haracteristic function m ust ensure that an action is
  • nly
p erformed
  • n
the elemen t
  • nce.
The new c haracteristic function rst p erforms the giv en action
  • n
all elemen ts in the
slide-18
SLIDE 18 150 CHAPTER 7. SETS rst set. Next, it p erforms the action
  • nly
  • n
those elemen ts in the second set that do not app ear in the rst set. With a suitable c hange in the declaration for the v ariable zooAnimals and the v ariable barkingAnimals, and b y c hanging the in v
  • cation
  • f
the c haracteristic function to instead use the predicate setIncludes, the example program sho wn in Figure 7.4 w
  • rks
exactly as b efore using this new represen tation. The new t yp e declaration for sets is as follo ws: follo wing: type stringSet : function(function (st ri ng )); T
  • illustrate
the additional p
  • w
er pro vided b y this new tec hnique, w e return again to the problem
  • f
forming the union
  • f
the set
  • f
barking animals and zo
  • animals,
and then prin ting
  • ut
the elemen ts in this third set. In fact, w e will rst solv e the more general problem
  • f
prin ting the v alues held in an arbitrary set. Y
  • u
will recall that it w as not p
  • ssible
to write suc h a function for sets main tained in the rst c haracteristic functional form without explicit kno wledge
  • f
the range
  • f
v alues, and ev en then
  • nly
b y iterating
  • v
er the en tire range
  • f
v alues and testing eac h
  • ne.
But suc h a function is v ery easy to write in the new format: function printSet [ X : equality ] (aSet : function(function (X ))) ; begin f prin t eac h v alue in the set g aSet(function(val : X); begin print(val); print(" "); end); end; With this function the formation and prin ting
  • f
the union can b e accomplished in a single expression: print("the union
  • f
barking animals and zoo animals is:"); printSet[string] (setUnion[string ] (barkingAnimals , zooAnimals)); print("\n");
slide-19
SLIDE 19 7.3. A COMP ARISON OF THE TECHNIQUES 151 7.3 A Comparison
  • f
the T ec hniques Although man y readers will undoubtedly nd the
  • b
ject-orien ted implemen tation
  • f
sets easier to understand, sev eral
  • f
the exercises at the end
  • f
this c hapter together suce to sho w that there is essen tially no functionalit y pro vided b y the
  • b
ject-orien ted v ersion that cannot also b e sim ulated using the functional tec hniques describ ed in Figures 7.7, 7.8 and 7.9. Th us, based
  • n
functionalit y alone there is little reason to prefer
  • ne
tec hnique
  • v
er the
  • ther.
Nev ertheless, sev eral imp
  • rtan
t dierences can b e noted:
  • Using
the functional approac h the represen tation
  • f
a set alw a ys increases in size, nev er getting smaller. Ev en remo ving an elemen t from a set increases the amoun t
  • f
storage used to represen t the set. Using the
  • b
ject-orien ted tec hnique the size
  • f
the set increases and decreases as elemen ts are added and remo v ed. (F
  • r
this reason the c haracteristic functional approac h is most
  • ften
used
  • nly
when the elemen ts in the set are xed,
  • r
c hange v ery slo wly).
  • The
  • b
ject-orien ted v ersion is able to lev erage considerable functionalit y
  • f
the existing List data structure. Using the functional tec hnique basically all new func- tionalit y m ust b e pro vided from scratc h. (This is not to sa y that the use
  • f
functional tec hniques do es not encourage soft w are reuse. In fact, functional programming sup- p
  • rts
a v ery p
  • w
erful, if dieren t, st yle
  • f
soft w are reuse. W e will explore this topic later in Chapter 14.)
  • In
some circumstances, the functional approac h yields pro cedures that are closer to the denition
  • f
the corresp
  • nding
set
  • p
eration than are the equiv alen t metho ds using the
  • b
ject-orien ted tec hnique. Compare, for example, the set union function sho wn in Figure 7.6 with the earlier function sho wn in Figure 7.1. The
  • b
ject-orien ted v ersion describ es
  • p
erationally ho w to form the union, but not what a union represen ts. The functional solution is m uc h closer to the denition
  • f
the union.
  • The
declaration
  • f
a set using the
  • b
ject-orien ted v ersion is sligh tly more self-eviden t then the declaration
  • f
a similar structure using the functional approac h.
  • In
terestingly , the functional approac h can result in programs that are faster. The follo wing table pro vides timings for an example program that created a set con taining 100 random elemen ts, then p erformed 10,000 tests for inclusion. This is p erhaps wh y the most common use
  • f
c haracteristic functions
  • ccurs
in those situations where sp eed is v ery imp
  • rtan
t (see commen ts in the bibliograph y for further discussion). Implemen tation execution time (seconds) Figure 7.1 103.9 Figure 7.3 93.9 Figure 7.7 72.0
slide-20
SLIDE 20 152 CHAPTER 7. SETS Notes and Bibliograph y The \y
  • -y
  • "
problem, whic h can
  • ccur
when a new data structure is implemen ted using inheritance, w as rst discussed b y T aenzer [T aenzer 89 ]. I also discuss this problem in m y b
  • k
  • n
Ob ject-Orien ted programming [Budd 91b ]. The functional implemen tation
  • f
sets w as suggested b y an example program in Sam uel Kamin's b
  • k
  • n
programming languages [Kamin 90 ]. Sets are found as a primitiv e data t yp e in a n um b er
  • f
programming languages. Examples include Setl [Baxter 89 ], ABC [Geurts 85 ], Icon [Grisw
  • ld
90 ] and
  • thers.
The dierence b et w een the \American" and the \Scandina vian" sc ho
  • ls
  • f
  • b
ject-orien ted program concerns the question
  • f
whether an
  • v
erridden function is a r enement
  • r
a r e- plac ement
  • f
the function named in the paren t class. In the Scandina vian sc ho
  • l
(so named b ecause it is c haracterized b y the languages Sim ula [Dahl 66 ] and BET A [Madsen 93 ], whic h w ere created in Scandina vian coun tries), an
  • v
erridden function m ust alw a ys rst in v
  • k
e the function in the paren t class, then in v
  • k
e the function in the c hild class. Th us, it w
  • uld
b e more dicult to write the Set class using inheritance in suc h a system, since the Add function from class List w
  • uld
alw a ys b e in v
  • k
ed for eac h call
  • n
Add in the class Set. This set class w
  • uld
then need to see if the elemen t w as already in the collection, and if so remo v e it
  • nce
again. On the
  • ther
hand, in the \American" sc ho
  • l
(c haracterized b y languages suc h as Smalltalk [Goldb erg 83 ] and C++ [Stroustrup 86 ]) an
  • v
erridden function totally replaces the function in the paren t class. This has b
  • th
adv an tages, as illustrated b y the implemen- tation
  • f
sets from lists, and disadv an tages. W e will explore some
  • f
the disadv an tages later in Chapter 10. While the implemen tation
  • f
sets as c haracteristic functions ma y strik e man y program- mers used to more con v en tional tec hniques as
  • dd,
the principle that data can
  • ften
b e replaced b y co de is found in a n um b er
  • f
dieren t situations. Finite state automata, for example, are easily describ ed in a tabular form. Y et when eciency is a concern, suc h as in parsers
  • r
lexical analyzers, the states
  • f
the nite state automata are frequen tly co ded directly as program statemen ts [Aho 86 , F raser 92 , Horsp
  • l
90 ]. Exercises 1. In the same w a y that the addition
  • p
erator w as
  • v
erloaded to mean addition to lists, redene the meaning
  • f
this
  • p
erator for eac h
  • f
the set structures dened in this c hapter. 2. Add a function named remove to the set abstraction dened in Figure 7.1. This function should remo v e the item, if presen t, from the collection, p erforming no action if the item is not presen t in the set. F
  • r
example, subsequen t to the follo wing set
  • f
statemen ts the collection should con tain exactly
  • ne
item. zooAnimals.add( lio n) ;
slide-21
SLIDE 21 7.3. A COMP ARISON OF THE TECHNIQUES 153 zooAnimals.add( dog ); zooAnimals.add( dog ); zooAnimals.remo ve( do g) ; 3. Explain wh y it is not necessary to dene a function to remo v e items from the set abstraction sho wn in Figure 7.3. 4. Tw
  • ther
  • p
erations commonly asso ciated with sets are in tersection and dierence. The in tersection
  • f
t w
  • sets
is the set
  • f
elemen ts con tained in b
  • th
sets. T
  • in
tersect
  • ne
set with another, remo v e all elemen ts from the rst set that do not app ear in the second. The dierence
  • f
t w
  • sets
is the in v erse
  • f
the in tersection; it is the elemen ts in the rst set that do not app ear in the second. W rite functions to implemen t these
  • p
erations using the
  • b
ject-orien ted form
  • f
sets. Can y
  • u
write functions that will w
  • rk
for b
  • th
the sets built using inheritance and the sets built using comp
  • sition?
5. Tw
  • sets
are equal if they con tain the same elemen ts. Sho w ho w the equalit y testing
  • p
erator, =, could b e implemen ted for the set data t yp e describ ed in Figure 7.3. T
  • do
so, mak e the set class a sub class
  • f
equalit y , and follo w the tec hniques used in Chapter 6 to redene the meaning
  • f
the equalit y testing
  • p
erator in class Association. Ho w w
  • uld
this dier if y
  • u
used the set data t yp e describ ed in Figure 7.1? 6. One set is said to b e a subset
  • f
another set if all the v alues in the rst are also found in the second. (The second set can also, although need not, con tain additional elemen ts). W e can
  • v
erload the meaning
  • f
the \less than"
  • p
erator, <, to mean the subset test when used with t w
  • set
v alues. Pro vide an implemen tation for this
  • p
erator using the set data t yp e describ ed in Figure 7.3. Ho w w
  • uld
this dier if y
  • u
used the set data t yp e describ ed in Figure 7.1? 7. Chapter 4 in tro duced t w
  • functional
uses
  • f
data structures, mapping and application. In application a function is pro vided as argumen t and applied to eac h elemen t
  • f
a collection. This functionalit y w as pro vided b y the function
  • nEach
in the list data abstraction sho wn in Figure 4.1 (page 63). Implemen t the function
  • nEach
for the set data abstraction sho wn in Figure 7.1. 8. Explain wh y , to ac hiev e the functionalit y describ ed in the previous question, it is not necessary to implemen t the
  • nEach
function for the set abstraction sho wn in Figure 7.3. 9. Implemen t a function named setRemove whic h will remo v e an elemen t from a set constructed using the functions in Figure 7.5. Hin t: it is not p
  • ssible
(or at least, not easy) to actually remo v e the elemen t from the c haracteristic function. Rather, simply create a new c haracteristic function that will return the v alue false when queried ab
  • ut
the giv en elemen t. 10. Implemen t the set
  • p
erations
  • f
in tersection and dierence using the c haracteristic function represen tation
  • n
Figure 7.5.
slide-22
SLIDE 22 154 CHAPTER 7. SETS 11. As w e note in Section 7.2.1, there is no easy w a y to determine whic h elemen ts are con tained in a set constructed in the fashion
  • f
Figure 7.5, short
  • f
simply lo
  • ping
  • v
er all p
  • ssible
v alues and testing eac h
  • ne
individually . Explain ho w this complicates the implemen tation
  • f
a set equalit y
  • r
subset
  • p
erator using this represen tation. Do es it also complicate the implemen tation
  • f
  • nEach?
Wh y do es it not complicate the set
  • p
erations describ ed in the previous question? 12. Consider the follo wing sequence
  • f
statemen ts, whic h mak e use
  • f
the set implemen- tation sho wn in Figures 7.7 and 7.8. type intSet : function(function (i nt ege r) ); begin intSet := setAdd:(integer) (i nt Set , 12); intSet := setAdd:(integer) (i nt Set , 237); intSet := setAdd:(integer) (i nt Set , 237); intSet := setAdd:(integer) (i nt Set , 1632); intSet := setRemove:(integ er )( int Se t, 237); intSet := setAdd:(integer) (i nt Set , 237); Describ e the structure
  • f
the nal v alue
  • f
intSet. Ho w man y elemen ts do es the set con tain? 13. Sho w ho w to implemen t the set equalit y
  • p
eration and subset
  • p
eration using the represen tation pro vided in Figure 7.7. 14. Implemen t the set
  • p
erations
  • f
in tersection and dierence using the c haracteristic function represen tation
  • n
Figure 7.7. 15. Sho w ho w to implemen t the functionalit y
  • f
the
  • nEach
  • p
eration using the set rep- resen tation pro vided in Figure 7.7.