6data structure design (データ構造の設計) data structure is one of the most important...

26
6 Data structure design デデデデデデデデデ () Data structure is one of the most important aspects of a program: Program = Data Structure + Algorithm

Upload: elijah-gallagher

Post on 14-Dec-2015

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 6Data structure design (データ構造の設計) Data structure is one of the most important aspects of a program: Program = Data Structure + Algorithm

6 Data structure design(データ構造の設計)

Data structure is one of the most important aspects of a program:

Program = Data Structure + Algorithm

Page 2: 6Data structure design (データ構造の設計) Data structure is one of the most important aspects of a program: Program = Data Structure + Algorithm

A

B C D

B1 B2 C1

D1 D2

a1

a2 a3

a4

a5

a6

a7

a8

In a Structure Chart there is no definition of data-items involved. For example, what do those arguments mean? (次の構造図で使われた引数は、定義されていない)

Page 3: 6Data structure design (データ構造の設計) Data structure is one of the most important aspects of a program: Program = Data Structure + Algorithm

RegisterItems

RegisterOne Item

RegisterItem Name

RegisterItem Price

RegisterItem Shop

RegisterItem Date

RegisterAs Food

RegisterAs Others

*

In a Jackson Structure Diagram there is no definition of data-items involved. (次の Jackson Structure 図で使われたデータ項目は定義されていない)

Page 4: 6Data structure design (データ構造の設計) Data structure is one of the most important aspects of a program: Program = Data Structure + Algorithm

total = 0;foodTotal = 0;clothTotal = 0;othersTotal = 0;

End of expenses-file?

Read item

is-Food(item)?yes no

no yes

is-Cloth(item)?foodTotal += item.price

clothTotal += item.price othersTotal += item.price

Make the next item ready

Output foodTotal;Output clothTotal;Output othersTotal;

Output total

yes no

In a flowchart there is no definition of data-items. E.g., expenses-file.

Page 5: 6Data structure design (データ構造の設計) Data structure is one of the most important aspects of a program: Program = Data Structure + Algorithm

Design of data structures include the following  aspects: (データ構造の定義には、次のことを定義すべきだ)Define types and variables. (型と変数の定義)Determine how the variables are used (e.g., are they global or local? do they hold internal or external data?) (変数の使い方を決まる)

Data structures involved in design should be defined in the form of being easily transformed into corresponding data structures in programming languages. (設計書に使われたデータ項目は、プログラミング言語で実装しやすい形で定義することが必要である)

Page 6: 6Data structure design (データ構造の設計) Data structure is one of the most important aspects of a program: Program = Data Structure + Algorithm

6.1 Defining types and variablesFirst, we need to understand the following points about

programs:Programs process directly variables denoting useful data. A variable has the features:

It corresponds to a memory unit.It has a name.It has a value.

Every variable must be declared with a type, indicating the range of the values the variable can take (e.g., x: T).A type is a set of values together with a set of operations:

Type = set of values + set of operations where the set of operations can be a single operation or

empty. (型は値の集合と操作の集合)

Page 7: 6Data structure design (データ構造の設計) Data structure is one of the most important aspects of a program: Program = Data Structure + Algorithm

int = {…, -1, 0, 1, …} + {+, -, *, /, …}

values operations

real = {…, -1.5, -1.0, 0, 1.0, 1.5, …} + {+, -, *, /, …}

values operations

WeekDays = {MONDAY, TUESDAY, WEDNESDAY,

THURSDAY, FRIDAY} + { }

values operations

Examples of types: (型の事例)

Page 8: 6Data structure design (データ構造の設計) Data structure is one of the most important aspects of a program: Program = Data Structure + Algorithm

A notation for type definitions

The design of data structures in the design phase should focus on the issues:

The values of elementary data items.

The member data items and their values of composite data items.

The structure of organizing data items.

By following this principle, a notation for defining types is

introduced next. This notation is part of the Structured

Object-Oriented Formal Language (SOFL), which can be

used for both abstract and detailed design.

