ca i r e n e g a t s on i L az ca u...

13
Luca Lista P r og r a mm az i on e g e n e r i ca P r og r a mm az i on e g e n e r i ca L u ca L i s t a I N F N , S ez i o n e d i N ap o li

Transcript of ca i r e n e g a t s on i L az ca u...

Luc

a L

ista

Prog

ram

maz

ione

gen

eric

aPr

ogra

mm

azio

ne g

ener

ica

Luc

a L

ista

INF

N, Se

zion

e d

i Na

poli

Luc

a L

ista

Prog

ram

maz

ione

gen

eric

aPr

ogra

mm

azio

ne g

ener

ica

Il C

++

for

nisc

e un

met

odo

per

crea

re u

n p

olim

orfism

o

par

amet

rico

. E’

poss

ibile

util

izza

re lo

ste

sso

codi

ce p

er ti

pi

diff

eren

ti: il

tip

o de

lla v

aria

bile

div

enta

un

para

met

ro.

tem

pla

te

<cl

ass

T>

T

m

ax(

T

p1

, T

p2

)

{if

( p

1

< p

2

) r

etu

rn p

2;

retu

rn

p1

;}tem

pla

te

<cl

ass

T>

T

m

ax(

T

p1

, T

p2

)

{if

( p

1

< p

2

) r

etu

rn p

2;

retu

rn

p1

;}

int

ma

in

()

{V

ect

or

v1,

v2

;co

ut

<<

m

ax(1

0,2

0)

<<

e

nd

l;

cou

t<

<

max

(2.6

,1.

0)

<<

e

nd

l;

cou

t<

<

max

(v1

,v2

) <

<

en

dl

;}

int

ma

in

()

{V

ect

or

v1,

v2

;co

ut

<<

m

ax(1

0,2

0)

<<

e

nd

l;

cou

t<

<

max

(2.6

,1.

0)

<<

e

nd

l;

cou

t<

<

max

(v1

,v2

) <

<

en

dl

;}

Mai

n.cc

Mai

n.cc

Per

il tip

o T

dev

e es

sere

de

fini

to l’

oper

ator

e <

Luc

a L

ista

Tem

plat

esT

empl

ates

•L

a st

essa

def

iniz

ione

di f

unz

ione

si p

appl

icar

e a

tipi d

iver

si

tem

pla

te

<cl

ass

T>

T

sqr

(c

on

stT

x)

{ re

tur

n x

*

x }

tem

pla

te

<cl

ass

T>

T

min

(c

on

stT

a,

co

nst

T

b)

{ re

tur

n (

a

< b

?

a

: b

);

} te

mp

lat

e<

cla

ss T

>T

m

ax(

co

nst

T a

, c

on

stT

b

){

retu

rn

(a

>

b ?

a

:

b)

;

}

int

i =

1

, j

= 3

;in

t n

=

m

ax(

3

, 2

)

;

Str

ing

a

=

“M

ela

”,

b =

Pe

ra”;

Str

ing

c

=

ma

x(

a,

b

);

Ma

trix

m

;M

atr

ix m

Sq

r =

sq

r(

m

);

Luc

a L

ista

Sint

assi

Sint

assi

•te

mp

la

te

<

cla

ss identifier

>

fun

cti

on

de

fin

iti

on

•te

mp

la

te

<

cla

ss identifier

> c

lass

d

efi

ni

tio

n

Ogn

i vol

ta c

he n

ella

def

iniz

ione

del

la fu

nzio

ne o

de

lla c

lass

e app

are

iden

tifie

r que

sto

vien

e so

stitu

ito

dal c

ompi

lato

re c

on il

tipo

forn

ito

nella

chi

amat

a.

Ogn

i vol

ta c

he n

ella

def

iniz

ione

del

la fu

nzio

ne o

de

lla c

lass

e app

are

iden

tifie

r que

sto

vien

e so

stitu

ito

dal c

ompi

lato

re c

on il

tipo

forn

ito

nella

chi

amat

a.

La

dich

iara

zion

e e l

’im

plem

enta

zion

e de

l tem

plat

e de

vono

ess

ere

nello

ste

sso

file

ove

il te

mpl

ate

vien

e ut

ilizz

ato

La

dich

iara

zion

e e l

’im

plem

enta

zion

e de

l tem

plat

e de

vono

ess

ere

nello

ste

sso

file

ove

il te

mpl

ate

vien

e ut

ilizz

ato

Luc

a L

ista

Un

ese

mpi

o: lo

sta

ckdi

int

eri

Un

ese

mpi

o: lo

sta

ckdi

int

eri

class

S

tac

