Docsity

Tipos Abstratos de Dados: Implementações e Métodos, Notas de estudo de Mecatrônica

Toggle sidebar
bg1
v
___________________________________________________________________________
Preface
No programming technique solves all problems.
No programming language produces only correct results.
No programmer should start each project from scratch.
Object-oriented programming is the current cure-all — although it has been
around for much more then ten years. At the core, there is little more to it then
finally applying the good programming principles which we have been taught for
more then twenty years. C++ (Eiffel, Oberon-2, Smalltalk ... take your pick) is the
New Language because it is object-oriented — although you need not use it that
way if you do not want to (or know how to), and it turns out that you can do just as
well with plain ANSI-C. Only object-orientation permits code reuse between pro-
jects — although the idea of subroutines is as old as computers and good program-
mers always carried their toolkits and libraries with them.
This book is not going to praise object-oriented programming or condemn the
Old Way. We are simply going to use ANSI-C to discover how object-oriented pro-
gramming is done, what its techniques are, why they help us solve bigger prob-
lems, and how we harness generality and program to catch mistakes earlier. Along
the way we encounter all the jargon — classes, inheritance, instances, linkage,
methods, objects, polymorphisms, and more — but we take it out of the realm of
magic and see how it translates into the things we have known and done all along.
I had fun discovering that ANSI-C is a full-scale object-oriented language. To
share this fun you need to be reasonably fluent in ANSI-C to begin with — feeling
comfortable with structures, pointers, prototypes, and function pointers is a must.
Working through the book you will encounter all the newspeak — according to
Orwell and Webster a language ‘‘designed to diminish the range of thought’’ — and
I will try to demonstrate how it merely combines all the good programming princi-
ples that you always wanted to employ into a coherent approach. As a result, you
may well become a more proficient ANSI-C programmer.
The first six chapters develop the foundations of object-oriented programming
with ANSI-C. We start with a careful information hiding technique for abstract data
types, add generic functions based on dynamic linkage and inherit code by judicious
lengthening of structures. Finally, we put it all together in a class hierarchy that
makes code much easier to maintain.
Programming takes discipline. Good programming takes a lot of discipline, a
large number of principles, and standard, defensive ways of doing things right. Pro-
grammers use tools. Good programmers make tools to dispose of routine tasks
once and for all. Object-oriented programming with ANSI-C requires a fair amount
of immutable code — names may change but not the structures. Therefore, in
chapter seven we build a small preprocessor to create the boilerplate required. It
looks like yet another new object-oriented dialect language (yanoodl perhaps?) but
it should not be viewed as such — it gets the dull parts out of the way and lets us
concentrate on the creative aspects of problem solving with better techniques. ooc
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Pré-visualização parcial do texto

Baixe Tipos Abstratos de Dados: Implementações e Métodos e outras Notas de estudo em PDF para Mecatrônica, somente na Docsity!

___________________________________________________________________________v

Preface

No programming technique solves all problems. No programming language produces only correct results. No programmer should start each project from scratch. Object-oriented programming is the current cure-all — although it has been around for much more then ten years. At the core, there is little more to it then finally applying the good programming principles which we have been taught for more then twenty years. C++ (Eiffel, Oberon-2, Smalltalk ... take your pick) is the New Language because it is object-oriented — although you need not use it that way if you do not want to (or know how to), and it turns out that you can do just as well with plain ANSI -C. Only object-orientation permits code reuse between pro- jects — although the idea of subroutines is as old as computers and good program- mers always carried their toolkits and libraries with them.

This book is not going to praise object-oriented programming or condemn the Old Way. We are simply going to use ANSI -C to discover how object-oriented pro- gramming is done, what its techniques are, why they help us solve bigger prob- lems, and how we harness generality and program to catch mistakes earlier. Along the way we encounter all the jargon — classes, inheritance, instances, linkage, methods, objects, polymorphisms, and more — but we take it out of the realm of magic and see how it translates into the things we have known and done all along.

I had fun discovering that ANSI -C is a full-scale object-oriented language. To share this fun you need to be reasonably fluent in ANSI -C to begin with — feeling comfortable with structures, pointers, prototypes, and function pointers is a must. Working through the book you will encounter all the newspeak — according to Orwell and Webster a language ‘‘designed to diminish the range of thought’’ — and I will try to demonstrate how it merely combines all the good programming princi- ples that you always wanted to employ into a coherent approach. As a result, you may well become a more proficient ANSI -C programmer.

