160 likes | 254 Views
FIFO ADT. version 3b2. MTE_errno.h MTE_type.h FIFO.h FIFO_Test.c FIFO_Imp_U_L1.c FIFO_Imp_B_A1.c. Imperative Programming, B. Hirsbrunner, diuf.unifr.ch/pai/ip. Session 9 – 17 November 2009. MTE_errno. boolean MTE_debug; // "application level" debugger
E N D
FIFO ADT version 3b2 • MTE_errno.h • MTE_type.h • FIFO.h • FIFO_Test.c • FIFO_Imp_U_L1.c • FIFO_Imp_B_A1.c Imperative Programming, B. Hirsbrunner, diuf.unifr.ch/pai/ip Session 9 – 17 November 2009
MTE_errno • boolean MTE_debug; // "application level" debugger • int MTE_errno; // place where the error numbers go • // Numerical values of the error numbers. • #define MTE_ERROR_not_yet_implemented 1 • #define MTE_ERROR_out_of_memory 2 • #define MTE_ERROR_ADT_is_empty 11 • #define MTE_ERROR_ADT_is_full 12 • #define MTE_ERROR_ADT_is_not_existing 13 • #define MTE_ERROR_ADT_element_is_not_existing 14 Lines 11-22
MTE_type.h • #define TRUE 1 • #define FALSE 0 • typedefint boolean; • typedefstruct std_elt { • int key; • char data[60]; // fixed array size for simplicity reasons !!! • } std_elt; Lines 36-43
FIFO.h • #define FIFO_SPECIF_VERSION "FIFO Specification: version 3b2, 31 May 2007" • typedefvoid *fifo; // private type • int fifo_capacity; // global variable initialized by the user • //--- Constructors • fifo *fifo_create(void); • fifo *fifo_destroy(fifo *f); • //--- Access functions • boolean fifo_is_empty(fifo *f); • boolean fifo_is_existing(fifo *f); • boolean fifo_is_full(fifo *f); • int fifo_size(fifo *f); • char *fifo_impVersion(); • void fifo_consult(fifo *f, std_elt *elt); • void fifo_traverse(fifo *f, void user_fct(std_elt *)); • //--- Manipulator functions • void fifo_put(fifo *f, std_elt *elt); • void fifo_get(fifo *f, std_elt *elt); Lines 67-95
FIFO_Test.c – Usage • void print_usage() • //---------------- • { • printf("ADT FIFO usage:\n"); • printf(" Constructors : c)create d)destroy\n"); • printf(" Access fcts : E)exists e)isEmpty f)isFull s)size v)version\n"); • printf(" : o)consult() t)traverse_display\n"); • printf(" Manipulator fcts: p)put(i,s) g)get T)traverse_add_one\n"); • printf("Test program usage:\n"); • printf(" C)SetADTCapacity(i) D)Debugger(0..1) V)Version H)Help Q)Quit\n"); • printf(" S)SetCurrentADT(0..2) G)GetCurrentADT\n"); • printf(" A)FillADT M)FillMem\n"); • printf(" X)Dealloc put/get Y)Dealloc create..destroy Z)ProcCall\n"); • } Lines 156-169
FIFO_Test.c – Main Loop • while (c = getchar()) { // Main loop of the test program • switch(c) { • case'c': • f = f_a[current_fifo_index] = fifo_create(); • if (MTE_errno != 0) goto error; /*----- ERROR TREATMENT -----*/ • printf("a FIFO has been created\n"); • break; • . . . • default: • print_usage(); • break; • } • while (getchar() != '\n') {} /* skip end of line */ • printf("%s", prompt); • continue; // all is ok, go to the beginning of the main loop • error: // ----- ERROR TREATMENT • while (getchar() != '\n') {} /* skip end of line */ • printf("Error reporting from the test program> Error: %i\n", MTE_errno); • printf("%s", prompt); • MTE_errno = 0; //----- END ERROR TREATMENT • } // End of the main loop of the test program Lines 190-383
FIFO_Test.c – include+global+local+init • #include <stdio.h> // import printf, scanf • #include <stdlib.h> // import rand() • #include "MTE_type.h" • #include "FIFO.h" • #include "MTE_errno.h" // import MTE_debug, MTE_errno, ... • #define FIFO_TEST_VERSION "FIFO Test Program: version 1.3, 31 May 2007" • // user-supplied functions for fifo_traverse • void display(std_elt *elt) { printf("%d %s\n", elt->key, elt->data); } • void add_one(std_elt *elt) { ++elt->key; } • . . . • main() /* Test program for the FIFO ADT */ • { • fifo *f = NULL, *f_a[3] = {NULL,NULL,NULL}; • std_elt elt = {1,"Hello"}; • MTE_errno = 0; // by default, there is no error • fifo_capacity = 10; // default value • . . . Lines 127-188
FIFO_Test.c – Misc: Fill ADT, Fill memory • case'A': // Fill up an FIFO ADT • li = 0; • f = fifo_create(); • while (TRUE) { • fifo_put(f, &elt); • if (MTE_errno != 0) goto error; /*----- ERROR TREATMENT -----*/ • if ( !(++li % grain) ) printf("%d\n", li/grain); • } • break; • case'M': // Fill memory with empty FIFOs • li = 0; • while (TRUE) { • f = fifo_create(); • if (MTE_errno != 0) goto error; /*----- ERROR TREATMENT -----*/ • if ( !(++li % grain) ) printf("%d\n", li/grain); • } • break; Lines 308-325
FIFO_imp_U_L1.c – Includes + Private decl. • #include <stdlib.h> // import malloc, free • #include "MTE_type.h" • #include "MTE_errno.h" • #include "FIFO.h" • //--- Private declarations • typedefstruct fifo_item { // FIFO instance is a linked list of fifo_item • std_elt elt; // element to be stored in the FIFO instance • struct fifo_item *next; // points to the next element, i.e. next head • } fifo_item; • typedefstruct fifo_descr { • fifo_item *head; // points to the head (i.e. first in) element • fifo_item *tail; // points to the tail (i.e. last in) element • int size; // number of elements in the FIFO • } fifo_descr; • #define FIFO_IMP_VERSION "FIFO Implementation: version U_L1 3b2, 31 May 2007" Lines 403-423
FIFO_imp_U_L1.c – create + destroy • fifo *fifo_create(void) { • fifo_descr *fd; • fd = (fifo_descr *) malloc(sizeof(fifo_descr)); • if(fd==NULL) • { • MTE_errno = MTE_ERROR_out_of_memory; • returnNULL; • } • fd->head = NULL; • fd->tail = NULL; • fd->size = 0; • return (fifo *) fd; • } • fifo *fifo_destroy(fifo *f) { • std_elt elt; • while ( !fifo_is_empty(f) ) fifo_get(f, &elt); // free FIFO instance • free( (fifo_descr *) f ); // free FIFO descriptor • returnNULL; // FIFO no longer exists • } Lines 430-457
FIFO_imp_U_L1.c – isEmpty + isFull • boolean fifo_is_empty(fifo *f) • // --------------------------- • { • fifo_descr *fd = (fifo_descr *) f; // casting the generic pointer f • return ( (fd->head == NULL) ? TRUE : FALSE ); • } • boolean fifo_is_full(fifo *f) • // -------------------------- • { • fifo_item *new_item; • new_item = (fifo_item *) malloc(sizeof(fifo_item)); • if(new_item==NULL) returnTRUE; • free(new_item); • returnFALSE; • } Lines 471-490
FIFO_imp_U_L1.c – put • void fifo_put(fifo *f, std_elt *elt) • //---------------------------------- • { • fifo_descr *fd = (fifo_descr *) f; // casting the generic pointer f • fifo_item *new_item; • new_item = (fifo_item *) malloc(sizeof(fifo_item)); • if(new_item==NULL) • { • MTE_errno = MTE_ERROR_out_of_memory; • return; • } • new_item->elt = *elt; • new_item->next = NULL; • if (fd->head == NULL) fd->head = new_item; • if (fd->tail != NULL) fd->tail->next = new_item; • fd->tail = new_item; • ++fd->size; • } Lines 540-560
FIFO_imp_B_A1.c – Includes + Private decl. • #include <stdlib.h> // import malloc, free • #include "MTE_type.h" • #include "MTE_errno.h" • #include "FIFO.h" • //--- Private declarations • typedefstruct fifo_descr { • std_elt *array; // points to the stack instance • int size; // 0 <= size <= fifo_capacity • int head; // pseudo pointer refering to the head • int tail; // pseudo pointer refering to the next free entry • } fifo_descr; • #define FIFO_IMP_VERSION "Fifo Implementation: version B_A1 3b2, 31 May 2007" Lines 600-616
FIFO_imp_B_A1.c – create + destroy • fifo *fifo_create(void) { • fifo_descr *fd; std_elt *array; • fd = (fifo_descr *) malloc(sizeof(fifo_descr)); • if(fd==NULL) { • MTE_errno = MTE_ERROR_out_of_memory; • returnNULL; • } • array = (std_elt *) malloc(fifo_capacity * sizeof(std_elt)); • if(array==NULL) { • MTE_errno = MTE_ERROR_out_of_memory; • free(fd); • returnNULL; • } • fd->array = array; fd->size = 0; fd->head = 0; fd->tail = 0; return (fifo *) fd; • } • fifo *fifo_destroy(fifo *f) { • fifo_descr *fd = (fifo_descr *) f; // casting the generic pointer f • free(fd->array); free(fd); // free fifo_instance and fifo_descriptor • returnNULL; // fifo no longer exists • } Lines 619-660
FIFO_imp_B_A1.c – isEmpty + isFull • boolean fifo_is_empty(fifo *f) • //---------------------------- • { • fifo_descr *fd = (fifo_descr *) f; // casting the generic pointer f • return ((fd->size==0) ? TRUE : FALSE ); • } • boolean fifo_is_full(fifo *f) • //--------------------------- • { • fifo_descr *fd = (fifo_descr *)f; // casting the generic pointer f • return ((fd->size==fifo_capacity) ? TRUE : FALSE ); • } Lines 674-689
FIFO_imp_B_A1.c – put • void fifo_put(fifo *f, std_elt *elt) • //---------------------------------- • { • fifo_descr *fd = (fifo_descr *)f; // casting the generic pointer f • if(fd->size == fifo_capacity) • { • MTE_errno = MTE_ERROR_ADT_is_full; • return; • } • (fd->array)[fd->tail] = *elt; • ++fd->size; • fd->tail = (fd->tail + 1) % fifo_capacity; • } Lines 747-761