Exemplo n.º 1
0
struct token *lexer_next_token(struct lexer_book *bk, struct dag_lookup_set *s)
{
    struct token *head;
    if(list_size(bk->token_queue) == 0) {
        if(bk->eof)
            return NULL;

        lexer_read_line(bk);
        return lexer_next_token(bk, s);
    }

    head = list_pop_head(bk->token_queue);

    if(head->type == SUBSTITUTION) {
        char *substitution = dag_lookup(head->lexeme, s);
        struct lexer_book *bk_s;

        if(!substitution) {
            debug(D_NOTICE, "Variable %s has not yet been defined at line %d.\n", head->lexeme, bk->line_number);
            return lexer_next_token(bk, s);
        }

        bk_s = lexer_init_book(STRING, dag_lookup(head->lexeme, s), bk->line_number, bk->column_number);

        lexer_append_all_tokens(bk, bk_s);

        lexer_free_book(bk_s);
        lexer_free_token(head);

        head = list_pop_head(bk->token_queue);
    }

    return head;
}
Exemplo n.º 2
0
int lexer_read_command(struct lexer *lx)
{
	struct list *tokens = lexer_read_command_aux(lx);

	struct token *t;

	if(list_size(tokens) < 2) {
		/* If the only token in the list is a NEWLINE, then this is an empty line. */
		while((t = list_pop_head(tokens)))
			lexer_free_token(t);

		list_delete(tokens);
		return 1;
	}

	/* Add command start marker.*/
	lexer_push_token(lx, lexer_pack_token(lx, TOKEN_COMMAND));


	/* Merge command tokens into main queue. */
	/* First merge command modifiers, if any. */
	list_first_item(tokens);
	while((t = list_peek_head(tokens))) {
		if(t->type == TOKEN_LITERAL &&
		   ((strcmp(t->lexeme, "LOCAL")    == 0) ||
			(strcmp(t->lexeme, "MAKEFLOW") == 0)    )) {
			t = list_pop_head(tokens);
			lexer_push_token(lx, t);
		} else if(t->type == TOKEN_SPACE) {
			//Discard spaces between modifiers.
			t = list_pop_head(tokens);
			lexer_free_token(t);
		} else {
			break;
		}
	}

	/* Mark end of modifiers. */
	lexer_push_token(lx, lexer_pack_token(lx, TOKEN_COMMAND_MOD_END));

	/* Now merge tha actual command tokens */

	/* Gives the number of actual command tokens, not taking into account command modifiers. */
	int count = 0;

	while((t = list_pop_head(tokens))) {
		count++;
		lexer_push_token(lx, t);
	}

	list_delete(tokens);

	if(count < 1)
		lexer_report_error(lx, "Command is empty.\n");

	return count;
}
Exemplo n.º 3
0
static INT64_T do_put_one_dir(const char *hostport, const char *source_file, const char *target_file, int mode, time_t stoptime)
{
	char new_source_file[CHIRP_PATH_MAX];
	char new_target_file[CHIRP_PATH_MAX];
	struct list *work_list;
	const char *name;
	INT64_T result;
	INT64_T total = 0;

	struct dirent *d;
	DIR *dir;

	work_list = list_create();

	result = chirp_reli_mkdir(hostport, target_file, mode, stoptime);
	if(result == 0 || errno == EEXIST) {
		result = 0;
		dir = opendir(source_file);
		if(dir) {
			while((d = readdir(dir))) {
				if(!strcmp(d->d_name, "."))
					continue;
				if(!strcmp(d->d_name, ".."))
					continue;
				list_push_tail(work_list, strdup(d->d_name));
			}
			closedir(dir);
			while((name = list_pop_head(work_list))) {
				sprintf(new_source_file, "%s/%s", source_file, name);
				sprintf(new_target_file, "%s/%s", target_file, name);
				result = chirp_recursive_put(hostport, new_source_file, new_target_file, stoptime);
				free((char *) name);
				if(result < 0)
					break;
				total += result;
			}
		} else {
			result = -1;
		}
	} else {
		result = -1;
	}

	while((name = list_pop_head(work_list)))
		free((char *) name);

	list_delete(work_list);

	if(result < 0) {
		return -1;
	} else {
		return total;
	}
}
Exemplo n.º 4
0
struct process_info *process_wait(int timeout)
{
	struct process_info *p;

