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 concept of a set (w e assume the reader is already familiar with the idea) but to further explore some of the tec hniques that can b e used in the represen tation of common data structures. F or this c hapter w e will de�ne a set to b e an unordered collection of ob jects of the same t yp e, with no v alue app earing more than once in the set. In order to simplify our presen ta- tion, w e will consider only three op erations on sets. The action of addition incorp orates 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 of one 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 of the c hapter explore the implemen tation of v arious other set op erations. Unlik e the c hapter on Lists (Chapter 4), whic h dev elop ed a single implemen tation of a data structure that made use of facilities from m ultiple di�eren t paradigms, in this c hapter w e will consider four di�eren t implemen tation approac hes, eac h more-or-less pure in their o wn paradigm. Tw o of the approac hes will mak e use of ob ject-orien ted programming, and t w o of the approac hes will use functional tec hniques. The presen tation of di�eren t imple- men tations p ermits the reader to explore the range of p oten tial solution tec hniques, and to more clearly see some of the adv an tages and disadv an tages of the di�eren 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 of existing soft w are abstractions in the dev elopmen t of new soft w are comp onen ts. W e sa w this in the previous c hapter, where w e constructed in an ob ject-orien ted fashion the data Table t yp e on top of the abstraction from Chapter 4. In the �rst part of this c hapter w e List 133
134 CHAPTER 7. SETS will once again illustrate ho w this is accomplished b y building the ob ject-orien ted v ersions of our set data abstraction b y making use of the List data structure. There are t w o primary mec hanisms a v ailable for soft w are reuse using ob ject-orien ted programming, and b oth are applicable in the dev elopmen t of a set data abstraction. In the �rst of the follo wing t w o sections w e illustrate soft w are reuse using the idea of c omp osition , while in the second section w e dev elop an en tirely di�eren t approac h using the tec hnique of inheritanc e . 7.1.1 Implemen ting Sets using Comp osition Y ou will recall that an ob ject is simply an encapsulation of state (data v alues) and b eha vior. When using comp osition to reuse an existing data abstraction in the dev elopmen t of a new data t yp e, a p ortion of the state of the new data structure is simply an instance of the existing structure. This is illustrated in Figure 7.1, where the data t yp e Set con tains a �eld of 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 of t yp e List . Op erations used to manipulate the new structure are implemen ted b y making use of the existing op erations pro vided for the earlier data t yp e. F or example, the implemen tation of the op eration for our set data structure simply in v ok es the similarly-named includes function already de�ned for lists. Notice that b oth 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 op eration, and initialize the data �eld to hold a newly created empt y list if needed. The addition of 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 our de�nition of a set stipulates that eac h elemen t app ears only once in the collection. Only if the elemen t is not already in the set is it added to the list. The op eration of union is made di�cult b y the necessit y of lo oping o v er the elemen ts in the second set. Since this is will undoubtedly also b e an op eration users of the set data abstraction will wish to p erform, w e mak e a v ailable the function named that w as items used with the list abstraction to p erform iteration. The function simply in v ok es the items functions with the same name in the list class. Figure 7.2 illustrates the use of our set data abstraction. An arra y literal is de�ned to hold the names of di�eren t t yp es of animals. Tw o sets are de�ned, one con taining animals that bark and one con taining animals that are found in a zo o. A lo op then prin ts out information concerning eac h t yp e of animal. The output of the program w ould 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
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; f g begin iterate o v er v alues from set return defined(values) & values.items(val) ; end; function unionWith(secon dSe t : Set[T]); f add all elemen ts from second set g var val : T; begin for secondSet.items( val ) do add(val); end; end; Figure 7.1: Sets Constructed using Comp osition
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") ; f g zooAnimals.add("l io n") ; second add has no e�ect 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
Recommend
More recommend