Пример #1
0
int main ( int argc, char **argv )
{
    /*для int*/
    int arr[MAX_LEN];
    int i;
    for (i = 0; i < MAX_LEN; ++i )
	arr[i] = i;

    int data = 4;
    int pos;
    find_int(arr, MAX_LEN, data, &pos );
    printf ( "find_method %d pos %d\n", data+1, pos ); 

    pos = -1;
    FIND( arr, MAX_LEN, data, &pos );
    printf ( "find_macros %d pos %d\n", data+1, pos ); 

    /*для char*/
    char arrf[MAX_LEN];
    for (i = 0; i < MAX_LEN; ++i )
	arrf[i] = i;

    pos = -1;	
    char dataf = 40;
    find_float(arrf, MAX_LEN, dataf, &pos );
    printf ( "find_method %d pos %d\n", dataf, pos ); 

    pos = -1;	
    FIND( arrf, MAX_LEN, dataf, &pos );
    printf ( "find_macros %d pos %d\n", dataf, pos ); 
}
Пример #2
0
/*
 * Binary search an array for an integer
 *  obj: pointer to an array of integers
         or array of fixed length structs containing an integer (in this case the obj pointer should point to the integer in the first struct)
 *  struct_len: length of one object in the array in bytes
 *  i: search integer
 *  min: minimal index to search
 *  max: maximal index to search
 */
int find_int(void *objs, int struct_len, int i, int min, int max) {
    int middle = (min + max) / 2;
    int cmp_val = *((int *) (((char *) objs) + middle * struct_len));

    if (cmp_val == i) {
        // string found
        return middle;
    } else if (min != max) {
        if (cmp_val > i && min != middle) {
            // continue search in left half
            return find_int(objs, struct_len, i, min, middle - 1);
        } else if (max != middle) {
            // continue search in right half
            return find_int(objs, struct_len, i, middle + 1, max);
        }
    }

    // finished searching
    return -1;
}
Пример #3
0
int
JSONConfiguration::GetInt(IN const string &name)
{
    return find_int(_rootValue.get_obj(),name);
}
Пример #4
0
/*
 * Parses a file and adds its words to the index
 */
void parse_file_for_index(index_p index, char *file) {
    // open file or print error message
    FILE *f = fopen(file, "r");
    if (!f) {
        printf("Cannot open %s!\nIndex not updated.\n", file);
        return;
    }

    // document id = index of document in list of all documents in filebase (alphabetically ordered)
    int doc_id = find_str(&index->documents[0].name, sizeof(indexed_document_t), file, 0, index->nr_docs-1);

    if (doc_id < 0) {
        printf("Error: %s is not in the filebase!\n", file);
        return;
    }

    char *l;
    while ((l = read_line(f))) {
        // turn non alpha characters into spaces
        nonalpha_to_space(l);

        char *word = strtok(l, " ");
        while (word) {
            // ignore stopwords
            if (is_stopword(word)) {
                word = strtok(NULL, " ");
                continue;
            }

            char *word_stem = stem(word);

            if (!strlen(word_stem)) {
                word = strtok(NULL, " ");
                continue;
            }

            // insert document into index / add new stem to index
            indexed_word_p w = index->words;    // current word
            indexed_word_p p = NULL;            // previous word
            int flag = 0;
            while (w && !flag) {
                int cmp = strcmp(w->stem, word_stem);
                if (!cmp) {
                    // stem is already indexed
                    flag = 1;
                    break;
                } else if (0 < cmp) {
                    // stem not indexed yet
                    flag = 2;
                    break;
                }

                p = w;
                w = w->next;
            }

            if (flag == 1) {
                // stem indexed, add document to list
                int i;
                for (i = 0; i < w->nr_docs; i++) {
                    if (w->documents[i].id == doc_id) {
                        // document is already indexed for this stem
                        flag = 0;
                        break;
                    } else if (w->documents[i].id > doc_id) {
                        break;
                    }
                }

                // only add document to list if it's not already in the list
                if (flag) {
                    w = (indexed_word_p) realloc(w, sizeof(indexed_word_t) + sizeof(doc_t) * (w->nr_docs + 1));

                    // update pointer to this group (needed after realloc)
                    if (!p) {
                        index->words = w;
                    } else {
                        p->next = w;
                    }

                    // insert document in list
                    memmove(&w->documents[i+1], &w->documents[i], sizeof(doc_t) * (w->nr_docs - i));
                    w->documents[i].id = doc_id;
                    w->documents[i].tf = 1;
                    w->nr_docs++;
                } else {
                    // increase counter for number of occurances of this word in this document
                    w->documents[i].tf++;
                }

                free(word_stem);
            } else {
                // stem is not indexed, add it to index
                w = (indexed_word_p) malloc(sizeof(indexed_word_t) + sizeof(doc_t));
                w->stem = word_stem;
                w->nr_docs = 1;
                w->documents[0].id = doc_id;
                w->documents[0].tf = 1;

                index->nr_words++;

                // insert this word in linked list
                if (!p) {
                    w->next = index->words;
                    index->words = w;
                } else {
                    w->next = p->next;
                    p->next = w;
                }
            }

            // increase counter for total number of words in this document
            index->documents[doc_id].nr_words++;

            // get next word
            word = strtok(NULL, " ");
        }

        free(l);
    }

    fclose(f);

    // finalize computation of TF
    indexed_word_p w = index->words;
    while (w) {

        int i = find_int(&w->documents[0].id, sizeof(doc_t), doc_id, 0, w->nr_docs - 1);

        if (i >= 0) {
            w->documents[i].tf /= index->documents[doc_id].nr_words;
        }

        w = w->next;
    }
}