0 Replies - 513 Views - Last Post: 22 September 2008 - 10:37 AM Rate Topic: -----

#1 GWatt   User is offline

  • member icon

Reputation: 309
  • View blog
  • Posts: 3,105
  • Joined: 01-December 05

Vector

Posted 22 September 2008 - 10:37 AM

Description: You must define: struct VECTOR { void** contents; int len, cap; }; and then: typedef typeof(struct VECTOR*) vector; somewhere for this code to work. I have it defined in a separate header file along with these functions: vector vector_new(); void vector_destroy(vector); vector vector_extract(vector,int,int); vector vector_merge(vector,vector); void vector_add(vector,void*); int vector_insert(vector,void*,int); void* vector_set(vector,void*,int); void* vector_remove(vector,int); void* vector_get(vector,int); This code provides the functionality of a vector in an entirely C setting. I don't know why you would use this as opposed to the vector class in C++, but it was fun to write.
/*
 * vector.c
 *
 * Graham Watt
*/

//#include "vector.h"
 /*
 I put all of my definitions in vector.h
 If you do the same, then un-comment that line
*/
#include 

static void* vector_increase(vector);

vector vector_new()
{
	vector vec = (vector) malloc(sizeof(struct VECTOR) );
	vec->len = 0;
	vec->cap = 0;
	vec->contents = malloc(sizeof(void*) * 10);
	vec->contents = (void*) vector_increase(vec);
	return vec;
}

void vector_add(vector vec, void* element)
{
	if (vec->len + 1 > vec->cap)
		vec->contents = vector_increase(vec);
	vec->contents[vec->len++] = element;
}

int vector_insert(vector vec, void* element, int index)
{
	if (vec->len + 1 > vec->cap)
		vec->contents = vector_increase(vec);
	if (index > vec->len || index < 0)
	{
		int i;
		for (i = vec->len ; i >= index; i--)
		{
			if (vec->len > 0)
				vec->contents[i + 1] = vec->contents[i];
		}
		vec->contents[index] = element;
		vec->len++;
		return 1;
	}
	else return 0;
}

void* vector_remove(vector vec, int index)
{
	if (index < vec->len || index < 0)
	{
		int i;
		void* result = vec->contents[index];
		for (i = index; i < vec->len - 1; i++)
		{
			vec->contents[i] = vec->contents[i + 1];
		}
		vec->len--;
		return result;
	}
	else return NULL;
}

void* vector_get(vector vec, int index)
{
	if (index < vec->len)
		return vec->contents[index];
	else return NULL;
}

void* vector_set(vector vec, void* item, int index)
{
	if (index >= vec->len || index < 0)
		return NULL;
	else
	{
		void* result = vec->contents[index];
		vec->contents[index] = item;
		return result;
	}
}

vector vector_extract(vector vec, int start, int length)
{
	if (start + length > vec->len || start < 0)
		return NULL;
	else
	{
		vector result = vector_new();
		int i;
		for (i = 0; i < length; i++)
			vector_add(result, vector_get(vec, i) );
		return result;
	}
}

vector vector_merge(vector vecOne, vector vecTwo)
{
	int i;
	for (i = 0; i < vecTwo->len; i++)
		vector_add(vecOne, vector_get(vecTwo, i) );
	return vecOne;
}

static void* vector_increase(vector vec)
{
	vec->contents = realloc(vec->contents, sizeof(void*) * (vec->cap + 10 ) );
	vec->cap += 10;
	return vec->contents;
}

void vector_destroy(vector vec)
{
	free(vec->contents);
	free(vec);
}


Is This A Good Question/Topic? 0
  • +

Replies To: Vector

#2 GWatt   User is offline

  • member icon

Reputation: 309
  • View blog
  • Posts: 3,105
  • Joined: 01-December 05

Re: Vector

Posted 22 September 2008 - 10:37 AM

Description: You must define:
struct VECTOR
{
void** contents;
int len, cap;
};

and then:
typedef typeof(struct VECTOR*) vector;

somewhere for this code to work.
I have it defined in a separate header file along with these functions:
vector vector_new();
void vector_destroy(vector);
void vector_add(vector,void*);
int vector_insert(vector,void*,int);
void* vector_set(vector,void*,int);
void* vector_remove(vector,int);
void* vector_get(vector,int);This code provides the functionality of a vector in an entirely C setting. I don't know why you would use this as opposed to the vector class in C++, but it was fun to write.
/*
 * vector.c
 *
 * Graham Watt
 */

#include "vector.h"
#include <stdlib.h>

static void* vector_increase(vector);

vector vector_new()
{
	vector vec = (vector) malloc(sizeof(struct VECTOR) );
	vec->len = 0;
	vec->cap = 0;
	vec->contents = malloc(sizeof(void*) * 10);
	vec->contents = (void*) vector_increase(vec);
	return vec;
}

void vector_add(vector vec, void* element)
{
	if (vec->len + 1 > vec->cap)
		vec->contents = vector_increase(vec);
	vec->contents[vec->len++] = element;
}

int vector_insert(vector vec, void* element, int index)
{
	if (vec->len + 1 > vec->cap)
		vec->contents = vector_increase(vec);
	if (index > vec->len || index < 0)
	{
		int i;
		for (i = vec->len ; i >= index; i--)
		{
			if (vec->len > 0)
				vec->contents[i + 1] = vec->contents[i];
		}
		vec->contents[index] = element;
		vec->len++;
		return 1;
	}
	else return 0;
}

void* vector_remove(vector vec, int index)
{
	if (index < vec->len || index < 0)
	{
		int i;
		void* result = vec->contents[index];
		for (i = index; i < vec->len - 1; i++)
		{
			if (vec->len > 1)
				vec->contents[i] = vec->contents[i + 1];
		}
		vec->len--;
		return result;
	}
	else return NULL;
}

void* vector_get(vector vec, int index)
{
	if (index < vec->len)
		return vec->contents[index];
	else return NULL;
}

void* vector_set(vector vec, void* item, int index)
{
	if (index >= vec->len || index < 0)
		return NULL;
	else
	{
		void* result = vec->contents[index];
		vec->contents[index] = item;
		return result;
	}
}

static void* vector_increase(vector vec)
{
	vec->contents = realloc(vec->contents, sizeof(void*) * (vec->cap + 10 ) );
	vec->cap += 10;
	return vec->contents;
}

void vector_destroy(vector vec)
{
	free(vec->contents);
	free(vec);
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1