The first six chapters develop the foundations of object-oriented programming with ANSI -C. We start with a careful information hiding technique for abstract data types, add generic functions based on dynamic linkage and inherit code by judicious lengthening of structures. Finally, we put it all together in a class hierarchy that makes code much easier to maintain.

Programming takes discipline. Good programming takes a lot of discipline, a large number of principles, and standard, defensive ways of doing things right. Pro- grammers use tools. Good programmers make tools to dispose of routine tasks once and for all. Object-oriented programming with ANSI -C requires a fair amount of immutable code — names may change but not the structures. Therefore, in chapter seven we build a small preprocessor to create the boilerplate required. It looks like yet another new object-oriented dialect language ( yanoodl perhaps?) but it should not be viewed as such — it gets the dull parts out of the way and lets us concentrate on the creative aspects of problem solving with better techniques. ooc

vi___________________________________________________________________________Preface

(sorry) is pliable: we have made it, we understand it and can change it, and it writes the ANSI -C code just like we would.

The following chapters refine our technology. In chapter eight we add dynamic type checking to catch our mistakes earlier on. In chapter nine we arrange for automatic initialization to prevent another class of bugs. Chapter ten introduces delegates and shows how classes and callback functions cooperate to simplify, for example, the constant chore of producing standard main programs. More chapters are concerned with plugging memory leaks by using class methods, storing and loading structured data with a coherent strategy, and disciplined error recovery through a system of nested exception handlers.

Finally, in the last chapter we leave the confines of ANSI -C and implement the obligatory mouse-operated calculator, first for curses and then for the X Window System. This example neatly demonstrates how elegantly we can design and implement using objects and classes, even if we have to cope with the idiosyn- crasies of foreign libraries and class hierarchies.

Each chapter has a summary where I try to give the more cursory reader a run- down on the happenings in the chapter and their importance for future work. Most chapters suggest some exercises; however, they are not spelled out formally, because I firmly believe that one should experiment on one’s own. Because we are building the techniques from scratch, I have refrained from making and using a massive class library, even though some examples could have benefited from it. If you want to understand object-oriented programming, it is more important to first master the techniques and consider your options in code design; dependence on somebody else’s library for your developments should come a bit later.

An important part of this book is the enclosed source floppy — it has a DOS file system containing a single shell script to create all the sources arranged by chapter. There is a ReadMe file — consult it before you say make. It is also quite instructive to use a program like diff and trace the evolution of the root classes and ooc reports through the later chapters.

The techniques described here grew out of my disenchantment with C++ when I needed object-oriented techniques to implement an interactive programming language and realized that I could not forge a portable implementation in C++. I turned to what I knew, ANSI -C, and I was perfectly able to do what I had to. I have shown this to a number of people in courses and workshops and others have used the methods to get their jobs done. It would have stopped there as my footnote to a fad, if Brian Kernighan and my publishers, Hans-Joachim Niclas and John Wait, had not encouraged me to publish the notes (and in due course to reinvent it all once more). My thanks go to them and to all those who helped with and suffered through the evolution of this book. Last not least I thank my family — and no, object-orientation will not replace sliced bread.

Hollage, October 1993 Axel-Tobias Schreiner

___________________________________________________________________________^1

Abstract Data Types

Information Hiding

1.1 Data Types

Data types are an integral part of every programming language. ANSI -C has int , double and char to name just a few. Programmers are rarely content with what’s available and a programming language normally provides facilities to build new data types from those that are predefined. A simple approach is to form aggregates such as arrays, structures, or unions. Pointers, according to C. A. R. Hoare ‘‘a step from which we may never recover,’’ permit us to represent and manipulate data of essentially unlimited complexity.

What exactly is a data type? We can take several points of view. A data type is a set of values — char typically has 256 distinct values, int has many more; both are evenly spaced and behave more or less like the natural numbers or integers of mathematics. double once again has many more values, but they certainly do not behave like mathematics’ real numbers.

Alternatively, we can define a data type as a set of values plus operations to work with them. Typically, the values are what a computer can represent, and the operations more or less reflect the available hardware instructions. int in ANSI -C does not do too well in this respect: the set of values may vary between machines, and operations like arithmetic right shift may behave differently.

More complicated examples do not fare much better. Typically we would define an element of a linear list as a structure

typedef struct node { struct node * next; ... information ... } node;

and for the operations we specify function headers like

node * head (node * elt, const node * tail); This approach, however, is quite sloppy. Good programming principles dictate that we conceal the representation of a data item and declare only the possible manipulations.

1.2 Abstract Data Types

