Commit c0480f71 authored by Alix Danvy's avatar Alix Danvy
Browse files

tp1 truc

parent cc6bb2ce
Loading
Loading
Loading
Loading
+26 −1
Changes for src/bst.c: 26 added lines, 1 removed line.
Original line number Diff line number Diff line
#include <stdlib.h>

struct node_t {
  unsigned long int key;
  void* val;
@@ -8,23 +10,46 @@ struct node_t {
typedef struct bst_t {
  // Use this to compute a key from a val
  // It is not really a hash function, the key must be unique
  unsigned long int (*h)(void*);
  // unsigned long int (*h)(void*);

  unsigned long int size;
  struct node_t* tree;
} bst_t;

typedef struct node_t* tree_t;


unsigned long int hash(void*);

bst_t bst_empty(void){
    bst_t res;
    t.size = 0;
    return res; 
}

int bst_is_empty(bst_t t){
    return (t.size==0);
}

bst_t bst_add(bst_t t, void* x){
    if(bst_empty(t)) {
      tree_t res = malloc(sizeof(struct node_t));
        res->val = x;
        res->key = hash(x);
        res->left = NULL;
        res->right = NULL;
    } else {
        tree_t tmp1;
        /// ICIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII

    }
    return t;
}

void bst_destroy(bst_t t, void (*free_void)(void*)){

}
// void bst_destroy_bis(node_t* )

int bst_in(bst_t t, void* x){
}
+11 −10
Changes for src/bst.h: 11 added lines, 10 removed lines.
Original line number Diff line number Diff line
#ifndef __BST__
#define __BST__

struct node_t {
  unsigned long int key;
  void* val;
  struct node_t* left;
  struct node_t* right;
};
// struct node_t {
//   unsigned long int key;
//   void* val;
//   struct node_t* left;
//   struct node_t* right;
// };

typedef struct bst_t {
  unsigned long int size;
  struct node_t* tree;
} bst_t;
// typedef struct bst_t {
//   unsigned long int size;
//   struct node_t* tree;
// //  unsigned long int (*h)(void*);
// } bst_t;

unsigned long int hash(void*);
bst_t bst_empty(void);
+9 −8
Changes for src/hash_tbl.c: 9 added lines, 8 removed lines.
Original line number Diff line number Diff line
#include <stddef.h>
#include "list.h"
#include "hash_tbl.h"

#define ARRAY_CAPACITY 1 << 16

struct hash_tbl {
  size_t (*h)(void*);
  int (*eq)(void*, void*);
  void (*free)(void*);
  unsigned long int size;
  size_t capacity; // array length
  list_t* array;
};
// struct hash_tbl {
//   size_t (*h)(void*);
//   int (*eq)(void*, void*);
//   void (*free)(void*);
//   unsigned long int size;
//   size_t capacity; // array length
//   list_t* array;
// };

typedef struct hash_tbl* hash_tbl;

+1 −1
Changes for src/hash_tbl.h: 1 added line, 1 removed line.
Original line number Diff line number Diff line
#ifndef __HASH_TBL__
#define __HASH_TBL__

#include "linked_list.h"
#include "list.h"

struct hash_tbl {
  size_t (*h)(void*);
+44 −1
Changes for src/list.c: 44 added lines, 1 removed line.
Original line number Diff line number Diff line
#include <stdlib.h>

struct cell_t {
  void* val;
  unsigned long int id;
@@ -7,30 +9,71 @@ struct cell_t {
typedef struct cell_t* list_t;

list_t list_empty(){
    return NULL;
}

int list_is_empty(list_t l){
    return (l==NULL);
}

list_t list_push(list_t l, void* x){
    list_t res = malloc(sizeof(struct cell_t));
    res->val=x;
    res->next=l;

    if(l==NULL)
        res->id=0;
    else
        res->id=1+l->id;
    return res;
}

list_t list_tail(list_t l){
    if(l==NULL)
        return NULL;
    return l->next;
}

void* list_pop(list_t* l){ 
    if(l==NULL)
        return NULL; 
    void* res = (*l)->val;
    list_t tmp = *l;
    (*l) = (*l)->next;
    free(tmp);
    return res;
}

void* list_top(list_t l){
    if(l==NULL)
        return NULL;
    return l->val;
}

void list_destroy(list_t l, void (*free)(void*)){
void list_destroy(list_t l, void (*free_void)(void*)){
    if(l!=NULL) {
          for(int i=l->id;i>=0;--i){
              free_void(l->val);
              list_t tmp = l;
              l = l->next;
              free(tmp);
          }
    }
}

// return the found element or NULL
void* list_in(list_t l, void* x, int (*eq)(void*, void*)){
    while(l!=NULL) {
        if(eq(x,l->val))
            return l->val;
        l = l->next;
    }
    return NULL;
}

unsigned long int list_len(list_t l){
    if(l==NULL)
        return 0;
    return 1+l->id;
}
Loading