示例#1
0
文件: list.c 项目: kroltan/ifcgame
List *list_new() {
	List *list = malloc(sizeof(List));
	list->capacity = 0;
	list->length = 0;
	list->data = NULL;
	list_ensure_capacity(list, MIN_CAPACITY);
	return list;
}
示例#2
0
/** Sets the length of the list to a specific value and increases the
    capacity to match the length (if the existing capacity is too
    small). This function does *not* reduce the amount of space
    allocated (i.e., the 'capacity') for the list if the length of the
    list is decreased. list_reclaim() or list_set_capacity() can be
    used to reduce the capacity.

    @param l The new length of the list.
    
    @return 1 if successful, 0 if failed. Failure can happen if the
    list is NULL, the specified length is negative, or if we need to
    expand the capacity of the list and failed to do so.
 */
int list_set_length(list *l, int length)
{
	if(l == NULL || length < 0)
		return 0;

	if(list_ensure_capacity(l, length) == 0)
		return 0;

	l->length = length;
	return 1;
}
示例#3
0
/** Sets an item in the list. If more space needs to be allocated, the
    capacity of the list is updated so that it is just large enough to
    hold the new item.

    @param l The list that the item should be added into.
    
    @param index The index to store the item in.
    
    @param item A pointer to the item to store.
    
    @return 1 if successful, 0 if failure. Failure can occur if we
    needed to expand the list and were unable to expand it.
*/
int list_set(list *l, int index, void *item)
{
	list_sanity_check(l);
	if(list_ensure_capacity(l, index+1) == 0)
		return 0;

	/* Update the length of our list if necessary. We need to do this
	 * before we call list_getptr() so that it will find the index to
	 * be in bounds. */
	if(l->length < index+1)
		l->length = index+1;
	
	// void* arithmetic is not allowed except as a GCC extension. Cast to char*:
	void *dest = list_getptr(l, index);
	if(dest != item)
		memcpy(dest, item, l->itemSize);

	return 1;
}
示例#4
0
/** Appends an item onto the end of the list (at index l->length). If
    the capacity of the list is too small, it will be doubled.

    @param l The list to append an item onto.

    @param item The item to append to the end of the list.
    
    @return 1 if successful, 0 if failure. Failure can occur if we
    needed to expand the capacity of the list and we failed to do
    so. Failure can also occur if the list or the item is NULL.
*/
int list_append(list *l, void *item)
{
	list_sanity_check(l);

	if(item == NULL)
	{
		msg(ERROR, "The pointer to the item to append to the list was NULL.");
		return 0;
	}

	/* Make sure that we double the capacity when needed. list_set()
	 * would also allocate more space for us---but will only increase
	 * the capacity just as large as needed. */
	if(l->length == l->capacity)
		if(list_ensure_capacity(l, l->capacity*2) == 0)
			return 0;

	return list_set(l, l->length, item);
}
示例#5
0
文件: list.c 项目: kroltan/ifcgame
static inline void list_ensure_extra_items(List *list, int delta) {
	list_ensure_capacity(list, list->length + delta);
}