We call a data type abstract , if we do not reveal its representation to the user. At a theoretical level this requires us to specify the properties of the data type by mathematical axioms involving the possible operations. For example, we can remove an element from a queue only as often as we have added one previously, and we retrieve the elements in the same order in which they were added.

(^2) ___________________________________________________________________________1 Abstract Data Types — Information Hiding

Abstract data types offer great flexibility to the programmer. Since the representation is not part of the definition, we are free to choose whatever is easi- est or most efficient to implement. If we manage to distribute the necessary infor- mation correctly, use of the data type and our choice of implementation are totally independent.

Abstract data types satisfy the good programming principles of information hid- ing and divide and conquer. Information such as the representation of data items is given only to the one with a need to know: to the implementer and not to the user. With an abstract data type we cleanly separate the programming tasks of imple- mentation and usage: we are well on our way to decompose a large system into smaller modules.

1.3 An Example — Set

So how do we implement an abstract data type? As an example we consider a set of elements with the operations add , find , and drop .* They all apply to a set and an element and return the element added to, found in, or removed from a set. find can be used to implement a condition contains which tells us whether an element is already contained in a set.

Viewed this way, set is an abstract data type. To declare what we can do with a set, we start a header file Set.h :

#ifndef SET_H #define SET_H extern const void * Set; void * add (void * set, const void * element); void * find (const void * set, const void * element); void * drop (void * set, const void * element); int contains (const void * set, const void * element); #endif

The preprocessor statements protect the declarations: no matter how many times we include Set.h , the C compiler only sees the declarations once. This technique of protecting header files is so standard, that the GNU C preprocessor recognizes it and does not even access such a file when its protecting symbol is defined.

Set.h is complete, but is it useful? We can hardly reveal or assume less: Set will have to somehow represent the fact, that we are working with sets; add() takes an element, adds it to a set, and returns whatever was added or already present in the set; find() looks for an element in a set and returns whatever is present in the set or a null pointer; drop() locates an element, removes it from a set, and returns whatever was removed; contains() converts the result of find() into a truth value.


  • Unfortunately, remove is an ANSI -C library function to remove a file. If we used this name for a set function, we could no longer include stdio.h.

(^4) ___________________________________________________________________________1 Abstract Data Types — Information Hiding

1.6 An Application

With the header files, i.e., the definitions of the abstract data types, in place we can write an application main.c :

#include <stdio.h> #include "new.h" #include "Object.h" #include "Set.h" int main () { void * s = new(Set); void * a = add(s, new(Object)); void * b = add(s, new(Object)); void * c = new(Object); if (contains(s, a) && contains(s, b)) puts("ok"); if (contains(s, c)) puts("contains?"); if (differ(a, add(s, a))) puts("differ?"); if (contains(s, drop(s, a))) puts("drop?"); delete(drop(s, b)); delete(drop(s, c)); return 0; }

We create a set and add two new objects to it. If all is well, we find the objects in the set and we should not find another new object. The program should simply print ok.

The call to differ() illustrates a semantic point: a mathematical set can only contain one copy of the object a ; an attempt to add it again must return the original object and differ() ought to be false. Similarly, once we remove the object, it should no longer be in the set.

Removing an element not in a set will result in a null pointer being passed to delete(). For now, we stick with the semantics of free() and require this to be acceptable.

1.7 An Implementation — Set

main.c will compile successfully, but before we can link and execute the program, we must implement the abstract data types and the memory manager. If an object stores no information and if every object belongs to at most one set, we can represent each object and each set as small, unique, positive integer values used as indices into an array heap[]. If an object is a member of a set, its array element con- tains the integer value representing the set. Objects, therefore, point to the set containing them.

_1.7 An Implementation__________________________________________________________________________^ —^ ‘‘Set’’^5

This first solution is so simple that we combine all modules into a single file Set.c. Sets and objects have the same representation, so new() pays no attention to the type description. It only returns an element in heap[] with value zero:

#if! defined MANY || MANY < 1 #define MANY 10 #endif static int heap [MANY]; void * new (const void * type, ...) { int * p; /* & heap[1..] */ for (p = heap + 1; p < heap + MANY; ++ p) if (! * p) break; assert(p < heap + MANY);

  • p = MANY; return p; }

We use zero to mark available elements of heap[] ; therefore, we cannot return a reference to heap[0] — if it were a set, its elements would contain the index value zero.

Before an object is added to a set, we let it contain the impossible index value MANY so that new() cannot find it again and we still cannot mistake it as a member of any set.

