コード例 #1
0
ファイル: HeapList.c プロジェクト: hansonry/opendwarf
void   HeapList_Init(HeapList_T * list, size_t element_size, size_t elements_per_block)
{
   list->element_size = element_size;
   list->count        = 0;
   if(elements_per_block == 0)
   {
      list->elements_per_block = DEFAULT_ELEMENTS_PER_BLOCK;
   }
   else
   {
      list->elements_per_block = elements_per_block;
   }
   ArrayList_Init(&list->block_list, sizeof(HeapList_Block_T), 0);
   ArrayList_Init(&list->unused_memory, sizeof(byte_t *), 0);
}
コード例 #2
0
ファイル: arraylist.c プロジェクト: stvschmdt/C-Array-List
/* TEST MAIN */
int main(void)
{
	 int n = 3;
	 int i;
	 int stat; // Error code
	 int size;
	 int val = 0;
	 int capacity;


	 // allocate list
	 ArrayList *list = NULL;
	 list = ArrayList_Init(n);
	 printf("ArrayList initialized to %d elements\n", n);
	 printf("Size of List = %d\n", list->size(list));
	 printf("Capacity of List = %d\n", list->capacity(list));

	 // Fill initial values

	 list->set(list, val++, 0);
	 list->set(list, val++, 1);
	 list->set(list, val++, 2);

	 // Print List
	 stat = list->print(list); // simple display of list contents
	 printf("Size of List = %d\n", list->size(list));
	 printf("Capacity of List = %d\n", list->capacity(list));

	 // Test append and auto-resize
	 stat = list->append(list,val++) ; // add val to end of the list
	 stat = list->print(list); // simple display of list contents
	 printf("Size of List = %d\n", list->size(list));
	 printf("Capacity of List = %d\n", list->capacity(list));

	 // Some more appends
	 stat = list->append(list,val++) ; // add val to end of the list
	 stat = list->append(list,val++) ; // add val to end of the list
	 stat = list->print(list); // simple display of list contents
	 printf("Size of List = %d\n", list->size(list));
	 printf("Capacity of List = %d\n", list->capacity(list));

	 // Test insert
	 stat = list->insert(list, val++, 1); // insert val into index (increasing ArrayList size by one, and bumping all values over)
	 stat = list->print(list); // simple display of list contents
	 printf("Size of List = %d\n", list->size(list));
	 printf("Capacity of List = %d\n", list->capacity(list));

	 // Test insert redux
	 stat = list->insert(list, val++, 4); // insert val into index (increasing ArrayList size by one, and bumping all values over)
	 stat = list->print(list); // simple display of list contents
	 printf("Size of List = %d\n", list->size(list));
	 printf("Capacity of List = %d\n", list->capacity(list));

	 // Test valueOf
	 stat = list->print(list); // simple display of list contents
	 printf("list[0] = %d\n", list->valueOf(list,0)) ;//return value of specified element
	 printf("list[1] = %d\n", list->valueOf(list,1)) ;//return value of specified element


	 // Test error handling
	 printf("insert error = %d\n", list->insert(list, val++, 1000));
	 printf("insert error = %d\n", list->insert(list, val++, -1000));
	 printf("set error = %d\n", list->set(list, val++, 1000));
	 printf("set error = %d\n", list->set(list, val++, -1000));
	 ArrayList *list2 = ArrayList_Init(0);
	 stat = list2->print(list2);
	 printf("bad print error = %d\n", stat);

	 // Test efficiency
	 // NOTE - grading script will kill your code if this takes > 3 seconds
	 val = 0;
	 printf("Efficiency test...\n");
	 for( i = 0; i < 10000000; i++ )
		  list2->append(list2, val++ );

	 // test destroy
	 stat = list->destroy(list); //free list memory
	 stat = list2->destroy(list2); //free list memory

	 return 0;
}