	if(!complete_list)
		complete_list = list_create();

	p = list_pop_head(complete_list);
	if(p)
		return p;

	process_work(timeout);

	return list_pop_head(complete_list);
}
Exemplo n.º 5
0
void categories_initialize(struct hash_table *categories, struct rmsummary *top, const char *summaries_file) {
	struct list *summaries = rmsummary_parse_file_multiple(summaries_file);

	if(!summaries) {
		fatal("Could not read '%s' file: %s\n", strerror(errno));
	}

	char *name;
	struct category *c;
	hash_table_firstkey(categories);
	while(hash_table_nextkey(categories, &name, (void **) &c)) {
		category_clear_histograms(c);
		if(c->first_allocation) {
			rmsummary_delete(c->first_allocation);
			c->first_allocation = rmsummary_create(-1);
		}
	}

	struct rmsummary *s;
	list_first_item(summaries);
	while((s = list_pop_head(summaries))) {
		if(s->category) {
			c = category_lookup_or_create(categories, s->category);
			category_accumulate_summary(c, s, NULL);
		}
		rmsummary_delete(s);
	}

	hash_table_firstkey(categories);
	while(hash_table_nextkey(categories, &name, (void **) &c)) {
		category_update_first_allocation(c, NULL);
		category_clear_histograms(c);
	}
}
Exemplo n.º 6
0
/* !DO NOT ALTER FUNCTION SIGNATURE! */
int callback_ofc_capable_switch_ofc_logical_switches (void ** data, XMLDIFF_OP op, xmlNodePtr node, struct nc_err** error)
{
	nc_verb_verbose("%s: data=%p, op=%d\n", __PRETTY_FUNCTION__, data, op);
	print_element_names(node, 0);

	int rv = EXIT_SUCCESS;
	if (NULL != node->children) {

		assert(ofc_state.lsi_list);
		assert(XMLDIFF_CHAIN & op);

		if ((XMLDIFF_ADD|XMLDIFF_REM|XMLDIFF_MOD|XMLDIFF_CHAIN) & op) {

			struct lsi *current_lsi;
			while ( (current_lsi = list_next(ofc_state.lsi_list)) ) {
				handle_ports(current_lsi->res.port_list_del);
			}

			while ( (current_lsi = list_pop_head(ofc_state.lsi_list)) ) {
				handle_ports(current_lsi->res.port_list_add);
				lsi_cleanup(current_lsi);
			}

		} else {
			nc_verb_error("unsupported op");
			assert(0);
		}

		list_delete(ofc_state.lsi_list);
		ofc_state.lsi_list = NULL;
	}

	return rv;
}
Exemplo n.º 7
0
void* list_remove (List* list, int32_t index) {
    assert(NULL != list);
    assert(0 <= index);
    assert(index < list->count);

    if (0 == index) {
        return list_pop_head(list);
    } else if (index == list->count - 1) {
        return list_pop_tail(list);
    } else if (index >= list->count) {
        return false;
    }

    ListItem* item = NULL;

    // we need the item previous to the removal index
    __LIST_GET(item, list, index - 1);

    ListItem* rem_item = item->next;
    void*     data     = rem_item->data;

    item->next = rem_item->next;
    list->count--;

    free(rem_item);

    return data;
}
Exemplo n.º 8
0
void lexer_roll_back_one(struct lexer_book *bk)
{
	int c = *bk->lexeme_end;

	if( c == '\n' )
	{

		bk->line_number--;
		bk->column_number = (uintptr_t) list_pop_head(bk->column_numbers);
	}
	else if( c == CHAR_EOF )
	{
		bk->eof = 0;
		bk->column_number--;
	}
	else
	{
		bk->column_number--;
	}

	if( bk->lexeme_end == bk->buffer )
		bk->lexeme_end = (bk->buffer + 2*BUFFER_CHUNK_SIZE);

	bk->lexeme_end--;

	if( *bk->lexeme_end == '\0' )
		bk->lexeme_end--;
}
Exemplo n.º 9
0
static void
handle_ports(void *list)
{
	if (NULL != list) {

		// handle ports
		struct port *p;
		while ((p = list_pop_head(list))) {

			nc_verb_verbose("dpid=%lx port %s with op=%u\n", p->dpid, p->resource_id, p->op);
			if (ADD == p->op) {
				// attach port
				port_attach(ofc_state.xmp_client_handle, p->dpid, p->resource_id);
			} else if (DELETE == p->op) {
				// detach port
				port_detach(ofc_state.xmp_client_handle, p->dpid, p->resource_id);
			} else {
				assert(0);
			}

			xmlFree(p->resource_id);
			free(p);
		}
	}
}
Exemplo n.º 10
0
static INT64_T do_get_one_dir(const char *hostport, const char *source_file, const char *target_file, int mode, time_t stoptime)
{
	char new_source_file[CHIRP_PATH_MAX];
	char new_target_file[CHIRP_PATH_MAX];
	struct list *work_list;
	const char *name;
	INT64_T result;
	INT64_T total = 0;

	work_list = list_create();

	result = mkdir(target_file, mode);
	if(result == 0 || errno == EEXIST) {
		result = chirp_reli_getdir(hostport, source_file, add_to_list, work_list, stoptime);
		if(result >= 0) {
			while((name = list_pop_head(work_list))) {
				if(!strcmp(name, "."))
					continue;
				if(!strcmp(name, ".."))
					continue;
				sprintf(new_source_file, "%s/%s", source_file, name);
				sprintf(new_target_file, "%s/%s", target_file, name);
				result = chirp_recursive_get(hostport, new_source_file, new_target_file, stoptime);
				free((char *) name);
				if(result < 0)
					break;
				total += result;
			}
		} else {
			result = -1;
		}
	} else {
		result = -1;
	}

	while((name = list_pop_head(work_list)))
		free((char *) name);

	list_delete(work_list);

	if(result >= 0) {
		return total;
	} else {
		return -1;
	}
}
Exemplo n.º 11
0
void rmonitor_file_watch_info_free(struct rmonitor_file_watch_info *f) {
    struct rmonitor_file_watch_event *e;
    list_first_item(f->events);
    while((e = list_pop_head(f->events))) {
        rmonitor_file_watch_event_free(e);
    }
    list_delete(f->events);
    free(f);
}
Exemplo n.º 12
0
void lexer_concatenate_consecutive_literals(struct list *tokens)
{
	struct list *tmp = list_create();
	struct token *t, *prev  = NULL;

	list_first_item(tokens);
	while((t = list_pop_head(tokens))) {
		if(t->type != TOKEN_LITERAL) {
			list_push_tail(tmp, t);
			continue;
		}

		prev = list_pop_tail(tmp);

		if(!prev) {
			list_push_tail(tmp, t);
			continue;
		}

		if(prev->type != TOKEN_LITERAL) {
			list_push_tail(tmp, prev);
			list_push_tail(tmp, t);
			continue;
		}

		char *merge = string_format("%s%s", prev->lexeme, t->lexeme);
		lexer_free_token(t);
		free(prev->lexeme);
		prev->lexeme = merge;

		list_push_tail(tmp, prev);
	}

	/* Copy to tokens, drop spaces. */
	list_first_item(tmp);
	while((t = list_pop_head(tmp)))
		if(t->type != TOKEN_SPACE) {
			list_push_tail(tokens, t);
		} else {
			lexer_free_token(t);
		}

	list_delete(tmp);
}
Exemplo n.º 13
0
void delete_projects_list(struct list *l)
{
	if(l) {
		struct jx *j;
		while((j=list_pop_head(l))) {
			jx_delete(j);
		}
		list_delete(l);
	}
}
Exemplo n.º 14
0
void delete_projects_list(struct list *l)
{
	if(l) {
		struct nvpair *nv;
		while((nv=list_pop_head(l))) {
			nvpair_delete(nv);
		}
		list_delete(l);
	}
}
Exemplo n.º 15
0
void channel_handle_client_read(connector_t pconn, int event)
{
    //由于和客户端只有一次交互,不用一直读取
    if (connector_read(pconn, event) > 0)
    {
        char *val = buffer_get_read(pconn->preadbuf);
        message_t pmsg = (message_t)malloc(sizeof(message));
        memset(pmsg, 0, sizeof(pmsg));
        size_t len1 = get_client_msg(val, pmsg);

        if (len1 == 0)
        {
            print_log(LOG_TYPE_ERROR, "Read Client Msg Error %s", val);
            free(pmsg);
            return;
        }

        char data[20] = {0};
        memcpy(data, pmsg->uid, pmsg->len);
        buffer_read(pconn->preadbuf, len1, TRUE);

        memcpy(pconn->uid, data, pmsg->len);

        int len2 = sizeof(connector_t);
        ht_insert(pconn->pworker->pht, data, (pmsg->len)+1, pconn, len2+1);

        context_t pcontext = (context_t)malloc(sizeof(context));
        memset(pcontext, 0, sizeof(context));
        memcpy(pcontext->data, data, pmsg->len);
        list_push_tail(pconn->pworker->plist, pcontext);

        //print_log(LOG_TYPE_DEBUG, "Hash key %s, Len %d", pcontext->data, pmsg->len);

        char cmd[REDIS_CMD_LEN] = {'\0'};
        get_request_str(data, cmd);

        int len = strlen(cmd);

        if (pconn->pworker->redis->state == CONN_STATE_RUN)
        {
            buffer_write(pconn->pworker->redis->pwritebuf, cmd, len);
            connector_write(pconn->pworker->redis);
        }
        else
        {
            print_log(LOG_TYPE_ERROR, "Redis not run");
            list_pop_head(pconn->pworker->plist);
            ht_remove(pconn->pworker->pht, data, (pmsg->len)+1);
            pconn->pworker->neterr_count++;
        }

        free(pmsg);
    }
}
/*--------------------------------------------
Author: Max Ashton
Description: remove element from head then decrease list size
----------------------------------------------*/
struct_list_element* remove_element_head( maStructList &list )
{
	struct_list_element* pdPoped = list_pop_head( list );

	if( pdPoped != NULL )
	{
		list._iListSize--;
	}

	return pdPoped;
}
Exemplo n.º 17
0
void* list_pop_head_ts (List* list) {
    assert(NULL != list);
    assert(NULL != list->mutex);

    pthread_mutex_lock(list->mutex);

    void* ret = list_pop_head(list);

    pthread_mutex_unlock(list->mutex);

    return ret;
}
Exemplo n.º 18
0
static int get_free_entry(struct mysql_login_info *info, struct list *l, struct mysql_pool_entry **mysql) {
    if (list_empty(l))
        return -1;
    struct list *item = list_pop_head(l);
    *mysql = LIST_ENT(item, struct mysql_pool_entry, l);
    // connect if haven't already done so.
    if (!((*mysql)->helper).is_connected) {
        if (0 > mysql_helper_connect(&(*mysql)->helper, info))
            return -1;
        ((*mysql)->helper).is_connected = 1;
    }
    return 0;
}
Exemplo n.º 19
0
int lexer_append_tokens(struct lexer *lx, struct list *tokens)
{
	struct token *t;
	int count = 0;

	list_first_item(tokens);
	while((t = list_pop_head(tokens))) {
		lexer_push_token(lx, t);
		count++;
	}

	return count;
}
Exemplo n.º 20
0
struct token *lexer_next_token(struct lexer *lx)
{
	struct token *head = lexer_peek_next_token(lx);

