Ejemplo n.º 1
0
Archivo: search.c Proyecto: ris21/yoda
/* Initialize the search and replace history lists. */
void history_init(void)
{
    search_history = make_new_node(NULL);
    search_history->data = mallocstrcpy(NULL, "");
    searchage = search_history;
    searchbot = search_history;

    replace_history = make_new_node(NULL);
    replace_history->data = mallocstrcpy(NULL, "");
    replaceage = replace_history;
    replacebot = replace_history;
}
Ejemplo n.º 2
0
node *make_new_leaf()
{
    node *leaf;
    leaf = make_new_node();
    leaf->is_leaf = true;
    return leaf;
}
Ejemplo n.º 3
0
            // huffman coding algorithm 
            // based on a pseudocode on 16.3 CLRS 3rd.
            SharedNode make_tree(FrequencyMap<Char, Freq> const& map) const
            {
                auto greater = [](SharedNode lhs, SharedNode rhs)
                {
                    if (lhs->freq_ != rhs->freq_)
                        return lhs->freq_ > rhs->freq_;
                    else
                        return lhs->character_ > rhs->character_;
                };


                using MinPriorityQueue = std::priority_queue < SharedNode, std::vector<SharedNode>, decltype(greater) > ;

                MinPriorityQueue queue(greater);
                for (auto const& pair : map)
                    queue.push(make_new_node(pair.first, pair.second));

                for (int count = 1; count != map.size(); ++count)
                {
                    auto merge = make_new_node<Char, Freq>();
                    merge->left_ = queue.top();		queue.pop();
                    merge->right_ = queue.top();	queue.pop();
                    merge->freq_ = merge->left_->freq_ + merge->right_->freq_;
                    queue.push(merge);
                }

                return queue.top();
            }
Ejemplo n.º 4
0
Archivo: search.c Proyecto: ris21/yoda
/* Update a history list.  h should be the current position in the
 * list. */
void update_history(linestruct **h, const char *s)
{
    linestruct **hage = NULL, **hbot = NULL, *p;

    assert(h != NULL && s != NULL);

    if (*h == search_history) {
	hage = &searchage;
	hbot = &searchbot;
    } else if (*h == replace_history) {
	hage = &replaceage;
	hbot = &replacebot;
    }

    assert(hage != NULL && hbot != NULL);

    /* If this string is already in the history, delete it. */
    p = find_history(*hage, *hbot, s, strlen(s));

    if (p != NULL) {
	linestruct *foo, *bar;

	/* If the string is at the beginning, move the beginning down to
	 * the next string. */
	if (p == *hage)
	    *hage = (*hage)->next;

	/* Delete the string. */
	foo = p;
	bar = p->next;
	unlink_node(foo);
	delete_node(foo);
	renumber(bar);
    }

    /* If the history is full, delete the beginning entry to make room
     * for the new entry at the end.  We assume that MAX_SEARCH_HISTORY
     * is greater than zero. */
    if ((*hbot)->lineno == MAX_SEARCH_HISTORY + 1) {
	linestruct *foo = *hage;

	*hage = (*hage)->next;
	unlink_node(foo);
	delete_node(foo);
	renumber(*hage);
    }

    /* Add the new entry to the end. */
    (*hbot)->data = mallocstrcpy((*hbot)->data, s);
    splice_node(*hbot, make_new_node(*hbot), (*hbot)->next);
    *hbot = (*hbot)->next;
    (*hbot)->data = mallocstrcpy(NULL, "");

    /* Indicate that the history's been changed. */
    history_changed = TRUE;

    /* Set the current position in the list to the bottom. */
    *h = *hbot;
}
Ejemplo n.º 5
0
/* Initialize the lists of historical search and replace strings
 * and the list of historical executed commands. */
