Simple Structure 2
Simple Structure 2
Year 2023-2024.
1
PLAN
Introduction.
objectives.
Initial Loading
Dichotomous search
Logical delete
Insertion
Conclusion
2
Chapter objectives
The aim of our course is to answer the following two questions:
How to define the organisation of the file: separation of fields, records, etc.
the access method: how do you locate a record in the file?
3
Definition
4
Physical location : How information is arranged on the medium.
Linked :
A pointer to the address of the next and previous record is
added to each record. Both access methods (sequential or
direct) are possible.
5
Access method: Means of finding a record on the medium.
Direct : access to the data set via its address - which implies a
commitment to use addressable storage.
6
Type of organisations
7
Example
“TOF” type file (file seen as an array, ordered with fixed-size records) .
The initial load operation consists of constructing an ordered file with n initial records,
leaving a little space in each block. This will help minimize offsets that may be caused
by future insert. Searching for a record is dichotomous (fast).
8
const
b = 30; // maximum block capacity (number of records)
type
Trecor = structure // Record structure :
del : boolean; // Boolean for logical deletion
key : typeanyth;
field2 : typeanyth;
field3 :typeanyth;...
end;
9
var
F : File of Tbloc Buffer buf Header(integer, integer);
/* Description of the file header: The header contains two integer type characteristics.
- the first is used to keep track of the number of blocks used (or the logical number of
the last block of the file) - the second will serve as an insertion counter to be able to
quickly calculate the loading factor, and therefore see if there is a need to reorganize
the file. */
10
Initial loading( namef : charaters; n : integer; u : real )
// u is a real number between 0 and 1 and indicates the desired charge rate at the start
var
e : Trecor;
i,j,k : integerr;
BEGIN
OPEN(F, namef, 'N' ); // a new file
i ← 1; // block number to fill
j ← 1; // record number in the block
write(‘List records in ascending order by key:’ );
FOR k←1 , n
Read ( e );
IF j <= u*b // e.g. if u=0.5, the blocks will be filled up to b/2 records
buf.tab[j] ← e
j ← j+1;
ELSE // j > u*b : buf must be written to disc
buf.NB = j-1;
WriteBlock( F, i, buf );
buf.tab[1] ← e; // the kth record will be placed in the next block, at position 1
i ← i+1;
j ← 2;
ENDIF
ENDFOR
// at the end of the loop, there are still records in buf that have not been saved to disk
buf.NB ← j-1;
WriteBlock( F, i, buf );
// update the header (the number of the last block and the insertion counter)
Ass-header( F, 1, i );
Ass-header( F, 2, n );
Close( F )
FIN // Initial loading 11
The search for a record is dichotomous (quick).
Input : the key to search for and the file's external name.
Output : boolean fd, the block number (i) that contains (c) and the displacement (j)
12
search( c:typeqlq; namef:char; var Fd:boalean; var i,j:enteger )
var
bi, bs, inf, sup : integer;
Fd, stop : boolean;
Begin
Open( F, namef, 'A' );
bs := header( F,1 );
bi : = 1; Fd := false; stop := false;
13
While ( bi <= bs et Not Fd et Not stop )
i := (bi + bs) div 2;
ReadBlock( F, i, buf );
If ( c >= buf.tab[1].key et c <= buf.tab[buf.NB].key )
// dichotomous search within the block (in the buf variable)...
inf := 1; sup := buf.NB;
While inf <= sup and Not Fd
j : = (inf + sup) div 2;
If c = buf.tab[j].cle: Fd:= true
Else
If c < buf.tab[j].cle: sup := j-1
Else inf := j+1
ENDIF
ENDIF
ENDWHILE
IF ( Not Fd )
j := inf
ENDIF
stop := true
14
ELSE // not ( c >= buf.tab[1].key and c <= buf.tab[buf.NB].key )
IF ( c < buf.tab[1].cle )
bs := i-1
ELSE// c > buf.tab[buf.NB].key
bi := i+1
ENDIF
ENDIF
ENDWHILE
IF( Not Fd )
i := bi
ENDIF
close( F )
END
15
Logical delete consists of searching for the record and setting the erase :=true
Delete(c:integer ,namef:char )
var
fd: boolean;
i,j : intger;
BEGIN
Search( c, namef, fd, i, j );
// then logically delete the record
IF ( Fd )
Open( F,name, 'A');
ReadBlock( F, i, buf );
buf.tab[j].erase := True;
WriteBlock( F, i, buf );
Close( F )
ENDIF
END// delete 16
Insert( e:Trecorf; namef:char )
var
Fd : boolean;
i,j,k : integer;
x : Trecord;
BEGIN
Search( e.key, namef, fd, i, j );
If ( Not fd ) // e must be inserted in block i at position j
Open( F,namef, 'A'); // by moving records j, j+1, j+2, ... Down
con nu ← true;
// if i is full, the last record in i must be inserted in i+1
While( continu and i<= header(F,1) ) // if block i+1 is also full, its last record will be
ReadBlock( F, i, buf ); // inserted in block i+2, etc ... so a While loop.
// before shifting, save the last record in a var x
x ← buf.tab[buf.NB];
// shift within buf...
k ← buf.NB;
While k > j
buf.tab[k] ← buf.tab[k-1];
k ← k-1
END WHILE
// insert e in pos j in buf ...
buf.tab[j] ← e;
17
// if buf is not full, reset x to pos NB+1 and stop ...
if ( buf.NB < b )
buf.NB ← buf.NB+1;
buf.tab[buf.NB] ← x;
WriteBlock( F, i, buf );
con nu ← false;
ELSE// if buf is full, x must be inserted in block i+1 at position 1
...
WriteBlock( F, i, buf );
i ← i+1;
j ← 1;
e ← x; // this will be done (the insertion) at the next iteration of the while
ENDIF// not ( buf.NB < b )
ENDWHILE
18
// If we go beyond the file end, add a new block containing a
single record. E
IF i > entete( F, 1 )
buf.tab[1] ← e;
buf.NB ← 1;
WriteBlock( F, i, buf ); Just write a new block in this place
Ass-header( F, 1, i );
ENDIF
Ass-header( F, 2 , header(F,2)+1 );
close( F );
ENDIF
END.
19
4. Conclusion
The file can be thought of as an array of M contiguous blocks. The file may or may
not be ordered.
If it is not ordered, items are added at the end of the file. Access is then sequential. If
it is ordered, adding items causes shifts(offsets). Access is then either dichotomous or
sequential.
20
4. Conclusion
The file can be seen as a linked linear list of blocks, i.e. made up of M non-
contiguous blocks. The file may be ordered or unordered. If it is unordered, elements
are added at the end of the file. If it is ordered, the addition of elements results in the
insertion of new blocks (dynamic allocation). In both cases, access is always
sequential.
If the file is ordered, an initial load is made by filling the blocks to 60-70%. This
saves intra-block offsets.
If the operation of searching for elements is very frequent, it is much better to use an
ordered table with a fixed format. This is, of course, to allow dichotomous
searching. 21
Dichtomios
Search
Initial
Loading
File Organisation
Simple Structure
Logical Delete
Insert
Mind Mapping
22
Reference
https://sites.google.com/a/esi.dz/hidouci/competences-professionnelles/algo2
http://zegour.esi.dz/Publication/Livre2/Partie-sdf/Part2-ss.htm
23