Page 9: 6Data structure design (データ構造の設計) Data structure is one of the most important aspects of a program: Program = Data Structure + Algorithm

Basic types (built-in types)

nat0: {0, 1, 2, …}

nat: {1, 2, 3, …}

int: {…, -1, 0, 1, …}

real: {…, -1.5, -1.0, 0, 1.0, 1.5, …}

string: {s | s is a sequence of characters}

enumeration: {A1, A2, …, An} (e.g., {DOG, CAT,

COW, HORSE})

subset of nat0: e.g., 1..8 (= {1, 2, …, 8})

9..100 (= {9, 10, …, 100})

and so on.

Page 10: 6Data structure design (データ構造の設計) Data structure is one of the most important aspects of a program: Program = Data Structure + Algorithm

The form of defining a type

A = Texp

where Texp is a type expression.

For example,

Name = string declares that every name in type Name is a string

of characters.

Page 11: 6Data structure design (データ構造の設計) Data structure is one of the most important aspects of a program: Program = Data Structure + Algorithm

Composite type

In general, a composite type A is defined as follows:

A = composed of

f1: Type-1

f2: Type-2

fn: Type-n

end

where f1, f2, …, fn are called fields of type A.

Page 12: 6Data structure design (データ構造の設計) Data structure is one of the most important aspects of a program: Program = Data Structure + Algorithm

ExampleItem = composed of name: Name price: real shop: Name date: Date end; /* Item is a composite type */Date = composed of day: 1..31 month: 1..12; year: nat0 end /* Date is another composite type */

Page 13: 6Data structure design (データ構造の設計) Data structure is one of the most important aspects of a program: Program = Data Structure + Algorithm

Sequence typesA sequence type is an abstract representation of the

concrete data types available in programming languages, such as array, vector, linked list, and file.

Two forms of sequence type definitions:(1) A = seq of Type-1(2) A1 = seq[n] of Type-1 (1) states that A is a set of sequences, each consisting of a

list of data items in type Type-1. (2) states that A1 is a set of sequences, each consisting of a list of at most n data items in Type-1. That is, every sequence in type A1 has at most n data items.

Page 14: 6Data structure design (データ構造の設計) Data structure is one of the most important aspects of a program: Program = Data Structure + Algorithm

ExamplesItem-List = seq of Item; /* illustration: Item-List = { [ ], [a], [a1, a2], [b1, b2, b3], …, [c1, c2, …, cn] } /*Item = composed of name: Name price: real shop: Name date: Date end; /* Item is a composite type defined before*/

Page 15: 6Data structure design (データ構造の設計) Data structure is one of the most important aspects of a program: Program = Data Structure + Algorithm

Class = seq[60] of Student;

/* illustration:

Class = {

[ ], [s1], [s1, s2], …, [s1, s2, …, s60]

}

*/

where Student is supposed to have been defined before.

Page 16: 6Data structure design (データ構造の設計) Data structure is one of the most important aspects of a program: Program = Data Structure + Algorithm

Variable declarations

The form of a variable declaration:

a, b, c: Type-1; /*Which has no difference in

semantics from the variable

declaration: Type-1 a, b, c; */

or

a: Type-1;

b: Type-2;

c: Type-3;

Page 17: 6Data structure design (データ構造の設計) Data structure is one of the most important aspects of a program: Program = Data Structure + Algorithm

Examples of variable declarations

(1) a: nat0; /* a = 0, a = 1, a = 2, or a = 10 … */

(2) a: nat; /* a = 1, a = 2, or a = 10, … */

(3) a: int; /*a = -1, a = 0, or a = 1, … */

(4) a: string; /*a = “Hosei”, a = “University, … */

(5) a: WeekDays; /*a = MONDAY, a =

TUESDAY, … */

(6) a: Item; /*field select: a.f, where f is a field of a.

for example, a.name, a.price, a.shop, a.date */

Page 18: 6Data structure design (データ構造の設計) Data structure is one of the most important aspects of a program: Program = Data Structure + Algorithm