k{

pu

blic

:S

tac

k()

{

top

= N

UL

L;}

~S

tac

k()

{;

}vo

idp

us

h

( in

ti

) {

Con

ten

uto

* tm

p=

ne

wC

ont

enu

to(i,to

p

);to

p

=

tm

p;

}in

tp

op

()

{in

t re

t=

top

->

Ge

tVa

l()

;C

onte

nu

to*

tmp

= to

p;

top

=

top

->

Ge

tNe

xt

();

de

lete

tm

p;

ret

urn

re

t;

}p

riva

te

:C

on

te

nut

o*

top;

};cla

ss

Sta

ck

{p

ub

lic:

Sta

ck

()

{to

p

= N

UL

L;}

~S

tac

k()

{;

}vo

idp

us

h

( in

ti

) {

Con

ten

uto

* tm

p=

ne

wC

ont

enu

to(i,to

p

);to

p

=

tm

p;

}in

tp

op

()

{in

t re

t=

top

->

Ge

tVa

l()

;C

onte

nu

to*

tmp

= to

p;

top

=

top

->

Ge

tNe

xt

();

de

lete

tm

p;

ret

urn

re

t;

}p

riva

te

:C

on

te

nut

o*

top;

};

cla

ss

Cont

enu

to {

pu

blic

:C

ont

enut

o

( in

ti,

Co

nt

en

uto

* p

tn

) {

v

al

=i;

ne

xt=

ptn

; }

int

GetV

al

(){

ret

urn

v

al

; }

Co

nt

enut

o*

GetN

ext

()

{r

et

urn

n

ex

t;}

priva

te

:C

on

te

nut

o*

next

;in

t v

al

; };cla

ss

Cont

enu

to {

pu

blic

:C

ont

enut

o

( in

ti,

Co

nt

en

uto

* p

tn

) {

v

al

=i;

ne

xt=

ptn

; }

int

GetV

al

(){

ret

urn

v

al

; }

Co

nt

enut

o*

GetN

ext

()

{r

et

urn

n

ex

t;}

priva

te

:C

on

te

nut

o*

next

;in

t v

al

; };

int

ma

in

()

{S

tack

s

;s.

pu

sh (

1

0 )

;s.

pu

sh (

2

0 )

;co

ut

<<

s.

po

p()

<<

-“

<<

s.

po

p()

;re

turn

0;

};int

ma

in

()

{S

tack

s

;s.

pu

sh (

1

0 )

;s.

pu

sh (

2

0 )

;co

ut

<<

s.

po

p()

<<

-“

<<

s.

po

p()

;re

turn

0;

};

Use

rco

deU

ser

code

>>

20

-10

>>

20

-10

Out

put

Out

put

Luc

a L

ista

Lo

stac

k “ t

empl

izza

to”

Lo

stac

k “ t

empl

izza

to”

tem

pla

te

<c

la

ss T

>cl

ass

S

tack

{p

ub

lic:

Sta

ck

() {

to

p =

NU

LL

;}~

Sta

ck

()

{;}

void

pu

sh (

T

i )

{C

onte

nu

to

<T

>*

tm

p=

ne

wC

on

ten

uto

<T

> (

i,to

p )

;to

p

=

tm

p;

}T

po

p

()

{T

r

et

= t

op

->

Ge

tVa

l(

);C

onte

nu

to

<T

>*

tm

p=

to

p;

top

=

to

p-

>G

etN

ext

()

;d

el

ete

t

mp;

ret

urn

r

et

;}

priva

te

:C

on

te

nu

to<

T>

* to

p;

};tem

pla

te

<c

la

ss T

>cl

ass

S

tack

{p

ub

lic:

Sta

ck

() {

to

p =

NU

LL

;}~

Sta

ck

()

{;}

void

pu

sh (

T

i )

{C

onte

nu

to

<T

>*

tm

p=

ne

wC

on

ten

uto

<T

> (

i,to

p )

;to

p

=

tm

p;

}T

po

p

()

{T

r

et

= t

op

->

Ge

tVa

l(

);C

onte

nu

to

<T

>*

tm

p=

to

p;

top

=

to

p-

>G

etN

ext

()

;d

el

ete

t

mp;

ret

urn

r

et

;}

priva

te

:C

on

te

nu

to<

T>

* to

p;

};

tem

pla

te

<c

la

ss T

>cl

ass

C

on

ten

uto

{p

ub

lic:

Co

nt

en

uto

(

T i,

C

on

te

nu

to*

ptn

) {

val

= i;

n

ext

=

ptn

; }

T

Get

Va

l(

){ r

et

urn

v

al

; }

Co

nt

en

uto

<T

>*

Get

Ne

xt(

) {r

etu

rn

n

ex

t;}

priva

te

:C

on

te

nu

to*

ne

xt;

T

val

; };tem

