Abstract Data Types

COMP2521 ♢ Abstract Data Types ♢ [0/36]
❖ Abstract Data Types

A data type is ...

An abstract data type is ... E.g. do you know what a (FILE *) looks like? do you want/need to know?
COMP2521 ♢ Abstract Data Types ♢ [1/36]
❖ DTs, ADTs, GADTs

We want to distinguish ...

DT = (non-abstract) data type   (e.g. C strings)

ADT = abstract data type   (e.g. C files) GADT = generic (polymorphic) abstract data type
COMP2521 ♢ Abstract Data Types ♢ [2/36]
❖ Interface/Implementation

ADT interface provides

ADT implementation gives
COMP2521 ♢ Abstract Data Types ♢ [3/36]
❖ Collections


Many of the ADTs we deal with ...

Collections may be categorised by ...
COMP2521 ♢ Abstract Data Types ♢ [4/36]
❖ ... Collections

Collection structures:

[Diagram:Pics/structures.png]

COMP2521 ♢ Abstract Data Types ♢ [5/36]
❖ ... Collections

Or even a hybrid structure like ...

[Diagram:Pics/structures2.png]

COMP2521 ♢ Abstract Data Types ♢ [6/36]
❖ ... Collections

Or this ...

[Diagram:Pics/structures3.png]

COMP2521 ♢ Abstract Data Types ♢ [7/36]
❖ ... Collections

Or this ...

[Diagram:Pics/structures4.png]

COMP2521 ♢ Abstract Data Types ♢ [8/36]
❖ ... Collections


Typical operations on collections

COMP2521 ♢ Abstract Data Types ♢ [9/36]
❖ Example: Set ADT


Set data type: collection of unique integer values.

"Book-keeping" operations:

Assignment operations:
COMP2521 ♢ Abstract Data Types ♢ [10/36]
❖ ... Example: Set ADT


Data-type operations:

Note: union and intersection return a newly-created Set
COMP2521 ♢ Abstract Data Types ♢ [11/36]
❖ Set ADT Interface


// Set.h ... interface to Set ADT

#ifndef SET_H
#define SET_H

#include <stdio.h>
#include <stdbool.h>

typedef struct SetRep *Set;

Set newSet();              // create new empty set
void dropSet(Set);         // free memory used by set
Set SetCopy(Set);          // make a copy of a set
void SetInsert(Set,int);   // add value into set
void SetDelete(Set,int);   // remove value from set
bool SetMember(Set,int);   // set membership
Set SetUnion(Set,Set);     // union
Set SetIntersect(Set,Set); // intersection
int SetCard(Set);          // cardinality
void showSet(Set);         // display set on stdout
void readSet(FILE *, Set); // read+insert set values

#endif

COMP2521 ♢ Abstract Data Types ♢ [12/36]
❖ ... Set ADT Interface


Example set client: set of small odd numbers

#include "Set.h"
...
Set s = newSet();
for (int i = 1; i < 26; i += 2)
    SetInsert(s,i);
showSet(s); putchar('\n');

Outputs:

{1,3,5,7,9,11,13,15,17,19,21,23,25}

COMP2521 ♢ Abstract Data Types ♢ [13/36]
❖ Set Applications

Example: eliminating duplicates

#include "Set.h"
...
// scan a list of items in a file
int item;
Set seenItems = newSet();
FILE *in = fopen(FileName,"r");
while (fscanf(in, "%d", &item) == 1) {
   if (!SetMember(seenItems, item)) {
      SetInsert(seenItems, item);
      process item;
   }
}
fclose(in);

COMP2521 ♢ Abstract Data Types ♢ [14/36]
❖ Set ADT Pre/Post-conditions

Each Set operation has well-defined semantics.

Express these semantics in detail via statements of:


Could  implement condition-checking via assert()s

But only during the development/testing phase

At the very least, implement as comments at start of functions.

COMP2521 ♢ Abstract Data Types ♢ [15/36]
❖ ... Set ADT Pre/Post-conditions


If x is a variable of type T, where T is an ADT

Can also use math/logic notation as used in pseudocode.
COMP2521 ♢ Abstract Data Types ♢ [16/36]
❖ ... Set ADT Pre/Post-conditions

Examples of defining pre-/post-conditions:

// pre:  true
// post: valid(Set,res) and res = {}
Set newSet() { ... }

// pre:  valid(Set,s) and valid(int n)
// post: n ∈ s'
void SetInsert(Set s, int n) { ... }

// pre:  valid(Set,s1) and valid(Set,s2)
// post: ∀ n ∈ res, n ∈ s1 or n ∈ s2
Set SetUnion(Set s1, Set s2) { ... }

// pre:  valid(Set,s)
// post: res = |s|
int SetCard(Set s)  { ... }

COMP2521 ♢ Abstract Data Types ♢ [17/36]
❖ Sets as Unsorted Arrays


Concrete data structure:

[Diagram:Pics/set-array.png]

COMP2521 ♢ Abstract Data Types ♢ [18/36]
❖ ... Sets as Unsorted Arrays

Concrete data structure (in C):

#define MAXELEMS 1000