	if(head)
	{
		if(lx->depth == 0)
			debug(D_MAKEFLOW_LEXER, "%s", lexer_print_token(head));

		list_pop_head(lx->token_queue);
	}

	return head;
}
Exemplo n.º 21
0
void channel_handle_redis_read(connector_t pconn, int event)
{
    //修复一个问题,Redis返回的数据已经读取到了缓冲区,不能只读取一个业务包
    if (connector_read(pconn, event) > 0)
    {
        while (buffer_readable(pconn->preadbuf) > 0)
        {
            char *origin = buffer_get_read(pconn->preadbuf);
            char analyse[100] = {0};
            int originlen = get_analyse_data(origin, analyse);

            if (originlen == 0)
            {
                print_log(LOG_TYPE_DEBUG, "buffer no value");
                break;
            }

            buffer_read(pconn->preadbuf, originlen, TRUE);

            if (strcmp(analyse, REDIS_HBVAL) == 0)
                return;

            context_t pcontext = (context_t)pconn->pworker->plist->head->value;
            //print_log(LOG_TYPE_DEBUG, "Redis Read %s List Head Uid %s", analyse, pcontext->data);

            char key[UID_MAX_LEN] = {0};
            memcpy(key, pcontext->data, strlen(pcontext->data));
            MEM_FREE(pcontext);
            list_pop_head(pconn->pworker->plist);

            size_t len1 = strlen(key);
            size_t len2 = 0;
            connector_t pclientcon = (connector_t)ht_get(pconn->pworker->pht, key, len1+1, &len2);

            if (pclientcon)
            {
                ht_remove(pconn->pworker->pht, key, len1+1);

                char val[100] = {0};
                get_response_str(val, key, analyse);
                size_t size = strlen(val);

                buffer_write(pclientcon->pwritebuf, val, size);
                connector_write(pclientcon);
            }
        }
    }
}
Exemplo n.º 22
0
void mpi_queue_job_delete(struct mpi_queue_job *job) {

	if(job->output_length) {
		free(job->output);
	}
	
	while(list_size(job->operations)) {
		struct mpi_queue_operation *tmp = list_pop_head(job->operations);
		if(tmp->buffer_length)
			free(tmp->buffer);
		if(tmp->output_length)
			free(tmp->output_buffer);
		free(tmp);
	}
	list_delete(job->operations);
	free(job);
}
Exemplo n.º 23
0
struct http_client_context *http_client_pool_create_client(struct http_client_pool *http_client_pool, struct in_addr addr, uint16_t port, struct ribs_context *rctx) {
    int cfd;
    struct http_client_key key = { .addr = addr, .port = port };
    uint32_t ofs = hashtable_lookup(&ht_persistent_clients, &key, sizeof(struct http_client_key));
    struct list *head;
    if (ofs > 0 && !list_empty(head = client_heads + *(uint32_t *)hashtable_get_val(&ht_persistent_clients, ofs))) {
        struct list *client = list_pop_head(head);
        cfd = client - client_chains;
    } else {
        cfd = socket(PF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);
        if (0 > cfd)
            return LOGGER_PERROR("socket"), NULL;

        const int option = 1;
        if (0 > setsockopt(cfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option)))
            return LOGGER_PERROR("setsockopt SO_REUSEADDR"), close(cfd), NULL;

