Ejemplo n.º 1
0
int liste_add_alphabetical(Liste* l, Celmot* elem) {
    Liste e = *l;
    if(elem == NULL)
        return 0;
    if(*l == NULL || (*l)->valeur == NULL) {
        *l = liste_new();
        (*l)->valeur = elem;
        return 1;
    }

    if(strcmp(e->valeur->mot, elem->mot) > 0) {
        Liste to_add = liste_new();
        to_add->valeur = elem;
        to_add->suivant = e;
        *l = to_add;
        return 1;
    }
    Liste prec = e;
    e = e->suivant;

    while(e != NULL && strcmp(e->valeur->mot, elem->mot) < 0) {
        prec = e;
        e = e->suivant;
    }
    if(e != NULL && strcmp(e->valeur->mot, elem->mot) == 0) {
        listepos_add(e->valeur->positions, elem->positions);
        return 1;
    }

    Liste to_add = liste_new();
    to_add->valeur = elem;
    to_add->suivant = e;
    prec->suivant = to_add;
    return 1;
}
Ejemplo n.º 2
0
int add_word(Cellule* tab, long size_of_tab, unsigned char* word, unsigned short word_len, unsigned long sentence_pos, Liste* alphabetical_word_list) {
    int hash = hache((char*) word);
    Liste l = &tab[hash % size_of_tab]; /* we get the address of the row we have to write to */

    /* we have to add it at the end of the cell list. */
    do {
        if(l->valeur != NULL  && strcmp((const char*)l->valeur->mot, (const char*) word) == 0) {
            celmot_add_position(l->valeur, sentence_pos);
            return 1;
        }
        if(l->suivant == NULL)
            break;
        l = l->suivant;
    } while(l->suivant != NULL);
    Liste to_add;
    if(l->valeur == NULL)
        to_add = l;
    else
        to_add = liste_new();
    char* word_to_add = malloc(sizeof(char) * (word_len));
    strcpy(word_to_add, word);
    Celmot* elem = celmot_new(word_to_add);
    to_add->valeur = elem;
    celmot_add_position(elem, sentence_pos);
    if(l != to_add)
        liste_add(&l, to_add);
    liste_add_alphabetical(alphabetical_word_list, elem);
    return 1;
}
Ejemplo n.º 3
0
void liste_add(Liste *l, Liste elem) {
    if(*l == NULL || *l == 0) {
        *l = liste_new();
        (*l)->suivant = elem;
        return ;
    }
    while((*l)->suivant){
        *l = (*l)->suivant;
    }
    (*l)->suivant = elem;
}
Ejemplo n.º 4
0
void* TGV_thread_fn(void* arg) {
    train *TGV = (train*) arg;
    list *l;

    switch (TGV->direction) {
    case OE:
        l = liste_new(dictionary_char_2d_get(TGV->postes, "Gare", "Voie C"));
        liste_add(l, dictionary_char_2d_get(TGV->postes, "P0", "Aiguillage 2"));
        liste_add(l, dictionary_char_2d_get(TGV->postes, "P1", "Ligne TGV"));

        printf("%p - %s - OE - locking : Voie C, Aiguillage 2, Ligne TGV\n", (void*) (pthread_self()), TGV->nom);

        banquier_lock(TGV->banquier, l);

        printf("%p - %s - OE - locked : Voie C, Aiguillage 2, Ligne TGV\n", (void*) (pthread_self()), TGV->nom);

        liste_del(l);

        l = liste_new(dictionary_char_2d_get(TGV->postes, "Gare", "Voie C"));
        liste_add(l, dictionary_char_2d_get(TGV->postes, "P0", "Aiguillage 2"));

        printf("%p - %s - OE - releasing : Voie C, Aiguillage 2\n", (void*) (pthread_self()), TGV->nom);

        banquier_unlock(TGV->banquier, l);

        printf("%p - %s - OE - released : Voie C, Aiguillage 2\n", (void*) (pthread_self()), TGV->nom);

        liste_del(l);

        l = liste_new(dictionary_char_2d_get(TGV->postes, "P2", "Tunnel"));
        liste_add(l, dictionary_char_2d_get(TGV->postes, "P3", "Ligne"));

        printf("%p - %s - OE - locking : Tunnel, Ligne\n", (void*) (pthread_self()), TGV->nom);

        banquier_lock(TGV->banquier, l);

        printf("%p - %s - OE - locked : Tunnel, Ligne\n", (void*) (pthread_self()), TGV->nom);

        liste_del(l);

        l = liste_new(dictionary_char_2d_get(TGV->postes, "P1", "Ligne TGV"));
        liste_add(l, dictionary_char_2d_get(TGV->postes, "P2", "Tunnel"));
        liste_add(l, dictionary_char_2d_get(TGV->postes, "P3", "Ligne"));

        printf("%p - %s - OE - releasing : Ligne TGV, Tunnel, Ligne\n", (void*) (pthread_self()), TGV->nom);

        banquier_unlock(TGV->banquier, l);

        printf("%p - %s - OE - released : Ligne TGV, Tunnel, Ligne\n", (void*) (pthread_self()), TGV->nom);

        liste_del(l);

        break;
    case EO:
        l = liste_new(dictionary_char_2d_get(TGV->postes, "P3", "Ligne"));
        liste_add(l, dictionary_char_2d_get(TGV->postes, "P3", "Tunnel"));
        liste_add(l, dictionary_char_2d_get(TGV->postes, "P2", "Ligne TGV"));

        printf("%p - %s - EO - locking : Ligne, Tunnel, Ligne TGV\n", (void*) (pthread_self()), TGV->nom);

        banquier_lock(TGV->banquier, l);

        printf("%p - %s - EO - locked : Ligne, Tunnel, Ligne TGV\n", (void*) (pthread_self()), TGV->nom);

        liste_del(l);

        l = liste_new(dictionary_char_2d_get(TGV->postes, "P3", "Ligne"));
        liste_add(l, dictionary_char_2d_get(TGV->postes, "P3", "Tunnel"));

        printf("%p - %s - EO - releasing : Ligne, Tunnel\n", (void*) (pthread_self()), TGV->nom);

        banquier_unlock(TGV->banquier, l);

        printf("%p - %s - EO - released : Ligne, Tunnel\n", (void*) (pthread_self()), TGV->nom);

        liste_del(l);

        l = liste_new(dictionary_char_2d_get(TGV->postes, "P1", "Aiguillage 2"));
        liste_add(l, dictionary_char_2d_get(TGV->postes, "P0", "Voie D"));

        printf("%p - %s - EO - locking : Aiguillage 2, Voie D\n", (void*) (pthread_self()), TGV->nom);

        banquier_lock(TGV->banquier, l);

        printf("%p - %s - EO - locked : Aiguillage 2, Voie D\n", (void*) (pthread_self()), TGV->nom);

        liste_del(l);

        l = liste_new(dictionary_char_2d_get(TGV->postes, "P2", "Ligne TGV"));
        liste_add(l, dictionary_char_2d_get(TGV->postes, "P1", "Aiguillage 2"));
        liste_add(l, dictionary_char_2d_get(TGV->postes, "P0", "Voie D"));

        printf("%p - %s - EO - releasing : Ligne TGV, Aiguillage 2, Voie D\n", (void*) (pthread_self()), TGV->nom);

        banquier_unlock(TGV->banquier, l);

        printf("%p - %s - EO - released : Ligne TGV, Aiguillage 2, Voie D\n", (void*) (pthread_self()), TGV->nom);

        liste_del(l);

        break;
    default:
        perror("Erreur : TGV - direction inconnu");
        pthread_exit(NULL);
    }

    printf("%p - %s - %s - ARRIVE ------------------------------------------\n", (void*) (pthread_self()), TGV->nom, TGV->direction == OE ? "OE" : "EO");

    return NULL;
}
Ejemplo n.º 5
0
void* M_thread_fn(void* arg) {
    train *M = (train*) arg;
    list* l;

    switch (M->direction) {
    case OE:
        l = liste_new(dictionary_char_2d_get(M->postes, "P1", "Voie A"));
        liste_add(l, dictionary_char_2d_get(M->postes, "P1", "Aiguillage 1"));
        liste_add(l, dictionary_char_2d_get(M->postes, "P1", "Ligne M - OE"));

        printf("%p - %s - OE - locking : Voie A, Aiguillage 1, Ligne M - OE\n", (void*) (pthread_self()), M->nom);

        banquier_lock(M->banquier, l);

        printf("%p - %s - OE - locked : Voie A, Aiguillage 1, Ligne M - OE\n", (void*) (pthread_self()), M->nom);

        liste_del(l);

        l = liste_new(dictionary_char_2d_get(M->postes, "P1", "Voie A"));
        liste_add(l, dictionary_char_2d_get(M->postes, "P1", "Aiguillage 1"));

        printf("%p - %s - OE - releasing : Voie A, Aiguillage 1\n", (void*) (pthread_self()), M->nom);

        banquier_unlock(M->banquier, l);

        printf("%p - %s - OE - released : Voie A, Aiguillage 1\n", (void*) (pthread_self()), M->nom);

        liste_del(l);

        l = liste_new(dictionary_char_2d_get(M->postes, "P2", "Tunnel"));
        liste_add(l, dictionary_char_2d_get(M->postes, "P3", "Ligne"));

        printf("%p - %s - OE - locking : Tunnel, Ligne\n", (void*) (pthread_self()), M->nom);

        banquier_lock(M->banquier, l);

        printf("%p - %s - OE - locked : Tunnel, Ligne\n", (void*) (pthread_self()), M->nom);

        liste_del(l);

        l = liste_new(dictionary_char_2d_get(M->postes, "P1", "Ligne M - OE"));
        liste_add(l, dictionary_char_2d_get(M->postes, "P2", "Tunnel"));
        liste_add(l, dictionary_char_2d_get(M->postes, "P3", "Ligne"));

        printf("%p - %s - OE - releasing : Ligne M - OE, Tunnel, Ligne\n", (void*) (pthread_self()), M->nom);

        banquier_unlock(M->banquier, l);

        printf("%p - %s - OE - released : Ligne M - OE, Tunnel, Ligne\n", (void*) (pthread_self()), M->nom);

        liste_del(l);

        break;
    case EO:
        l = liste_new(dictionary_char_2d_get(M->postes, "P3", "Ligne"));
        liste_add(l, dictionary_char_2d_get(M->postes, "P3", "Tunnel"));
        liste_add(l, dictionary_char_2d_get(M->postes, "P2", "Ligne M - EO"));

        printf("%p - %s - EO - locking : Ligne, Tunnel, Ligne M - EO\n", (void*) (pthread_self()), M->nom);

        banquier_lock(M->banquier, l);

        printf("%p - %s - EO - locked : Ligne, Tunnel, Ligne M - EO\n", (void*) (pthread_self()), M->nom);

        liste_del(l);

        l = liste_new(dictionary_char_2d_get(M->postes, "P3", "Ligne"));
        liste_add(l, dictionary_char_2d_get(M->postes, "P3", "Tunnel"));

        printf("%p - %s - EO - releasing : Ligne, Tunnel\n", (void*) (pthread_self()), M->nom);

        banquier_unlock(M->banquier, l);

        printf("%p - %s - EO - released : Ligne, Tunnel\n", (void*) (pthread_self()), M->nom);

        liste_del(l);

        l = liste_new(dictionary_char_2d_get(M->postes, "P1", "Aiguillage 1"));
        liste_add(l, dictionary_char_2d_get(M->postes, "P1", "Voie B"));

        printf("%p - %s - EO - locking : Aiguillage 1, Voie B\n", (void*) (pthread_self()), M->nom);

        banquier_lock(M->banquier, l);

        printf("%p - %s - EO - locked : Aiguillage 1, Voie B\n", (void*) (pthread_self()), M->nom);

        liste_del(l);

        l = liste_new(dictionary_char_2d_get(M->postes, "P2", "Ligne M - EO"));
        liste_add(l, dictionary_char_2d_get(M->postes, "P1", "Aiguillage 1"));
        liste_add(l, dictionary_char_2d_get(M->postes, "P1", "Voie B"));

        printf("%p - %s - EO - releasing : Ligne M - EO, Aiguillage 1, Voie B\n", (void*) (pthread_self()), M->nom);

        banquier_unlock(M->banquier, l);

        printf("%p - %s - EO - released : Ligne M - EO, Aiguillage 1, Voie B\n", (void*) (pthread_self()), M->nom);

        liste_del(l);

        break;
    default:
        perror("Erreur : M - direction inconnu");
        pthread_exit(NULL);
    }

    printf("%p - %s - %s - ARRIVE ------------------------------------------\n", (void*) (pthread_self()), M->nom, M->direction == OE ? "OE" : "EO");

    return NULL;
}