450 likes | 584 Views
File Management. Lecture 2. Introduction. Most direct interactions between the user and the computer involves significant use of the file management system of the operating system
E N D
File Management Lecture 2 Prepared by Natalie Rose
Introduction • Most direct interactions between the user and the computer involves significant use of the file management system of the operating system • Most user commands, whether typed into the command line interface (CLI) or selected from a menu or activated with a mouse, are operations on files • It is the file management system software that allows users and programs to manipulate files as logical entities, instead of physical blocks of binary data Prepared by Natalie Rose
Logical View of Files • Whether on a computer or paper, a file is an organised collection of data • A computer file may be • As simple as a single data stream that represents an entire program or • A collection of text data read sequentially or • As complex as a database made up of individual records, each with many fields that could be retrieved randomly • Nearly all data on computers is stored and retrieved as files Prepared by Natalie Rose
Physical View of Files • Physical view of the file is the way in which the data is actually stored • Most commonly, the data is stored in physical blocks on a disk • The blocks are usually of fixed size, for example 512 bytes • One file may spread in more than one physical blocks – as a sequence of blocks • The physical view may look very different from the logical view Prepared by Natalie Rose
Accessing Files • Much of user data is logically represented in record form • Data files whose records are always retrieved in sequence form the beginning are known as sequential files • Data in some other file known as random access files can be retrieved in a random sequence • They are also known as relative access files as the location is frequently specified relative to the beginning of the file • One common method of retrieving records sequentially uses one field as the key field as an index to identify a record Prepared by Natalie Rose
Accessing Files Contd. • Indexing provides an additional means for accessing specific records in a file • A file may have multiple indices, each representing a different way of viewing data • A telephone list may be indexed by address, by name or by phone number for example • The index provides a pointer that can be used to locate specific records • An index is usually small enough to be kept in memory for faster access Prepared by Natalie Rose
Sequential Access Starts 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Prepared by Natalie Rose
Direct Access 0 1 2 3 4 5 6 7 8 9 Starts 10 11 12 13 14 15 Prepared by Natalie Rose
Indexed-Sequential Access 0 0 1 2 3 4 5 6 7 8 Starts 9 10 10 11 12 13 14 15 Prepared by Natalie Rose
Hash Table • Separate chaining - chain them up… Prepared by Natalie Rose
Open Addressing Prepared by Natalie Rose
Question: Hashing • Given input {427, 132, 617, 419, 434, 967, 198, 254, 821} and a hash function h(x) = x mod 10, show the resulting hash tables using: • Separate chaining • Linear Probing, with probing function F(i)=i • (i.e, hi(x)= h(x) + i) • Quadratic Probing, with probing function F(i)=i2 • (i.e, hi(x)= h(x) + i2) Ref: 1998a3 Prepared by Natalie Rose
Answer 1 - Chaining • {427,132, 617, 419, 434, 967, 198, 254, 821} • h(x) = x mod 10 value 0 1 2 3 4 5 6 7 8 9 Prepared by Natalie Rose
Answer 1 - Chaining • {427, 132, 617, 419, 434, 967, 198, 254, 821} • h(x) = x mod 10 value 0 1 2 3 4 5 6 7 427 8 9 Prepared by Natalie Rose
Answer 1 - Chaining • {427, 132, 617, 419, 434, 967, 198, 254, 821} • h(x) = x mod 10 value 0 1 2 132 3 4 5 6 7 427 8 9 Prepared by Natalie Rose
Answer 1 - Chaining • {427, 132, 617, 419, 434, 967, 198, 254, 821} • h(x) = x mod 10 value 0 1 2 132 3 4 434 5 6 7 617 427 8 9 419 Prepared by Natalie Rose
Answer 1 - Chaining • {427, 132, 617, 419, 434, 967, 198, 254, 821} • h(x) = x mod 10 value 0 1 2 132 3 4 434 5 6 617 427 7 8 9 419 Prepared by Natalie Rose
Answer 1 - Chaining • {427, 132, 617, 419, 434, 967, 198, 254, 821} • h(x) = x mod 10 value 0 821 1 2 132 3 4 254 434 5 6 967 617 427 7 8 198 9 419 Prepared by Natalie Rose
Answer 2 - Linear Probing • {427,132, 617, 419, 434, 967, 198, 254, 821} • h(x) = x mod 10 • hi(x)= h(x) + i value 0 1 2 3 4 5 6 7 8 9 Prepared by Natalie Rose
Answer 2 - Linear Probing • {427, 132, 617, 419, 434, 967, 198, 254, 821} • h(x) = x mod 10 • hi(x)= h(x) + i value 0 1 2 132 3 4 5 6 7 427 8 9 Prepared by Natalie Rose
Answer 2 - Linear Probing • {427, 132, 617, 419, 434, 967, 198, 254, 821} • h(x) = x mod 10 • hi(x)= h(x) + i value 0 1 2 132 3 4 5 6 7 427 8 617 9 Prepared by Natalie Rose
Answer 2 - Linear Probing • {427, 132, 617, 419, 434, 967, 198, 254, 821} • h(x) = x mod 10 • hi(x)= h(x) + i value 0 1 2 132 3 4 434 5 6 7 427 8 617 9 419 Prepared by Natalie Rose
Answer 2 - Linear Probing • {427, 132, 617, 419, 434, 967, 198, 254, 821} • h(x) = x mod 10 • hi(x)= h(x) + i value 967 % 10 = 7 i.e h(x) = 7 H0(x)=7 % 10 = 7 occupied H1(x)=(7+1)%10 = 8 occupied H2(x)=(7+2)% 10 = 9 occupied H3(x)=(7+3) % 10 = 0 ok! 0 967 1 2 132 3 4 434 5 6 7 427 8 617 9 419 Prepared by Natalie Rose
Answer 2 - Linear Probing • {427, 132, 617, 419, 434, 967, 198, 254, 821} • h(x) = x mod 10 • hi(x)= h(x) + i value 0 967 1 198 And so on… 2 132 3 821 4 434 5 254 6 7 427 8 617 9 419 Prepared by Natalie Rose
Answer 3 - Quadratic Probing • {427,132, 617, 419, 434, 967, 198, 254, 821} • h(x) = x mod 10 • hi(x)= h(x) + i2 value 0 1 2 3 4 5 6 7 8 9 Prepared by Natalie Rose
Answer 3 - Quadratic Probing • {427, 132, 617, 419, 434, 967, 198, 254, 821} • h(x) = x mod 10 • hi(x)= h(x) + i2 value 0 1 2 132 3 4 5 6 7 427 8 9 Prepared by Natalie Rose
Answer 3 - Quadratic Probing • {427, 132, 617, 419, 434, 967, 198, 254, 821} • h(x) = x mod 10 • hi(x)= h(x) + i2 value 0 1 2 132 3 4 434 5 6 7 427 8 617 9 419 Prepared by Natalie Rose
Answer 3 - Quadratic Probing • {427, 132, 617, 419, 434, 967, 198, 254, 821} • h(x) = x mod 10 • hi(x)= h(x) + i2 value 967 % 10 = 7 i.e h(x) = 7 H0(x)=7 % 10 = 7 occupied H1(x)=(7+12)%10 = 8 occupied H2(x)=(7+22)% 10 = 1 OK! 0 1 967 2 132 3 4 434 5 6 7 427 8 617 9 419 Prepared by Natalie Rose
Answer 3 - Quadratic Probing • {427, 132, 617, 419, 434, 967, 198, 254, 821} • h(x) = x mod 10 • hi(x)= h(x) + i2 value And so on… 0 821 1 967 2 132 3 198 4 434 5 254 6 7 427 8 617 9 419 Prepared by Natalie Rose
Double Hashing Table size = 10 elements Hash1(key) = key % 10 Hash2(key) = 7 – ( key % 7 ) Insert Keys: 89, 18, 49, 58, 69 HashKey(89) = 89 % 10 = 9 HashKey(18) = 18 % 10 = 1 HashKey(49) = 49 % 10 = 9 a collision! = (7 – (49 % 7)) = (7 – (0)) = 7 positions from [9] Prepared by Natalie Rose
Double Hashing Insert Keys: 58, 69 HashKey(58) = 58 % 10 = 8 a collision! = (7 – (58 % 7)) = (7 – (2)) = 5 positions from [8] HashKey(69) = 69 % 10 = 9 a collision! = (7 – (69 % 7)) = (7 – (6)) = 1 position from [9] Prepared by Natalie Rose
Rehashing Once the hash table gets too full, the running time for operations will start to take too long and may fail. To solve this problem, a table at least twice the size of the original will be built and the elements will be transferred to the new table. Of course, the new size of the hash table: • should also be prime • will be used to calculate the new insertion spot (hence the name rehashing). Prepared by Natalie Rose
Stack Stack: Last In, First Out Push or Add A to the list A Prepared by Natalie Rose
Stack Stack: Last In, First Out Push or Add B to the list B A Prepared by Natalie Rose
Stack Stack: Last In, First Out Push or Add C to the list C B A Prepared by Natalie Rose
Stack Stack: Last In, First Out Pop an element from the list C B A Prepared by Natalie Rose
Stack Stack: Last In, First Out Pop an element from the list B A Prepared by Natalie Rose
2 1 0 A Queue: Queue: First In, First Out Enqueue or Add A to the list Prepared by Natalie Rose
2 1 0 A B Queue: Queue: First In, First Out Enqueue or Add B to the list Prepared by Natalie Rose
2 1 0 B A C Queue: Queue: First In, First Out Enqueue or Add C to the list Prepared by Natalie Rose
2 1 0 B A C Queue: Queue: First In, First Out Dequeue or remove an element from the list Prepared by Natalie Rose
2 1 0 B C Queue: Queue: First In, First Out Dequeue or remove an element from the list Prepared by Natalie Rose
Anatomy of a Program • The project source for a C++ program: #include <iostream> using namespace std; int main() { cout << “No more Soap Operas. Please.” << endl; return EXIT_SUCCESS; } Prepared by Natalie Rose
Headers • The difference between new and old compilers is header format: #include <iostream.h> int main () { cout << “this is old style”; return 0; } #include <iostream> using namespace std; int main () { cout << “this is new style”; return EXIT_SUCCESS; } traditional C++ Prepared by Natalie Rose modern C++
Executing Program • If you are using DEV-C++ • You need to Compile program first • Then click on Run Prepared by Natalie Rose