        if (0 > setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY, &option, sizeof(option)))
            return LOGGER_PERROR("setsockopt TCP_NODELAY"), close(cfd), NULL;

        struct sockaddr_in saddr = { .sin_family = AF_INET, .sin_port = htons(port), .sin_addr = addr };
        if (0 > connect(cfd, (struct sockaddr *)&saddr, sizeof(saddr)) && EINPROGRESS != errno)
            return LOGGER_PERROR("connect"), close(cfd), NULL;

        struct epoll_event ev;
        ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
        ev.data.fd = cfd;
        if (0 > epoll_ctl(ribs_epoll_fd, EPOLL_CTL_ADD, cfd, &ev))
            return LOGGER_PERROR("epoll_ctl"), close(cfd), NULL;
    }
    struct ribs_context *new_ctx = ctx_pool_get(&http_client_pool->ctx_pool);
    new_ctx->fd = cfd;
    new_ctx->data.ptr = http_client_pool;
    struct epoll_worker_fd_data *fd_data = epoll_worker_fd_map + cfd;
    fd_data->ctx = new_ctx;
    ribs_makecontext(new_ctx, rctx ? rctx : current_ctx, http_client_fiber_main);
    struct http_client_context *cctx = (struct http_client_context *)new_ctx->reserved;
    cctx->key = (struct http_client_key) {
        .addr = addr, .port = port
    };
    vmbuf_init(&cctx->request, 4096);
    vmbuf_init(&cctx->response, 4096);
    timeout_handler_add_fd_data(&http_client_pool->timeout_handler, fd_data);
    return cctx;
}
Exemplo n.º 24
0
struct list *list_sort(struct list *list, int (*comparator) (const void *, const void *))
{
	void **array;
	int size, i = 0;