// concrete data structure
struct SetRep {
    int elems[MAXELEMS];
    int nelems;
};

Need to set upper bound on number of elements

Could do statically (as above) or dynamically

Set newSet(int maxElems) { ... }

COMP2521 ♢ Abstract Data Types ♢ [19/36]
❖ ... Sets as Unsorted Arrays

Set creation:

// create new empty set
Set newSet()
{
   Set s = malloc(sizeof(struct SetRep));
   if (s == NULL) {
      fprintf(stderr, "Insufficient memory\n");
      exit(EXIT_FAILURE);
   }
   s->nelems = 0;
   // assert(isValid(s));
   return s;
}

COMP2521 ♢ Abstract Data Types ♢ [20/36]
❖ ... Sets as Unsorted Arrays

Checking membership:

// set membership test
int SetMember(Set s, int n)
{   
   // assert(isValid(s));
   int i; 
   for (i = 0; i < s->nelems; i++)
      if (s->elems[i] == n) return TRUE;
   return FALSE;
}

COMP2521 ♢ Abstract Data Types ♢ [21/36]
❖ ... Sets as Unsorted Arrays

Costs for set operations on unsorted array:

Assuming: s1 has n items, s2 has m items
COMP2521 ♢ Abstract Data Types ♢ [22/36]
❖ Sets as Sorted Arrays


Same data structure as for unsorted array.

Differences in

COMP2521 ♢ Abstract Data Types ♢ [23/36]
❖ ... Sets as Sorted Arrays

Costs for set operations on sorted array:

COMP2521 ♢ Abstract Data Types ♢ [24/36]
❖ Sets as Linked Lists

Concrete data structure:

[Diagram:Pics/set-list.png]

COMP2521 ♢ Abstract Data Types ♢ [25/36]
❖ ... Sets as Linked Lists

Concrete data structure (in C):

typedef struct Node {
   int  value;
   struct Node *next;
} Node;

struct SetRep {
   Node *elems;  // pointer to first node
   Node *last;   // pointer to last node
   int nelems;   // number of nodes
};

COMP2521 ♢ Abstract Data Types ♢ [26/36]
❖ ... Sets as Linked Lists

Set creation:

// create new empty set
Set newSet()
{
   Set s = malloc(sizeof(struct SetRep));
   if (s == NULL) {...}
   s->nelems = 0;
   s->elems = s->last = NULL;
   return s;
}

COMP2521 ♢ Abstract Data Types ♢ [27/36]
❖ ... Sets as Linked Lists

Checking membership:

// set membership test
int SetMember(Set s, int n)
{
   // assert(isValid(s));
   Node *cur = s->elems;
   while (cur != NULL) {
      if (cur->value == n) return true;
      cur = cur->next;
   }
   return false;
}   

COMP2521 ♢ Abstract Data Types ♢ [28/36]
❖ ... Sets as Linked Lists

Costs for set operations on linked list:

Assume n = size of s1, m = size of s2

If we don't have nelems, card becomes O(n)

COMP2521 ♢ Abstract Data Types ♢ [29/36]
❖ Sets as Bit-strings


Set is a very long bit-string, typically an array of words.

Restrict possible values that can be stored in the Set

COMP2521 ♢ Abstract Data Types ♢ [30/36]
❖ ... Sets as Bit-strings

Concrete data structure:

[Diagram:Pics/set-bits.png]

COMP2521 ♢ Abstract Data Types ♢ [31/36]
❖ ... Sets as Bit-strings

Concrete data structure (in C):

#define NBITS 1024
#define NWORDS (NBITS/32)
typedef unsigned int Word;
typedef Word Bits[NWORDS];

struct SetRep {
   int nelems;
   Bits bits;  // Word bits[NWORDS]
};

Sets defined like this can hold values in range 0..1023

COMP2521 ♢ Abstract Data Types ♢ [32/36]
❖ ... Sets as Bit-strings

Implementation as bit-strings requires extra functions:

Can be implemented efficiently, e.g.

getBit(Bits b, int i) {
   int whichWord = i / 32;
   int whichBit  = i % 32;
   Word mask = (1 << whichBit)
   return (b[whichWord] & mask) >> whichBit;
}
COMP2521 ♢ Abstract Data Types ♢ [33/36]
❖ Setting and unsetting bits

Setting and unsetting bits by & and |

COMP2521 ♢ Abstract Data Types ♢ [34/36]
❖ ... Setting and unsetting bits

Powers of two by bit-shifting - don't use pow(...) from math.h!

COMP2521 ♢ Abstract Data Types ♢ [35/36]
❖ Performance of Set Implementations


Performance comparison:

Data Structure insert delete member , storage
unsorted array O(n) O(n) O(n) O(n.m) O(N)
sorted array O(n) O(n) O(log2n) O(n+m) O(N)
unsorted linked list O(n) O(n) O(n) O(n.m) O(n)
sorted linked list O(n) O(n) O(n) O(n+m) O(n)
bit-maps O(1) O(1) O(1) O(N) O(N)

n,m = #elems,   N = max #elems,  

COMP2521 ♢ Abstract Data Types ♢ [36/36]


Produced: 9 Feb 2021