(7) a: Item-List; /*a = [b1, b2, …, bn], … where

b1, b2, …, bn are variables of

composite type Item.

Simple operations: application: a(i), where i is

the index of sequence a.

for example, a(1) = b1, a(2) = b2, a(n) = ?

length: len(a),

for example, len(a) = n. */

Page 19: 6Data structure design (データ構造の設計) Data structure is one of the most important aspects of a program: Program = Data Structure + Algorithm

6.2 Determining how the variables are used

(変数の使い方が決まる) When we declare variables with types, we must try

to understand how they will be potentially used in

the program.

Two aspects are important: (二つのポイントが重要

である)Global and local variables?

Representation of internal or external data?

Page 20: 6Data structure design (データ構造の設計) Data structure is one of the most important aspects of a program: Program = Data Structure + Algorithm

6.2.1 Global and local variables

Description 1: a global variable in a module (e.g., a single Java class) is a variable that can be accessed directly by different operations (e.g., methods in a single Java class).

Description 2: a local variable is a variable that can only be accessed by a single operation; and after the termination of an execution of the operation this variable will no longer exist (e.g., variables declared inside a method of a Java class).

Page 21: 6Data structure design (データ構造の設計) Data structure is one of the most important aspects of a program: Program = Data Structure + Algorithm

An abstract example

module A a: int; b: int; c: string;

end-module;

operation P1(x: int) tem: int; begin tem := a; a := x + b; b := a; … end-op;

operation P2(y: string)

begin

if c = y

then

b := a * b;

else

b := a + b

end-op;

Page 22: 6Data structure design (データ構造の設計) Data structure is one of the most important aspects of a program: Program = Data Structure + Algorithm

A specific example in Java//Scope.java

import java.awt.Graphics;

import java.applet.Applet;

public class Scope extends Applet {

int x = 1; //instance variable, global variable

public void paint(Graphics g)

{

int x = 5; //local variable, with the same name as the global variable.

System.out.println(“the initial value of x in paint” + x);

a();

b();

System.out.println(“the final value of x in paint” + x); //the same as the initial x.

}

Page 23: 6Data structure design (データ構造の設計) Data structure is one of the most important aspects of a program: Program = Data Structure + Algorithm

void a() {

int y = 0; //local variable

y = x; //the initial value of x is held in y, x is used as a global variable

++x;

x = x * x;

System.out.println(“the initial value of x in a” + y);

System.out.println(“the final value of x in a” + x);

}

void b() {

System.out.println(“the initial value of global variable x in b” + x);

x *= 10; //x is used as a global variable

System.out.println(“the final value of x in b” + x);

}

}

Page 24: 6Data structure design (データ構造の設計) Data structure is one of the most important aspects of a program: Program = Data Structure + Algorithm

6.2.2 Variables representing internal and external data

( 内部変数と外部変数)Usually, files are used to hold external data and all other data structures

are used to hold internal data, such as array, vector, and linked list.

External data are the data independent of the program system. This kind of data is expected to be kept for future use, either by the same program or by other programs. For example, files are usually used to hold such kinds of data.

Internal data means the data that exist only during executions of the program system. This kind of data is usually used by the program before its termination, and will disappear after the termination of the program.

Page 25: 6Data structure design (データ構造の設計) Data structure is one of the most important aspects of a program: Program = Data Structure + Algorithm

How to distinguish variables representing external data and internal

data(内部変数と外部変数の区別)

Definition A variable representing external data is called external variable, and a variable representing internal data is called internal variable.

A way to distinguish external and internal variables is to mark external variables with a special symbol #.

Page 26: 6Data structure design (データ構造の設計) Data structure is one of the most important aspects of a program: Program = Data Structure + Algorithm

Specifically speaking, suppose f1 is expected to hold external data, its declaration takes the form:

#f1: Type-1 where Type-1 is usually a sequence type.

Example: #item-list: Item-List where Item-List is defined as: Item-List = seq of Item