Esempio n. 1
0
/**
 * Inserts a new item into the hash table using double
 * hashing or linear probing.
 *
 * @param h the htable to be hashed into.
 * @param s the value to be inserted into the hash table.
 * @return returns whether the value was inserted successfully or not.
 */
int htable_insert(htable h, const char *s, int docid){
    int collisions;
    int position = htable_word_to_int(s) % h-> capacity;
    int step = htable_step(h, htable_word_to_int(s));

    for (collisions = 0; collisions <= h->capacity; collisions++){
        if ((h->keys[position]).key == NULL) {
            (h->keys[position]).key = emalloc((strlen(s) + 1) * sizeof(s[0]));
            strcpy((h->keys[position]).key, s);
            (h->keys[position]).postings = flexarray_new();
            flexarray_append((h->keys[position]).postings, docid);
            h->keys[position].term_freq = 1;
            h->num_keys++;
            h->count[position]++;
            return 1;
        } else if (!strcmp((h->keys[position]).key, s)){

            h->count[position]++;

            if (flexarray_get_last_id((h->keys[position]).postings) != docid) {
                flexarray_append((h->keys[position]).postings, docid);
                h->keys[position].term_freq++;
            } else {
                flexarray_updatecount((h->keys[position]).postings);
            }
            return 1;
        } else {
            position = (position + step) % h->capacity;
        }
    }
    return 0;
}
Esempio n. 2
0
/**
 * Searches the hash table for the given value.
 *
 * @param h the htable to be searched.
 * @param s the value to search the hash table with.
 * @return whether the value was found or not.
 */
int htable_search(htable h, const char *s){
    int i;
    int position = htable_word_to_int(s) % h-> capacity;
    int step = htable_step(h, htable_word_to_int(s));

    for (i = 0; i <= h->capacity; i++){
        if (h->keys[position].key == NULL){
            return 0;
        } else if(strcmp(h->keys[position].key, s) == 0){
            return 1;
        } else {
            position = (position + step) % h->capacity;
        }
    }
    return 0;
}
Esempio n. 3
0
int htable_search(htable h, char *str) {
    int collision = 0;
    int searchIndex = htable_word_to_int(str) % h->capacity;
    int step = htable_step(h, htable_word_to_int(str));
    
    
    while (collision <= h->capacity) {
        if (h->keys[searchIndex] == NULL) {
            return 0;
        }
        if (strcmp(str, h->keys[searchIndex]) == 0) {
            return h->freqs[searchIndex];
        }
        if (h->method == DOUBLE_H) {
            searchIndex = (searchIndex + step ) % h->capacity;
        } else {
            searchIndex = (searchIndex + 1 ) % h->capacity;
        }
        collision++;
    }
    

    return 0;
}
Esempio n. 4
0
/**
 return 0 means insert failed
 return 1 means insert succeed at the fisrt time
 return >1 means the frequencies the word has been inserted
 */
int htable_insert(htable h, char *str) {
    unsigned int wordInteger = htable_word_to_int(str);
    unsigned int wordIndex = wordInteger % h->capacity;
    unsigned int step = htable_step(h, wordInteger);
    int collision = 0;
    unsigned int index;
    
    if (h->keys[wordIndex] == NULL) {
        h->keys[wordIndex] = emalloc((strlen(str) + 1) * sizeof(h->keys[0][0]));
        strcpy(h->keys[wordIndex], str);
        h->freqs[wordIndex] += 1;
        h->num_keys += 1;
        h->stats[h->num_keys-1] = 0;
        
        
        
        return 1;
    } else if (strcmp(str, h->keys[wordIndex]) == 0) {
        h->freqs[wordIndex] += 1;
        
        return h->freqs[wordIndex];
    } else {
        
        
        
        
        index = wordIndex;
        while (collision < h->capacity && h->keys[index] != NULL &&
               strcmp(str, h->keys[index]) != 0) {
            
            
            
            if (h->method == LINEAR_P) {
                index += 1;
            } else {
                index += step;
            }
            index = index % h->capacity;
            
            collision++;
        }
        
        if (h->keys[index] == NULL) {
            
            h->keys[index] = emalloc((strlen(str) + 1) * sizeof(h->keys[0][0]));
            strcpy(h->keys[index], str);
            h->freqs[index] += 1;
            h->num_keys += 1;
            h->stats[h->num_keys-1] = collision;
            
            
            
            return 1;
        } else if (strcmp(str, h->keys[index]) == 0) {
            
            
            
            h->freqs[index] += 1;
            return h->freqs[index];
        } else {
            
            return 0;
        }
    }
}