	size = list_size(list);
	array = malloc(size * sizeof(*array));
	while(list_size(list)) {
		array[i] = list_pop_head(list);
		i++;
	}
	qsort(array, size, sizeof(*array), comparator);
	for(i = 0; i < size; i++) {
		list_push_tail(list, array[i]);
	}
	free(array);
	return list;
}
Exemplo n.º 25
0
/*
 * Implements the UP operation on the specified kernel semaphore.
 */
void ksema_up(struct ksema *sem)
{
    unsigned long eflags;
    struct task_struct *t;

    disable_hwint(eflags);

    /* Increment the value of the semaphore. */
    sem->value++;

    /* Remove the first task from the semaphore's wait queue and wake it up! */
    if (!list_empty(sem->waiting_task_list_head)) {
        t = list_pop_head(sem->waiting_task_list_head);
        t->state = TASK_RUNNABLE;
    }

    restore_hwint(eflags);
}
Exemplo n.º 26
0
void http_client_free(struct http_client_pool *client_pool, struct http_client_context *cctx) {
    if (cctx->persistent) {
        int fd = RIBS_RESERVED_TO_CONTEXT(cctx)->fd;
        epoll_worker_set_fd_ctx(fd, idle_ctx);
        uint32_t ofs = hashtable_lookup(&ht_persistent_clients, &cctx->key, sizeof(struct http_client_key));
        struct list *head;
        if (0 == ofs) {
            if (list_empty(&free_list)) {
                close(fd);
                return;
            }
            head = list_pop_head(&free_list);
            uint32_t h = head - client_heads;
            hashtable_insert(&ht_persistent_clients, &cctx->key, sizeof(struct http_client_key), &h, sizeof(h));
            list_init(head);
        } else
            head = client_heads + *(uint32_t *)hashtable_get_val(&ht_persistent_clients, ofs);
        struct list *client = client_chains + fd;
        list_insert_head(head, client);
    }
    ctx_pool_put(&client_pool->ctx_pool, RIBS_RESERVED_TO_CONTEXT(cctx));
}
Exemplo n.º 27
0
/**
 * returns the depth of the given DAG.
 */