pla

te

<c

la

ss T

>cl

ass

C

on

ten

uto

{p

ub

lic:

Co

nt

en

uto

(

T i,

C

on

te

nu

to*

ptn

) {

val

= i;

n

ext

=

ptn

; }

T

Get

Va

l(

){ r

et

urn

v

al

; }

Co

nt

en

uto

<T

>*

Get

Ne

xt(

) {r

etu

rn

n

ex

t;}

priva

te

:C

on

te

nu

to*

ne

xt;

T

val

; };

int

ma

in

()

{S

tack

<i

nt

>

s;

s.p

ush

(

10

);

s.p

ush

(

20

);

Sta

ck<

do

ub

le

> s

1;

Sta

ck<

Sh

ap

e*

> s

2;

cou

t <

<

s.p

op

() <

<

“ “

<<

s.p

op

();

retu

rn 0

;};

int

ma

in

()

{S

tack

<i

nt

>

s;

s.p

ush

(

10

);

s.p

ush

(

20

);

Sta

ck<

do

ub

le

> s

1;

Sta

ck<

Sh

ap

e*

> s

2;

cou

t <

<

s.p

op

() <

<

“ “

<<

s.p

op

();

retu

rn 0

;};

Use

rco

deU

ser

code

Luc

a L

ista

Lin

ked

Lis

tL

inke

dL

ist

•C

onte

nito

re d

i ogg

etti

dello

st

esso

tipo

•O

gni n

odo

ha u

n pu

ntat

ore

al s

ucce

ssivo

; l’

ulti

mo

nodo

pun

ta

al p

rim

o, e

chi

ude

la c

aten

a

_n

ext

_n

ext

_n

ext

_d

atu

m

No

de 1

0

_n

ext

_n

ext

_n

ext

_d

atu

m

No

de

_n

ext

_n

ext

_n

ext

_d

atu

m

No

de

11

_la

st

Lis

t

12

Luc

a L

ista

Lin

ked

Lis

t: in

terf

accia

Lin

ked

Lis

t: in

terf

accia

#ifn

de

fLI

ST

_H

#d

efin

eLI

ST

_H

tem

pla

te

<c

las

s T

>

cla

ss

Lis

t{ p

ub

lic:

Lis

t(

);

~

Lis

t()

;vo

id a

pp

end

(T

*x);

T*

op

era

tor

[](

int

) co

ns

t;

in

t l

eng

th

()

const

;pro

tec

ted

:

clas

s N

ode

{

p

ub

li

c:

Nod

e(

T*x

) : _

ne

xt

(0)

, _

datu

m(

x)

{}

N

ode

* _

ne

xt

;

T*

_d

at

um

;

};

No

de

* _

la

st;

int

_le

ng

th;

frie

nd c

las

s Lis

tIt

era

tor

<T

>;

// v

ed

i d

opo...

}; #e

nd

if

dire

ttive

al p

repr

oces

sore

per

evita

re in

clus

ione

mul

tipl

a

Luc

a L

ista

Lin

ked

Lis

t: im

plem

enta

zion

eL

inke

d L

ist:

impl

emen

tazi

one

tem

pl

at

e<

cla

ss

T>

Li

st<

T>

::

Lis

t(

) :

_la

st

(0

),

_l

en

gt

h(0

){} te

mp

la

te

<c

las

s T

>vo

id

Li

st

<T

>:

:a

pp

en

d(

T*

x)

{ i

f(

_l

as

t=

=

0)

{

_

la

st

=

ne

w N

ode

(x

);

_l

as

t-

>_

ne

xt=

_l

as

t;

}

e

lse

{

N

od

e*

fir

st

=

_l

as

t-

>_

ne

xt;

_

la

st

->

_n

ex

t=

n

ew

Nod

e(x

);

_l

as

t=

_

la

st-

>_

ne

xt

;

_

la

st

->

_n

ex

t=

f

irs

t;

}

_

len

gt

h+

+;

}

tem

pl

at

e<

cla

ss

T>

int

Li

st

<T

>:

:l

en

gt

h()

c

on

st

{ r

et

ur

n _

len

gth

; }

tem

pl

at

e<

cla

ss

T>

T*

Li

st

<T

>::

op

er

at

or[

](

in

ti

) co

ns

t{

if

(i >

=

_l

en

gth

|| i <

0)

re

tu

rn

0

;

els

e

{

N

od

e*

no

de

=

_l

as

t-

>_

ne

xt

;

w

hil

e(

i--

>

0)

no

de

=

no

de

->

_n

ex

t;

ret

ur

n

no

de

->

_d

atu

m;

}

} te

mp

la

te