void history_init(void)
{
	search_history = make_new_node(NULL);
	search_history->data = mallocstrcpy(NULL, "");
	searchtop = search_history;
	searchbot = search_history;

	replace_history = make_new_node(NULL);
	replace_history->data = mallocstrcpy(NULL, "");
	replacetop = replace_history;
	replacebot = replace_history;

	execute_history = make_new_node(NULL);
	execute_history->data = mallocstrcpy(NULL, "");
	executetop = execute_history;
	executebot = execute_history;
}
Ejemplo n.º 6
0
node *make_new_leaf()
{
    node *leaf = NULL;
    leaf = make_new_node();
    if(leaf)
    leaf->is_leaf = true;
    return leaf;
}
Ejemplo n.º 7
0
node *insert_into_node_after_splitting(node *root, node *nd, node *right, int index, char *key)
{
    int i, split;
    node **temp_ps, *new_nd, *child;
    char **temp_ks, *new_key;
    temp_ps = malloc((size + 1) * sizeof(node *));
    temp_ks = malloc(size * sizeof(char *));

    for (i = 0; i < size + 1; i++){
        if (i == index + 1)
            temp_ps[i] = right;
        else if (i < index + 1)
            temp_ps[i] = nd->pointers[i];
        else
            temp_ps[i] = nd->pointers[i-1];
    }
    for (i = 0; i < size; i++){
        if (i == index){
            temp_ks[i] = malloc(MAX_KEY_LEN);
            strcpy(temp_ks[i], key);
        }
        else if (i < index)
            temp_ks[i] = nd->keys[i];
        else
            temp_ks[i] = nd->keys[i-1];
    }


    split = size % 2 ? size / 2 + 1 : size / 2;  // split is #pointers
    nd->num_keys = split - 1;
    for (i = 0; i < split - 1; i++){
        nd->pointers[i] = temp_ps[i];
        nd->keys[i] = temp_ks[i];
    }
    nd->pointers[i] = temp_ps[i];  // i == split - 1
    new_key = temp_ks[split - 1];

    new_nd = make_new_node();
    new_nd->num_keys = size - split;
    for (++i; i < size; i++){
        new_nd->pointers[i - split] = temp_ps[i];
        new_nd->keys[i - split] = temp_ks[i];
    }
    new_nd->pointers[i - split] = temp_ps[i];
    new_nd->parent = nd->parent;
    for (i = 0; i <= new_nd->num_keys; i++){  //  #pointers == num_keys + 1
        child = (node *)(new_nd->pointers[i]);
        child->parent = new_nd;
    }

    free(temp_ps);
    free(temp_ks);
    return insert_into_parent(root, nd, new_nd, new_key);
}
Ejemplo n.º 8
0
/* Update a history list (the one in which item is the current position)
 * with a fresh string text.  That is: add text, or move it to the end. */
