• No se han encontrado resultados

2. PARTE II: EXPERIENCIAS DE LA ECONOMÍA DE LA

2.4 Cooperativas integrales

2.4.5 Sistema público cooperativo integral

Types such asint,float, andcharare calledatomictypes because we think of their values as single entities only, not something we wish to subdivide. Computer languages like C++, however, provide tools such as arrays, classes, and pointers with which we can build new types, calledstructuredtypes. A single value of a structured type (that is, a single element of its set) is a structured object such as a contiguous stack. A value of a structured type has two ingredients: It is made up ofcomponentelements, and there is astructure, a set of rules for putting the components together.

For our general point of view we shall use mathematical tools to provide the rules for building up structured types. Among these tools are sets, sequences, and functions. For the study of lists of various kinds the one that we need is thefinite

building types

sequence, and for its definition we use mathematical induction.3 A definition by

induction (like a proof by induction) has two parts: First is an initial case, and second is the definition of the general case in terms of preceding cases.

Definition Asequence of length0 is empty. Asequence of lengthn≥1 of elements from a setT is an ordered pair(Sn−1, t)whereSn−1 is a sequence of lengthn−1 of

elements fromT, andtis an element ofT.

From this definition we can build up longer and longer sequences, starting with the empty sequence and adding on new elements fromT, one at a time.

From now on we shall draw a careful distinction between the wordsequential, meaning that the elements form a sequence, and the wordcontiguous, which we take to mean that the elements have adjacent addresses in memory. Hence we shall sequential versus

contiguous be able to speak of asequentiallist in acontiguousimplementation.

3. Abstract Data Types

The definition of a finite sequence immediately makes it possible for us to attempt a definition of

list definition of a list: alistof items of a typeT is simply a finite sequence of elements of the setT.

Next we would like to define a stack, but if you consider the definitions, you will realize that there will be nothing regarding the sequence of items to distin-

50

guish these structures from a list. The primary characteristic of a stack is the set of operationsormethods by which changes or accesses can be made. Including a statement of these operations with the structural rules defining a finite sequence, we obtain

Definition Astackof elements of typeT is a finite sequence of elements ofT, together with the following operations:

1. Createthe stack, leaving it empty. 2. Test whether the stack isEmpty.

3. Pusha new entry onto the top of the stack, provided the stack is not full. 4. Popthe entry off the top of the stack, provided the stack is not empty. 5. Retrieve theTopentry from the stack, provided the stack is not empty. Note that this definition makes no mention of the way in which the abstract data type stack is to be implemented. In the coming chapters we will study several different implementations of stacks, and this new definition fits any of these im- plementations equally well. This definition produces what is called an abstract data type, often abbreviated asADT. The important principle is that the definition abstract data type

of any abstract data type involves two parts: First is a description of the way in which the components are related to each other, and second is a statement of the operations that can be performed on elements of the abstract data type.

2.5.3 Refinement of Data Specification

Now that we have obtained such a general definition of an abstract data type, it is time to begin specifying more detail, since the objective of all this work is to find general principles that will help with designing programs, and we need more detail to accomplish this objective.

There is, in fact, a close analogy between the process of top-down refinement of algorithms and the process of top-down specification of data structures that we have now begun. In algorithm design we begin with a general but precise statement top-down specification

Section 2.5 Abstract Data Types and Their Implementations

75

program. In data specification we begin with the selection of the mathematical concepts and abstract data types required for our problem and slowly specify more detail until finally we can implement our data structures as classes.

The number of stages required in this specification process depends on the stages of refinement

application. The design of a large software system will require many more decisions than will the design of a single small program, and these decisions should be

51 taken in several stages of refinement. Although different problems will require

different numbers of stages of refinement, and the boundaries between these stages sometimes blur, we can pick out four levels of the refinement process.

1. On theabstractlevel we decide how the data are related to each other and what conceptual

operations are needed, but we decide nothing concerning how the data will actually be stored or how the operations will actually be done.

2. On thedata structureslevel we specify enough detail so that we can analyze algorithmic

the behavior of the methods and make appropriate choices as dictated by our problem. This is the level, for example, at which we might choose a contiguous structure where data is stored in an array.

3. On theimplementationlevel we decide the details of how the data structures programming

will be represented in computer memory.

4. On theapplicationlevel we settle all details required for our particular appli- cation, such as names for variables or special requirements for the operations imposed by the application.

The first two levels are often calledconceptualbecause at these levels we are more concerned with problem solving than with programming. The middle two levels can be calledalgorithmicbecause they concern precise methods for representing data and operating with it. The last two levels are specifically concerned with

programming.

Our task in implementing a data structure in C++ is to begin with conceptual information, often the definition of an ADT, and refine it to obtain an implemen- tation as a C++ class. The methods of the C++ class correspond naturally to the operations of the ADT, while the data members of the C++ class correspond to the physical data structure that we choose to represent our ADT. In this way, the process of moving from an abstract ADT, to a data structure, and then on to an implementation leads directly to a C++ class definition.

Let us conclude this section by restating its most important principles as pro- gramming precepts:

Programming Precept

Let your data structure your program.

Refine your algorithms and data structures at the same time.

Programming Precept

Once your data are fully structured, your algorithms should almost write themselves.

Exercises 2.5

E1. Give a formal definition of the term extended stackas used inExercise E1of Section 2.2.

E2. In mathematics theCartesian productof setsT1, T2, . . . , Tnis defined as the set of alln-tuples(t1, t2, . . . , tn), where ti is a member of Ti for alli,1≤i≤n.

Use the Cartesian product to give a precise definition of a class.

POINTERS AND PITFALLS

1. Use data structures to clarify the logic of your programs.

52

2. Practice information hiding and encapsulation in implementing data struc- tures: Use functions to access your data structures, and keep these in classes separate from your application program.

3. Postpone decisions on the details of implementing your data structures as long as you can.

4. Stacks are among the simplest kind of data structures; use stacks when possible. 5. In any problem that requires a reversal of data, consider using a stack to store

the data.

6. Avoid tricky ways of storing your data; tricks usually will not generalize to new situations.

7. Be sure to initialize your data structures.

8. In designing algorithms, always be careful about the extreme cases and handle them gracefully. Trace through your algorithm to determine what happens in extreme cases, particularly when a data structure is empty or full.

9. Before choosing implementations, be sure that all the data structures and their associated operations are fully specified on the abstract level.

REVIEW QUESTIONS

1. What is the standard library?

Documento similar