<c

las

s T

>L

is

t<T

>:

:~

Li

st

()

{

if

(_

la

st

==

0

) r

et

ur

n;

wh

il

e(

_la

st

!=

_

las

t-

>_

ne

xt

){

No

de

* la

st

=

_l

as

t;

_l

as

t=

_

la

st

->

_n

ex

t;

de

le

te

l

as

t;

} de

le

te

_la

st

;}

L’u

so d

i[

] è

inef

fici

ente

Luc

a L

ista

Lin

ked

Lis

t: u

soL

inke

dL

ist:

uso

#i

nc

lu

de <

io

st

re

am

>#

in

cl

ud

e "

Lis

t.

h”

in

t m

ai

n(

){

Li

st<

in

t>

l

is

t;

in

ti =

1

0, j =

1

00,

k =

10

00

;

li

st.

ap

pe

nd

(&

i);

li

st.

ap

pe

nd

(&

j);

li

st.

ap

pe

nd

(&

k);

// u

so in

ef

fi

cie

nt

e!

fo

r(i =

0

; i <

3

; i+

+)

{ c

ou

t<

<

*l

is

t[i

];

}

re

tu

rn 0

;}

Luc

a L

ista

Iter

ator

esu

lla

list

aIt

erat

ore

sull

a li

sta

#ifn

de

fL

IST

ITE

RA

TO

R_

H#

de

fine

LIS

TIT

ER

AT

OR

_H

#in

clu

de

"L

ist

.h”

tem

pla

te

<cl

ass

T>

cla

ss

Lis

tIt

era

tor

{ pu

blic

:

Lis

tI

tera

to

r(

con

st

Li

st<

T>

& l

ist)

;

T*

cu

rre

nt

()

con

st

;

bo

ol

ne

xt(

);

void

re

wi

nd

();

priva

te

:

con

st

Lis

t<

T>

*

_lis

t;

L

ist

<T

>::

No

de

* _

cu

rre

nt

;}; #

en

dif

Agg

iung

ere :

frie

nd c

lass

L

istIte

rato

r<

T>

;al

l’he

ader

file

Lis

t.h

!

Luc

a L

ista

Iter

ator

esu

lla

list

aIt

erat

ore

sull

a li

sta

tem

pla

te

<c

las

s T

>L

istIt

era

to

r<

T>

::L

ist

Ite

rat

or

(co

ns

tL

ist

<T

>&

lis

t)

:

_lis

t(&

li

st)

, _

cur

rent

(0)

{} tem

pla

te

<c

las

s T

>T

* L

ist

Ite

rat

or

<T

>::

curr

ent

()

co

nst

{if

(_

cur

ren

t=

= 0

)

re

turn

0;

els

e

ret

urn

_c

urr

ent

->

_d

at

um

; } te

mp

lat

e<

cl

as

s T

>vo

idL

ist

It

er

ato

r<

T>

::

rew

ind

(){

_cu

rr

ent

=

0; }

tem

pla

te

<c

las

s T

>b

oo

l Li

stI

ter

ato

r<

T>

::

ne

xt

()

{

// l

ist

a v

uo

taif

(_

list

->

_la

st=

=

0)

ret

urn

fa

lse

;

if(

_cu

rr

en

t=

= 0

)

{

_c

urr

en

t=

_

list

->

_la

st-

>_

nex

t;

ret

urn

tr

ue

;

}

els

e

{

_c

urr

en

t=

_

cur

ren

t-

>_

ne

xt

;

if(

_cu

rre

nt

!=

_li

st

->

_la

st-

>_

ne

xt

)

retu

rn

tru

e;

el

se

{

retu

rn

fals

e;

} }

}

Luc

a L

ista

Iter

ator

esu

lla

list

aIt

erat

ore

sull

a li

sta

•U

so d

i Lis

te

Lis

tIte

rato

r

#in

clu

de <

ios

tre

am

>#

incl

ud

e "

Lis

t.h

"#

incl

ud

e "

Lis

tItera

to

r.h

int m

ai

n()

{L

ist

<in

t>

l

ist

;in

ti

=

10, j

=

10

0, k

=

1

000;

list

.app

end

(&

i);

list

.app

end

(&

j);

list

.app

end

(&

k);

// u

so i

nef

fic

ient

e!

for

(i

=

0;

i <

3;

i+

+)

{ co

ut

<<

*

lis

t[i]

; }

// i

tera

zio

ne e

ffi

cie

nt

eL

ist

Ite

ra

tor

<in

t>

i

t(l

ist

);

wh

ile

(it

.ne

xt

())

{co

ut

<<

*

(it

.cu

rr

ent

())

<<

end

l;

} retu

rn 0

;

}