int dag_depth(struct dag *d)
{
	struct dag_node *n, *parent;
	struct dag_file *f;

	struct list *level_unsolved_nodes = list_create();
	for(n = d->nodes; n != NULL; n = n->next) {
		n->level = 0;
		list_first_item(n->source_files);
		while((f = list_next_item(n->source_files))) {
			if((parent = f->target_of) != NULL) {
				n->level = -1;
				list_push_tail(level_unsolved_nodes, n);
				break;
			}
		}
	}

	int max_level = 0;
	while((n = (struct dag_node *) list_pop_head(level_unsolved_nodes)) != NULL) {
		list_first_item(n->source_files);
		while((f = list_next_item(n->source_files))) {
			if((parent = f->target_of) != NULL) {
				if(parent->level == -1) {
					n->level = -1;
					list_push_tail(level_unsolved_nodes, n);
					break;
				} else {
					int tmp_level = parent->level + 1;
					n->level = n->level > tmp_level ? n->level : tmp_level;
					max_level = n->level > max_level ? n->level : max_level;
				}
			}
		}
	}
	list_delete(level_unsolved_nodes);

	return max_level + 1;
}
Exemplo n.º 28
0
struct token *lexer_concat_expandable(struct lexer *lx, struct list *tokens)
{
	struct token *t;

	struct buffer b;
	buffer_init(&b);

	char *substitution;

	list_first_item(tokens);

	while((t = list_pop_head(tokens))) {
		switch(t->type) {
		case TOKEN_SUBSTITUTION:
			substitution = dag_variable_lookup_string(t->lexeme, lx->environment);
			if(!substitution)
				fatal("Variable %s has not yet been defined at line % " PRId64 ".\n", t->lexeme, lx->line_number);
			buffer_printf(&b, "%s", substitution);
			free(substitution);
			break;
		case TOKEN_LITERAL:
			if(strcmp(t->lexeme, "") != 0)           // Skip empty strings.
				buffer_printf(&b, "%s", t->lexeme);
			break;
		default:
			lexer_report_error(lx, "Error in expansion, got: %s.\n", lexer_print_token(t));
			break;
		}

		lexer_free_token(t);
	}