void update_history(linestruct **item, const char *text)
{
	linestruct **htop = NULL, **hbot = NULL, *thesame;

	if (*item == search_history) {
		htop = &searchtop;
		hbot = &searchbot;
	} else if (*item == replace_history) {
		htop = &replacetop;
		hbot = &replacebot;
	} else if (*item == execute_history) {
		htop = &executetop;
		hbot = &executebot;
	}

	/* See if the string is already in the history. */
	thesame = find_history(*hbot, *htop, text, HIGHEST_POSITIVE);

	/* If an identical string was found, delete that item. */
	if (thesame != NULL) {
		linestruct *after = thesame->next;

		/* If the string is at the head of the list, move the head. */
		if (thesame == *htop)
			*htop = after;

		unlink_node(thesame);
		renumber_from(after);
	}

	/* If the history is full, delete the oldest item (the one at the
	 * head of the list), to make room for a new item at the end. */
	if ((*hbot)->lineno == MAX_SEARCH_HISTORY + 1) {
		linestruct *oldest = *htop;

		*htop = (*htop)->next;
		unlink_node(oldest);
		renumber_from(*htop);
	}

	/* Store the fresh string in the last item, then create a new item. */
	(*hbot)->data = mallocstrcpy((*hbot)->data, text);
	splice_node(*hbot, make_new_node(*hbot));
	*hbot = (*hbot)->next;
	(*hbot)->data = mallocstrcpy(NULL, "");

	/* Indicate that the history needs to be saved on exit. */
	history_changed = TRUE;

	/* Set the current position in the list to the bottom. */
	*item = *hbot;
}
Ejemplo n.º 9
0
node *make_new_root(node *left, node *right, char *key)
{
    node *root;
    root = make_new_node();
    root->pointers[0] = left;
    root->pointers[1] = right;
    root->keys[0] = malloc(MAX_KEY_LEN);
    strcpy(root->keys[0], key);
    root->num_keys++;
    left->parent = root;
    right->parent = root;
    return root;
}
Ejemplo n.º 10
0
static PyObject* insert_before(LinkedListObject* self, LinkedListNode* node,
        PyObject* obj)
{
    LinkedListNode* new_node;
    PyObject* retval;

    new_node = make_new_node(obj, node->prev, node);
    if(!new_node) return NULL;
    node->prev->next = new_node;
    node->prev = new_node;
    self->count += 1;
    retval = (PyObject*)LinkedListIterObject_new(self, new_node);
    return retval;
}
Ejemplo n.º 11
0
static PyObject* LinkedList_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    LinkedListObject *self;
    LinkedListNode *sentinal;

    self = (LinkedListObject *)type->tp_alloc(type, 0);
    if (self == NULL) return NULL;

    sentinal = make_new_node(NULL, NULL, NULL);
    if(!sentinal) {
        Py_DECREF(self);
        return NULL;
    }
    self->sentinal = sentinal->next = sentinal->prev = sentinal;
    sentinal->iter_count = 1; // prevent the sentinal from being deleted
    self->count = 0;

    return (PyObject *)self;
}
Ejemplo n.º 12
0
node *make_new_root(node *left, node *right, char *key,unsigned long key_id)
{
    node *root;
    root = make_new_node();
    root->pointers[0] = left;
    root->pointers[1] = right;
    root->keys[0] = malloc(MAX_KEY_LEN);
    strcpy(root->keys[0], key);
    root->num_keys++;
    left->parent = root;
    right->parent = root;

    root->block->pointers_id[0] = left->block->node_id;
    root->block->pointers_id[1] = right->block->node_id;
    root->block->keys_id[0] = key_id;
    root->block->num_keys = root->num_keys;
    left->block->parent_id = root->block->node_id;
    right->block->parent_id = root->block->node_id;

    return root;
}
Ejemplo n.º 13
0
node *insert_into_node_after_splitting(node *root, node *nd, node *right, int index, char *key,unsigned long key_id)
{
    int i, split;
    node **temp_ps, *new_nd, *child;
    char **temp_ks, *new_key;
    unsigned long *temp_ps_block,*temp_ks_block;
    unsigned long new_key_id; 
    
    temp_ps = malloc((size + 1) * sizeof(node *));
    temp_ks = malloc(size * sizeof(char *));
    
    temp_ps_block = malloc((size+1)*sizeof(unsigned long));
    temp_ks_block = malloc(size*sizeof(unsigned long));

    for (i = 0; i < size + 1; i++){
        if (i == index + 1){
            temp_ps[i] = right;
            temp_ps_block[i] = right->block->node_id; 
        }
        else if (i < index + 1){
            temp_ps[i] = nd->pointers[i];
            temp_ps_block[i] = nd->block->pointers_id[i];
        }
        else {    
            temp_ps[i] = nd->pointers[i-1];
            temp_ps_block[i] = nd->block->pointers_id[i-1];
        }
    }
  #if 1 
    for (i = 0; i < size; i++){
        if (i == index){
            temp_ks[i] = malloc(MAX_KEY_LEN);
            strcpy(temp_ks[i], key);
            temp_ks_block[i] = key_id;
        }
        else if (i < index){
            temp_ks[i] = nd->keys[i];
            temp_ks_block[i] = nd->block->keys_id[i];
        }
        else {
            temp_ks[i] = nd->keys[i-1];
            temp_ks_block[i] = nd->block->keys_id[i-1];
        }
    }
#endif
    split = size % 2 ? size / 2 + 1 : size / 2;  // split is #pointers
    nd->num_keys = split - 1;
    nd->block->num_keys = split-1; 
    for (i = 0; i < split - 1; i++){
        nd->pointers[i] = temp_ps[i];
        nd->keys[i] = temp_ks[i];
        nd->block->pointers_id[i] =  temp_ps_block[i];
        nd->block->keys_id[i] = temp_ks_block[i];
    }
    nd->pointers[i] = temp_ps[i];  // i == split - 1
    nd->block->pointers_id[i] = temp_ps_block[i];
    new_key = temp_ks[split - 1];
    new_key_id = temp_ks_block[split - 1];    


    new_nd = make_new_node();
    new_nd->num_keys = size - split;
    new_nd->block->num_keys= size - split;

    for (++i; i < size; i++){
        new_nd->pointers[i - split] = temp_ps[i];
        new_nd->keys[i - split] = temp_ks[i];
        new_nd->block->pointers_id[i - split] = temp_ps_block[i];
        new_nd->block->keys_id[i - split] = temp_ks_block[i];
    
    }
    new_nd->pointers[i - split] = temp_ps[i];
    new_nd->block->pointers_id[i - split] = temp_ps_block[i];
    new_nd->parent = nd->parent;
    new_nd->block->parent_id = nd->parent->block->node_id;

    for (i = 0; i <= new_nd->num_keys; i++){  //  #pointers == num_keys + 1
        child = (node *)(new_nd->pointers[i]);
        child->parent = new_nd;
        child->block->parent_id = new_nd->block->node_id;
    }

    free(temp_ps);
    free(temp_ks);
    free(temp_ps_block);
    free(temp_ks_block);

    return insert_into_parent(root, nd, new_nd, new_key,new_key_id);
}