new() can run out of memory. This is the first of many errors, that ‘‘cannot happen’’. We will simply use the ANSI -C macro assert() to mark these points. A more realistic implementation should at least print a reasonable error message or use a general function for error handling which the user may overwrite. For our pur- pose of developing a coding technique, however, we prefer to keep the code uncluttered. In chapter 13 we will look at a general technique for handling excep- tions.

delete() has to be careful about null pointers. An element of heap[] is recycled by setting it to zero:

void delete (void * _item) { int * item = _item; if (item) { assert(item > heap && item < heap + MANY);

  • item = 0; } }

We need a uniform way to deal with generic pointers; therefore, we prefix their names with an underscore and only use them to initialize local variables with the desired types and with the appropriate names.

A set is represented in its objects: each element points to the set. If an ele- ment contains MANY , it can be added to the set, otherwise, it should already be in the set because we do not permit an object to belong to more than one set.

_1.8 Another Implementation__________________________________________________________________________^ —^ ‘‘Bag’’^7

int differ (const void * a, const void * b) { return a != b; }

Objects differ exactly when the array indices representing them differ, i.e., a simple pointer comparison is sufficient.

We are done — for this solution we have not used the descriptors Set and Object but we have to define them to keep our C compiler happy:

const void * Set; const void * Object;

We did use these pointers in main() to create new sets and objects.

1.8 Another Implementation — Bag

Without changing the visible interface in Set.h we can change the implementation. This time we use dynamic memory and represent sets and objects as structures:

struct Set { unsigned count; }; struct Object { unsigned count; struct Set * in; };

count keeps track of the number of elements in a set. For an element, count records how many times this element has been added to the set. If we decrement count each time the element is passed to drop() and only remove the element once count is zero, we have a Bag , i.e., a set where elements have a reference count.

Since we will use dynamic memory to represent sets and objects, we need to initialize the descriptors Set and Object so that new() can find out how much memory to reserve:

static const size_t _Set = sizeof(struct Set); static const size_t _Object = sizeof(struct Object); const void * Set = & _Set; const void * Object = & _Object;

new() is now much simpler:

void * new (const void * type, ...) { const size_t size = * (const size_t *) type; void * p = calloc(1, size); assert(p); return p; }

delete() can pass its argument directly to free() — in ANSI -C a null pointer may be passed to free().

add() has to more or less believe its pointer arguments. It increments the element’s reference counter and the number of elements in the set:

(^8) ___________________________________________________________________________1 Abstract Data Types — Information Hiding

void * add (void * _set, const void * _element) { struct Set * set = _set; struct Object * element = (void *) _element; assert(set); assert(element); if (! element —> in) element —> in = set; else assert(element —> in == set); ++ element —> count, ++ set —> count; return element; }

find() still checks, if the element points to the appropriate set:

void * find (const void * _set, const void * _element) { const struct Object * element = _element; assert(_set); assert(element); return element —> in == _set? (void *) element : 0; }

contains() is based on find() and remains unchanged.

If drop() finds its element in the set, it decrements the element’s reference count and the number of elements in the set. If the reference count reaches zero, the element is removed from the set:

void * drop (void * _set, const void * _element) { struct Set * set = _set; struct Object * element = find(set, _element); if (element) { if (—— element —> count == 0) element —> in = 0; —— set —> count; } return element; } We can now provide a new function count() which returns the number of ele- ments in a set:

unsigned count (const void * _set) { const struct Set * set = _set; assert(set); return set —> count; }

Of course, it would be simpler to let the application read the component .count directly, but we insist on not revealing the representation of sets. The overhead of a function call is insignificant compared to the danger of an application being able to overwrite a critical value.

(^10) ___________________________________________________________________________1 Abstract Data Types — Information Hiding

apply() calls action() for each element in set and passes the rest of the argument list. action() must not change set but it may return zero to terminate apply() early. apply() returns true if all elements were processed.

___________________________________________________________________________^11

Dynamic Linkage

Generic Functions

2.1 Constructors and Destructors

Let us implement a simple string data type which we will later include into a set. For a new string we allocate a dynamic buffer to hold the text. When the string is deleted, we will have to reclaim the buffer.

new() is responsible for creating an object and delete() must reclaim the resources it owns. new() knows what kind of object it is creating, because it has the description of the object as a first parameter. Based on the parameter, we could use a chain of if statements to handle each creation individually. The draw- back is that new() would explicitly contain code for each data type which we sup- port.

