Click here to load reader

Object Oriented · PDF file strcpy(cor .name,”Cor ‘de boon’ Boonstra”); cor .sofi_no = 74365235; cor .name[8] = ‘B’; /* set the 9th index of cor.name */ taxp ->balance

  • View
    2

  • Download
    0

Embed Size (px)

Text of Object Oriented · PDF file strcpy(cor .name,”Cor ‘de boon’...

  • 1

    ComputationComputation

    5JJ70 5JJ70 ‘‘ImplementatieImplementatie van van rekenprocessenrekenprocessen’’

    Object Oriented Object Oriented ProgrammingProgramming

    HenkHenk CorporaalCorporaal February 2011February 2011

  • © PG/HC Programming 5JJ70 pg 2

    Welcome!

    • Previous lecture: – Structures – C++ class: introduction

    • Today: – Hashing hints (other hash function) – Structures and Classes recap – Continue our adventures in the aquarium – Multiple constructors – Introduction to Class Inheritance

    • Book: The C++ Programming Language Bjarne Stroustrup

    • Related Labs – Hash assignment, task 7 – task 8 on linked lists uses C++ / OOProgramming

  • © PG/HC Programming 5JJ70 pg 3

    Finding strings • Suppose we have a set of n strings in an array. • To check whether a string is in the set, we can perform ‘brute-force’ comparison:

    int string_exists(char * string, char ** string_array)

    {

    int j;

    for(j = 0; string_array[j] != 0; j++) {

    if(strcmp(string_array[j], string) == 0)

    return 1; // bingo! found string

    }

    return 0; // string not found

    }

    Here I assume that the array is terminated by a Null pointer.

    0

    string

    string-array

    p i e t 0

    s i n t 0

    a a i i i i 0

    a i 0

    a a a a i i i 0

    j

    stackframe of string_exists

  • © PG/HC Programming 5JJ70 pg 4

    Finding strings

    • If the string exists, this will take me on average n/2 string comparisons. If it doesn’t, this will set me back n string comparisons.

    • This algorithm is O(n) (i.e. run time is in the order of n) – The more strings there are, the slower it will get.

    • This is often unacceptably slow.

    int string_exists(char * string, char ** string_array)

    {

    int j;

    for(j = 0; string_array[j] != 0; j++) {

    if(strcmp(string_array[j], string) == 0)

    return 1; // bingo! found string

    }

    return 0; // string not found

    }

  • © PG/HC Programming 5JJ70 pg 5

    Hashing: searching a subset

    • Lets divide the strings into m subsets. In that case, the string lookup will take at most n/m steps. If m is sufficiently large, the order of the algorithm becomes O(1), that is, constant complexity!!

    • But… This will only work if we know which of the m subsets of strings we need to check.

    • So lets invent a function int hashhash(char * string) that returns a (homogenously distributed) number for each string. This number indexes the subset of strings we want to search.

    int string_exists(char * string, char *** string_array)

    {

    int j;

    int hashval = hash(string);

    for(j = 0; string_array[hashval][j] != 0; j++) {

    if(strcmp(string_array[hashval][j], string) == 0)

    return 1; // bingo! found string

    }

    return 0; // string not found

    }

    We only search the subset of strings that belong to the hashval

  • © PG/HC Programming 5JJ70 pg 6

    Hash implementation: 3 dim. array

    B

    A

    L

    K

    I

    E

    !

    !

    \0

    ..

    char *

    char

    .. .. .. .. .. .. 0..

    ..

    ..

    ..

    ..

    ..

    ..

    ..

    char **

    .. char ***

    char ** hashtable[HASHSIZE];

    char *** a = hashtable

    char ** b = hashtable[1]

    char * c = hashtable[1][5]

    char d = hashtable[1][5][2]

  • © PG/HC Programming 5JJ70 pg 7

    A possible hash function

    #define HASHSIZE 100

    unsigned int hash(char * str)

    {

    unsigned int hashval;

    for(hashval = 0; *str != '\0'; str++) { // steps through string

    hashval = *str + 31 * hashval; // mangles string

    }

    return hashval % HASHSIZE;

    }

    Taking the remainder ensures that the returned value is between 0 and HASHSIZE-1

    Here the ascii value of the character is multiplied by (31 * the previous hash value). 31 is a ‘magic number’ that was determined experimentally.

    hash(”a”) hashval = 97 -> returns 97

    hash(“aa”) hashval = 97 + (31 * 97) = 3104 -> returns 4

    hash(“aaa”) hashval = 97 + (31 * 3104) = 96321 -> returns 21

    hash(“aaab”) = ( 98 + (31 * 96321) )%100 = 49 -> return 49

  • © PG/HC Programming 5JJ70 pg 8

    Another hash function

    • This one is similar, except that the mangling happens using exors and shifts

    #define HASHSIZE 100

    unsigned int hash(char * str)

    {

    unsigned int hashval;

    for(hashval = 0; *str != '\0'; str++) {

    hashval = (hashval28)^(*str);

    }

    return hashval % HASHSIZE;

    }

    ^ exor operator for all 32 pairs of input bits do:

    0^0 = 0 0^1 = 1 1^0 = 1 1^1 = 0

  • © PG/HC Programming 5JJ70 pg 9

    A suggestion for P7 program structure char **hashtable[HASHSIZE]; // global database

    void main(void)

    {

    char command[100], char sentence[1000];

    while(1) { // endless command loop

    scanf(“ %s”, command);

    if(strcmp(command, “stop”) == 0)

    break;

    if(strcmp(command, “test”) == 0) {

    random_sentence(sentence);

    printf(“%s\n”, sentence);

    continue;

    }

    if(strcmp(command, “add”) == 0) {

    read_sentence(sentence);

    add_sentence_to_hashtable(sentence);

    continue;

    }

    // etc.

    }

    }

    void random_sentence(char * buffer) { .. }

    void read_sentence(char * buffer) { .. }

    Note: your hashtable may be slightly different

  • © PG/HC Programming 5JJ70 pg 10

    Recap: Structures

    • A structure defines a data type that consists of one or more members. • Examples:

    struct coordinate {

    int x;

    int y;

    };

    struct coordinate a, b[3];

    struct taxpayer {

    char name[80];

    int sofi_no;

    double balance;

    };

    struct taxpayer pietje;

    sofi_no

    balance

    pietje

    name

    a x

    y

    x

    y

    x

    y

    x

    y

    b

    b[1].y

    pietje.name

    pietje.name[79]

  • © PG/HC Programming 5JJ70 pg 11

    Example

    struct coordinate crd; /* a struct variable called crd */

    struct taxpayer cor; /* a taxpayer called cor */

    struct coordinate * crd_p = &crd; /* pointer to a coordinate structure */

    struct taxpayer * taxp = &cor; /* pointer to a pointer to a taxpayer */

    struct taxpayer family[4]; /* an array of 4 taxpayers */

    crd.x = 3; crd.y = 5; /* set values of x and y in crd */

    crd.x++; /* increment crd.x to 4 */

    crd_p->x = 10; crd_p->y = 20; /* set the values of x and y in crd */

    ++crd_p->y; /* increment crd_p->y (=crd.y) to 21 */

    strcpy(cor.name,”Cor ‘de boon’ Boonstra”); cor.sofi_no = 74365235;

    cor.name[8] = ‘B’; /* set the 9th index of cor.name */

    taxp->balance = -654643.00;

    family[2].balance = 40.0; family[2].balance += 100.0;

    printf(“Taxpayer %s Sofi number: %d, balance %.2f\n”,

    cor.name, taxp->sofi_no, cor.balance);

    • In case of a struct, use the dot . – struct_var.fname

    • In case of a pointer to a struct, we use the arrow -> – struct_var->fname

    struct coordinate {

    int x;

    int y;

    };

    struct taxpayer {

    char name[80];

    int sofi_no;

    double balance;

    };

  • © PG/HC Programming 5JJ70 pg 12

    Self referential structs

    struct Node {

    int data;

    struct Node * nextptr;

    };

    struct Node a, b;

    /* or use

    typedef struct Node mynode;

    mynode a, b;

    */

    a.data = 34; b.data = 35;

    a.nextptr = &b;

    b.nextptr = 0;

    a

    b

    34

    35

    0

  • © PG/HC Programming 5JJ70 pg 13

    More onTypedef

    typedef int aaa, bbb, ccc;

    typedef int ar[15], arr[9][6];

    typedef char c, *cp, carr[100];

    /* now declare some objects */

    aaa int1; /* all ints */

    bbb int2;

    ccc int3;

    ar yyy; /* array of 15 ints */

    arr xxx; /* 9*6 array of int */

    c ch; /* a char */

    cp pnt; /* pointer to char */

    carr chry; /* array of 100 char */

    typedef int (*a10ptoa5i[10])[5]; // oeps !!

    /* or

    typedef int a5i[5];

    typedef a5i *a10ptoa5i[10]; */

  • © PG/HC Programming 5JJ70 pg 14

    Struct vs. Class

    typedef struct {

    char * _name; /* name of fish */

    int _weight; /* its weight */

    } fish;

    void main() {

    fish * f1, * f2;

    /* make a fish called orca */

    f1 = (fish *) malloc(sizeof(fish));

    f1->_name = (char *) malloc(5);

    strcpy(f1->_name,”Orca”);

    f1->_weight = 250;

    /* make a fish called seal */

    f2 = (fish *) malloc(sizeof(fish));

    f2->_name = (char

Search related