Git Product home page Git Product logo

vector's Introduction

vector

GitHub license

vector is a feature-complete, generic and customizable resizable array implementation in pure C that supports almost the entire C++ std::vector API, including iterators.

Usage

#include "vector.h"

int main(int argc, const char* argv[]) {
	Vector vector;
	int x, y, sum;

	/* Choose initial capacity of 10 */
	/* Specify the size of the elements you want to store once */
	vector_setup(&vector, 10, sizeof(int));

	x = 6, y = 9;
	vector_push_back(&vector, &x);
	vector_insert(&vector, 0, &y);
	vector_assign(&vector, 0, &y);

	x = *(int*)vector_get(&vector, 0);
	y = *(int*)vector_back(&vector);
	x = VECTOR_GET_AS(int, &vector, 1);

	vector_remove(&vector, 1);

	/* Iterator support */
	Iterator iterator = vector_begin(&vector);
	Iterator last = vector_end(&vector);
	for (; !iterator_equals(&iterator, &last); iterator_increment(&iterator)) {
		*(int*)iterator_get(&iterator) += 1;
	}

	/* Or just use pretty macros */
	sum = 0;
	VECTOR_FOR_EACH(&vector, i) {
		sum += ITERATOR_GET_AS(int, &i);
	}

	/* Memory management interface */
	vector_resize(&vector, 10);
	vector_reserve(&vector, 100);

	vector_clear(&vector);
	vector_destroy(&vector);
}

LICENSE

This project is released under the MIT License. For more information, see the LICENSE file.

Authors

Peter Goldsborough + cat ❤️

vector's People

Contributors

goldsborough avatar jchnkl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

vector's Issues

There is no append method for the vector

I'd like to confirm the understanding of the project is that, there is no implements for appending the vector data just like in STL, such as: vector1.insert(vector1.end(),vector2.begin(),vector2.end()).

Please tell me whether my understanding is true or false. If it is false, could you please give an example for the implementation of my requirement?

Safer Vector + VECTOR_ASSIGN_ARRAY

Due to void * based design, currently one can add whatever one wants to this vector. double's to vector of int's , bool's or whatever. Core helper to avoid this might be this macro

#if defined( __clang__ ) || defined( __GNUC__ )
#define VECTOR_CHECK( V_ , E_ ) V_.element_size == sizeof(typeof(E_))
#endif

Now we can use it for example for safe array assign to the vector

// from chromium
#define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))

// this might not fail on that many ocasions
#define VECTOR_ASSIGN_ARRAY(V_, S_, A_) \
do { \
assert( VECTOR_CHECK(V_, A_[0] ) ) ; \
for (int j = 0; j < S_ ; ++j) {\
	vector_push_back(&V_, &(A_[j]));\
};\
} while(0)

The usage

Vector vector;
// vector of int's
vector_setup(&vector, 10, sizeof(int));

int arr[] = { 1,2,3,4,5,6,7,8,9 };
VECTOR_ASSIGN_ARRAY(vector, COUNT_OF(arr), arr  );

printf("\n sum of: {");
int sum = 0;
VECTOR_FOR_EACH(&vector, iterator_ ) {
	sum += ITERATOR_GET_AS(int, &iterator_);
	printf(" %3d ", ITERATOR_GET_AS(int, &iterator_));
}
printf("} --> %d \n\n", sum );

If array element size and vector element size do not match VECTOR_CHECK will assert. Of course this is not type checking but mitigates the type mixing risk a little bit.

The code in vector_move may have problem

int vector_move(Vector* destination, Vector* source) {
assert(destination != NULL);
assert(source != NULL);

if (destination == NULL) return VECTOR_ERROR;
if (source == NULL) return VECTOR_ERROR;

*destination = *source;
source->data = NULL;

return VECTOR_SUCCESS;

}

should destination = source

safe twins

Basing a design on void * might not be very safe. One added safety measure might be to have a "safe twin" for every function trying to add a new element. For example

/* Insertion */
int vector_push_back_safe(Vector* vector, void* element, const size_t element_size_ ) 
{
	assert(vector != NULL);
	assert(element != NULL);

       if( vector->element_size != element_size_ ) {
          /* apply your error handling policy here */
       }
           return vector_push_back( vector, element ) ;
}

MSVC and pointer arithmetic

Hey Peter. Thank you for this implementation! So far I encountered only one problem, not a big deal actually, but MSVC is not happy with pointer arithmetic and throws some errors during compilation:

vector.c(375): error C2036: void *
vector.c(380): error C2036: void *
vector.c(415): error C2036: void *
vector.c(435): error C2036: void *
vector.c(439): error C2036: void *const
vector.c(473): error C2036: void *
vector.c(488): error C2036: void *

I fixed this with an explicit cast and just want to let you know that such a problem occurs.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.