delete() , however, has a bigger problem. It, too, must behave differently based on the type of the object being deleted: for a string the text buffer must be freed; for an object as used in chapter 1 only the object itself has to be reclaimed; and a set may have acquired various chunks of memory to store references to its ele- ments.

We could give delete() another parameter: either our type descriptor or the function to do the cleaning up, but this approach is clumsy and error-prone. There is a much more general and elegant way: each object must know how to destroy its own resources. Part of each and every object will be a pointer with which we can locate a clean-up function. We call such a function a destructor for the object.

Now new() has a problem. It is responsible for creating objects and returning pointers that can be passed to delete() , i.e., new() must install the destructor infor- mation in each object. The obvious approach is to make a pointer to the destructor part of the type descriptor which is passed to new(). So far we need something like the following declarations:

struct type { size_t size; /* size of an object / void ( dtor) (void ); / destructor / }; struct String { char * text; / dynamic string / const void * destroy; / locate destructor / }; struct Set { ... information ... const void * destroy; / locate destructor */ };

_2.3 Selectors, Dynamic Linkage, and Polymorphisms__________________________________________________________________________^13

struct Set { const void * class; /* must be first */ ... };

Each of our objects starts with a pointer to its own type description, and through this type description we can locate type-specific information for the object: .size is the length that new() allocates for the object; .ctor points to the constructor called by new() which receives the allocated area and the rest of the argument list passed to new() originally; .dtor points to the destructor called by delete() which receives the object to be destroyed; .clone points to a copy function which receives the object to be copied; and .differ points to a function which compares its object to something else.

Looking down this list, we notice that every function works for the object through which it will be selected. Only the constructor may have to cope with a partially initialized memory area. We call these functions methods for the objects. Calling a method is termed a message and we have marked the receiving object of the message with the parameter name self. Since we are using plain C functions, self need not be the first parameter.

Many objects will share the same type descriptor, i.e., they need the same amount of memory and the same methods can be applied to them. We call all objects with the same type descriptor a class ; a single object is called an instance of the class. So far a class, an abstract data type, and a set of possible values together with operations, i.e., a data type, are pretty much the same.

An object is an instance of a class, i.e., it has a state represented by the memory allocated by new() and the state is manipulated with the methods of its class. Conventionally speaking, an object is a value of a particular data type.

2.3 Selectors, Dynamic Linkage, and Polymorphisms

Who does the messaging? The constructor is called by new() for a new memory area which is mostly uninitialized:

void * new (const void * _class, ...) { const struct Class * class = _class; void * p = calloc(1, class —> size); assert(p);

  • (const struct Class **) p = class; if (class —> ctor) { va_list ap; va_start(ap, _class); p = class —> ctor(p, & ap); va_end(ap); } return p; }

The existence of the struct Class pointer at the beginning of an object is extremely important. This is why we initialize this pointer already in new() :

(^14) ___________________________________________________________________________2 Dynamic Linkage — Generic Functions

p •

object

class size ctor dtor clone differ struct Class

The type description class at the right is initialized at compile time. The object is created at run time and the dashed pointers are then inserted. In the assignment

  • (const struct Class **) p = class;

p points to the beginning of the new memory area for the object. We force a conversion of p which treats the beginning of the object as a pointer to a struct Class and set the argument class as the value of this pointer.

Next, if a constructor is part of the type description, we call it and return its result as the result of new() , i.e., as the new object. Section 2.6 illustrates that a clever constructor can, therefore, decide on its own memory management.

Note that only explicitly visible functions like new() can have a variable parame- ter list. The list is accessed with a va_list variable ap which is initialized using the macro va_start() from stdarg.h. new() can only pass the entire list to the construc- tor; therefore, .ctor is declared with a va_list parameter and not with its own vari- able parameter list. Since we might later want to share the original parameters among several functions, we pass the address of ap to the constructor — when it returns, ap will point to the first argument not consumed by the constructor.

delete() assumes that each object, i.e., each non-null pointer, points to a type description. This is used to call the destructor if any exists. Here, self plays the role of p in the previous picture. We force the conversion using a local variable cp and very carefully thread our way from self to its description:

void delete (void * self) { const struct Class ** cp = self; if (self && * cp && (* cp) —> dtor) self = (* cp) —> dtor(self); free(self); }

The destructor, too, gets a chance to substitute its own pointer to be passed to free() by delete(). If the constructor decides to cheat, the destructor thus has a chance to correct things, see section 2.6. If an object does not want to be deleted, its destructor would return a null pointer.

All other methods stored in the type description are called in a similar fashion. In each case we have a single receiving object self and we need to route the method call through its descriptor: