CSC265 - Inspection 2
Module interface specifications
-
A Module interface specification (MIS) is a precise, written
description of the required observable behavior of a module
-
The need for standard sections:
There will be many modules.
The specifications will be far easier to read and write if the
information is presented in the same form in each MIS.
-
The standard sections in CSc 265 module interface specifications:
- overview
- constants
- types
- exported functions
- usage example
Module Interface Specification: IntList.h
/********** IntList module---interface specification **********/
/***** overview *****
The IntList module stores a list of integers. List elements
may be added, removed, modified, and retrieved at any position.
*/
/***** constants *****/
/***** types *****/
typedef enum {SUCCESS,MEMORYERROR,RANGEERROR} ILStatus;
/***** exported functions *****/
/* initialize the list to empty */
void ILInit();
/* if i is not in [0..ILSize()]
* return RANGEERROR
* else if memory is available to add an additional element
* add x at position i, shifting elements i,i+1, ... right
* return SUCCESS
* else
* return MEMORYERROR
*/
int ILAdd(int i,int x);
/* if i is in [0..ILSize()-1]
* remove the element at position i, shifting elements i+1,i+2 ... left
* else
* return RANGEERROR
*/
int ILRemove(int i);
/* if i is in [0..ILSize()-1]
* replace the value at position i with x
* else
* return RANGEERROR
*/
int ILSetVal(int i,int x);
/* if i is in [0..ILSize()-1]
* return the value stored at position i
* else
* return RANGEERROR
* Note: Because RANGEERROR is a possible element value, ILGetVal parameters
* should be explictly checked using ILSize
*/
int ILGetVal(int i);
/* return the number of elements in the list */
int ILSize();
Module Implementation: IntList.c
#include
#include "IntList.h"
/*****constants*****/
/*****types*****/
typedef struct item {
int val;
struct item *nextPtr;
struct item *prevPtr;
} item;
/***** local variables *****/
static item *headPtr; /*pointer to the head of the list*/
static int size; /*list size*/
/***** state invariant *****
1. headPtr points to a circular, doubly-linked list of items.
2. There are size items in this list.
3. All of the items allocated by IntList (and not yet freed)
are in this list.
*/
/***** local functions *****/
/* if i in [0..size-1] then
* return the address of ith item
* else
* return NULL
*/
static item* findItem(int i)
{
item *tmpPtr = headPtr;
if (i < 0 || i >= size)
return NULL;
while(tmpPtr && i--)
tmpPtr = tmpPtr->nextPtr;
return tmpPtr;
}
/***** exported functions *****/
void ILInit()
{
headPtr = NULL;
size = 0;
}
int ILAdd(int i,int x)
{
item *tmpPtr,*newPtr;
if (i < 0 || i > size) {
return RANGEERROR;
}
if ((newPtr = (item*) malloc(sizeof(item))) == NULL) {
return MEMORYERROR;
}
newPtr->val = x;
if (size == 0) { /* empty list */
headPtr = newPtr;
newPtr->nextPtr = newPtr;
newPtr->prevPtr = newPtr;
} else { /* non-empty list */
if (i == 0 || i == size) /* adding first or last */
tmpPtr = headPtr;
else /* adding at any other position */
tmpPtr = findItem(i);
newPtr->nextPtr = tmpPtr;
newPtr->prevPtr = tmpPtr->prevPtr;
tmpPtr->prevPtr->nextPtr = newPtr;
tmpPtr->prevPtr = newPtr;
if (i == 0)
headPtr = newPtr;
}
size++;
}
int ILRemove(int i)
{
item *tmpPtr;
if((tmpPtr = findItem(i)) == NULL) {
return RANGEERROR;
} else {
tmpPtr->prevPtr->nextPtr = tmpPtr->nextPtr;
tmpPtr->nextPtr->prevPtr = tmpPtr->prevPtr;
if (i == 0)
if (size == 1)
headPtr = NULL;
else
headPtr = tmpPtr->nextPtr;
free(tmpPtr);
size--;
}
}
int ILSetVal(int i,int x)
{
item *tmpPtr;
if ((tmpPtr = findItem(i)) == NULL) {
return RANGEERROR;
} else
tmpPtr->val = x;
}
int ILGetVal(int i)
{
item *tmpPtr;
if((tmpPtr = findItem(i)) == NULL) {
return RANGEERROR;
}
return(tmpPtr->val);
}
int ILSize()
{
return size;
}