	t = lexer_pack_token(lx, TOKEN_LITERAL);
	t->lexeme = xxstrdup(buffer_tostring(&b));
	buffer_free(&b);

	return t;
}
Exemplo n.º 29
0
void lexer_roll_back_one(struct lexer *lx)
{
	int c = *lx->lexeme_end;

	if(c == '\n') {

		lx->line_number--;
		lx->column_number = (uintptr_t) list_pop_head(lx->column_numbers);
	} else if(c == CHAR_EOF) {
		lx->eof = 0;
		lx->column_number--;
	} else {
		lx->column_number--;
	}

	if(lx->lexeme_end == lx->buffer)
		lx->lexeme_end = (lx->buffer + 2 * BUFFER_CHUNK_SIZE);

	lx->lexeme_end--;

	if(*lx->lexeme_end == '\0')
		lx->lexeme_end--;
}
Exemplo n.º 30
0
struct cluster *nearest_neighbor_clustering(struct list *initial_clusters, double (*cmp)(struct cluster *, struct cluster *))
{
	struct cluster *top, *closest, *subtop;
	struct list   *stack;
	struct itable *active_clusters;
	double dclosest, dsubtop;

	int merge = 0;

	list_first_item(initial_clusters);
	top = list_next_item(initial_clusters);

	/* Return immediately if top is NULL, or there is a unique
	 * initial cluster */
	if(list_size(initial_clusters) < 2)
		return top;

	stack = list_create(0);
	list_push_head(stack, top);

	/* Add all of the initial clusters as active clusters. */
	active_clusters = itable_create(0);
	while( (top = list_next_item(initial_clusters)) ) 
		itable_insert(active_clusters, (uintptr_t) top, (void *) top);

	do
	{
		/* closest might be NULL if all of the clusters are in
		 * the stack now. subtop might be NULL if top was the
		 * only cluster in the stack */
		top     = list_pop_head( stack );
		closest = cluster_nearest_neighbor(active_clusters, top, cmp);
		subtop  = list_peek_head( stack );

		dclosest = -1;
		dsubtop  = -1;

		if(closest)
			dclosest = cluster_ward_distance(top, closest);

		if(subtop)
			dsubtop = cluster_ward_distance(top, subtop);

		/* The nearest neighbor of top is either one of the
		 * remaining active clusters, or the second topmost
		 * cluster in the stack */
		if( closest && subtop )
		{
			/* Use pointer address to systematically break ties. */
			if(dclosest < dsubtop || ((dclosest == dsubtop) && (uintptr_t)closest < (uintptr_t)subtop)) 
				merge = 0;
			else 
				merge = 1;
		}
		else if( subtop )
			merge = 1;
		else if( closest )
			merge = 0;
		else
			fatal("Zero clusters?\n"); //We should never reach here.

		if(merge)
		{
			/* If the two topmost clusters in the stack are
			 * mutual nearest neighbors, merge them into a single
			 * cluster */
			subtop = list_pop_head( stack );
			list_push_head(stack, cluster_merge(top, subtop));
		}
		else
		{
			/* Otherwise, push the nearest neighbor of top to the
			 * stack */
			itable_remove(active_clusters, (uintptr_t) closest);
			list_push_head(stack, top);
			list_push_head(stack, closest);
		}

		debug(D_DEBUG, "stack: %d  active: %d  closest: %lf subtop: %lf\n", 
				list_size(stack), itable_size(active_clusters), dclosest, dsubtop);

		/* If there are no more active_clusters, but there is not
		 * a single cluster in the stack, we try again,
		 * converting the clusters in the stack into new active
		 * clusters. */
		if(itable_size(active_clusters) == 0 && list_size(stack) > 3)
		{
			itable_delete(active_clusters);
			return nearest_neighbor_clustering(stack, cmp);
		}

	}while( !(itable_size(active_clusters) == 0 && list_size(stack) == 1) );

	/* top is now the root of a cluster hierarchy, of
	 * cluster->right, cluster->left. */
	top = list_pop_head(stack);

	list_delete(stack);
	itable_delete(active_clusters);

	return top;
}