pax_global_header 0000666 0000000 0000000 00000000064 13272032507 0014513 g ustar 00root root 0000000 0000000 52 comment=55b723c4a570249f0827833a90baa47cb2c9b30f
cvector-CVector-1.0.4.1/ 0000775 0000000 0000000 00000000000 13272032507 0014664 5 ustar 00root root 0000000 0000000 cvector-CVector-1.0.4.1/CMakeLists.txt 0000664 0000000 0000000 00000013016 13272032507 0017425 0 ustar 00root root 0000000 0000000 ######################################################################
# CMakeLists.txt - cmake build file to create CVector #
# #
# Version 1.0.4 26 November 2014 #
# Rev 23 November 2014 #
# #
# Herbert J. Bernstein (yayahjb@gmail.com) #
# #
# (C) Copyright 2008 - 2014 Herbert J. Bernstein #
# #
######################################################################
######################################################################
# #
# YOU MAY REDISTRIBUTE THE CVector API UNDER THE TERMS OF THE LGPL #
# #
######################################################################
######################################################################
# #
# This library is free software; you can redistribute it and/or #
# modify it under the terms of the GNU Lesser General Public #
# License as published by the Free Software Foundation; either #
# version 2.1 of the License, or (at your option) any later version. #
# #
# This library is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
# Lesser General Public License for more details. #
# #
# You should have received a copy of the GNU Lesser General Public #
# License along with this library; if not, write to the Free #
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, #
# MA 02110-1301 USA #
# #
######################################################################
cmake_minimum_required (VERSION 2.8.7)
project (CVector C)
set(CV_APIVERS,"2.0.0")
#
# Environment Variables
#
set(CV_USE_LOCAL_HEADERS_ENV $ENV{CV_USE_LOCAL_HEADERS})
if (CV_USE_LOCAL_HEADERS_ENV STREQUAL "YES")
set(COMPILE_DEFINITIONS "USE_LOCAL_HEADERS=1")
endif (CV_USE_LOCAL_HEADERS_ENV STREQUAL "YES")
#
# Directories
#
#
# Directories on the kit side
#
set(CV__SRC "${CVector_SOURCE_DIR}" )
set(CV__INCLUDE "${CVector_SOURCE_DIR}" )
set(CV__EXAMPLES "${CVector_SOURCE_DIR}" )
set(CV__TESTDATA "${CVector_SOURCE_DIR}" )
#
# Directories on the build side
#
set(CV__BIN "${CVector_BINARY_DIR}/bin" )
set(CV__LIB "${CVector_BINARY_DIR}/lib" )
#
# CV_REQUIRE_DIRECTORY -- require directory dir
#
macro(CV_REQUIRE_DIRECTORY dir)
if (NOT EXISTS "${dir}")
file(MAKE_DIRECTORY "${dir}")
CBF_DEBUG_MESSAGE("Created directory ${dir}")
endif (NOT EXISTS "${dir}")
endmacro(CV_REQUIRE_DIRECTORY)
CV_REQUIRE_DIRECTORY(${CV__BIN})
CV_REQUIRE_DIRECTORY(${CV__LIB})
#
# Source files
#
set(
CV_C_SOURCES
${CV__SRC}/CVector.c
)
set(
CV_HEADERS
${CV__INCLUDE}/CVector.h
)
# Set up the necessary includes
include_directories(
BEFORE SYSTEM
${CV__INCLUDE}
)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CV__BIN}")
#
# Build the static and shared CVector libraries
#
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CV__LIB}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CV__LIB}")
add_library(CV_static STATIC ${CV__SRC}/CVector.c)
set_target_properties(CV_static PROPERTIES OUTPUT_NAME "CVector")
set_target_properties(CV_static PROPERTIES LINKER_LANGUAGE C)
set_target_properties(CV_static PROPERTIES SOVERSION "${CV_APIVERSION}")
set(CV_STATIC_LIBRARY_PATH ${CV__LIB}/libCVector${CMAKE_STATIC_LIBRARY_SUFFIX})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CV__LIB}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CV__LIB}")
add_library(CV_shared SHARED ${CV__SRC}/CVector.c)
set_target_properties(CV_shared PROPERTIES OUTPUT_NAME "CVector")
set_target_properties(CV_shared PROPERTIES LINKER_LANGUAGE C)
set_target_properties(CV_shared PROPERTIES SOVERSION "${CV_APIVERSION}")
set(CV_SHARED_LIBRARY_PATH ${CV__LIB}/libCVector.${CMAKE_SHARED_LIBRARY_SUFFIX})
#
# C examples
#
# Note: to add a target with multiple sources/dependencies/libraries you must pass a list
# separated by semicolons for the appropriate parameter.
# Note: the math library should be linked in by appending ';m' to the library list here
macro(add_target target source dependencies libraries)
add_executable(${target} ${source})
add_dependencies(${target} ${dependencies})
target_link_libraries(${target} ${libraries})
endmacro()
add_executable(CVectorBasicTest "${CV__EXAMPLES}/CVectorBasicTest.c")
set_target_properties(CVectorBasicTest PROPERTIES LINK_LIBRARIES CV_static)
#
# install
#
install (TARGETS CVectorBasicTest DESTINATION bin)
install (TARGETS CV_static DESTINATION lib)
install (TARGETS CV_shared DESTINATION lib)
#
# test
#
enable_testing()
add_test(CVectorBasicTest ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/CVectorBasicTest CVectorBasicTest.lst)
add_test(cmp-CVectorBasicTest_orig.lst-CVectorBasicTest.lst ${CMAKE_COMMAND} -E compare_files ${CV__TESTDATA}/CVectorBasicTest_orig.lst CVectorBasicTest.lst)
cvector-CVector-1.0.4.1/CVector.c 0000664 0000000 0000000 00000031445 13272032507 0016404 0 ustar 00root root 0000000 0000000 /*
* CVector.c
* CVector
*
* Created by Herbert J. Bernstein on 11/28/08.
* Copyright 2008 Herbert J. Bernstein. All rights reserved.
*
*/
/**********************************************************************
* *
* YOU MAY REDISTRIBUTE THE CVector API UNDER THE TERMS OF THE LGPL *
* *
**********************************************************************/
/************************* LGPL NOTICES *******************************
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free *
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
* MA 02110-1301 USA *
* *
**********************************************************************/
#ifdef __cplusplus
extern "C" {
#endif
#ifdef USE_LOCAL_HEADERS
#include "CVector.h"
#else
#include
#endif
/* CVectorCreate -- create a generic vector */
int CVectorCreate(CVectorHandle CVECTOR_FAR * vectorhandle, const size_t elementsize, const size_t capacity) {
size_t cap = capacity;
if (vectorhandle==NULL) { return CVECTOR_BAD_ARGUMENT; }
*vectorhandle = (CVectorHandle)CVECTOR_MALLOC(sizeof(CVector));
if (*vectorhandle==NULL) {
return CVECTOR_MALLOC_FAILED;
}
(*vectorhandle)->size = 0;
(*vectorhandle)->flags = 0;
(*vectorhandle)->capacity = 0;
(*vectorhandle)->elementsize = elementsize;
if (!cap) { cap = 10; }
(*vectorhandle)->array = (void CVECTOR_FAR *)CVECTOR_MALLOC(cap*elementsize);
if ((*vectorhandle)->array) {
(*vectorhandle)->capacity = cap;
return 0;
}
CVECTOR_FREE(*vectorhandle);
*vectorhandle = NULL;
return CVECTOR_MALLOC_FAILED;
}
/* CVectorAddElement -- add an element to a generic vector
equivalent to vector::push_back */
int CVectorAddElement(const CVectorHandle vectorhandle, const void CVECTOR_FAR * element) {
size_t newcap;
int errorcode;
if (vectorhandle==NULL) { return CVECTOR_BAD_ARGUMENT; }
if ( (vectorhandle->flags&CVECTOR_FLAGS_NO_RESIZE) ) { return CVECTOR_NO_RESIZE; }
if (vectorhandle->size >= vectorhandle->capacity) {
newcap = vectorhandle->capacity*2;
if (newcap < 1) { newcap = 1; }
errorcode = CVectorSetCapacity (vectorhandle, newcap);
if (errorcode != 0) {
newcap = vectorhandle->capacity+(size_t)(vectorhandle->capacity>>2);
if (newcap < 1) { newcap = 1; }
errorcode = CVectorSetCapacity (vectorhandle, newcap);
if (errorcode != 0) { return errorcode; }
}
}
CVECTOR_MEMMOVE(((char CVECTOR_FAR *)(vectorhandle->array))+vectorhandle->size*vectorhandle->elementsize,
(const char CVECTOR_FAR *)element, vectorhandle->elementsize);
vectorhandle->size ++;
return 0;
}
/* CVectorGetElement -- get a copy of an element from a generic vector */
int CVectorGetElement(const CVectorHandle vectorhandle, void CVECTOR_FAR * element, const size_t index) {
if (vectorhandle==NULL) { return CVECTOR_BAD_ARGUMENT; }
if (index < vectorhandle->size) {
CVECTOR_MEMMOVE((char *)element,((char *)(vectorhandle->array))+index*vectorhandle->elementsize,
vectorhandle->elementsize);
return 0;
} else {
return CVECTOR_NOT_FOUND;
}
}
/* CVectorGetElementptr -- get a pointer to an element from a generic vector */
int CVectorGetElementptr(const CVectorHandle vectorhandle, void CVECTOR_FAR ** elementptr, const size_t index) {
if (vectorhandle==NULL) { return CVECTOR_BAD_ARGUMENT; }
if (index < vectorhandle->size) {
*elementptr = (void CVECTOR_FAR*)(((char *)(vectorhandle->array))+index*vectorhandle->elementsize);
vectorhandle->flags |= CVECTOR_FLAGS_NO_RELOCATION;
return 0;
} else {
return CVECTOR_NOT_FOUND;
}
}
/* CVectorSetElement -- set a copy of an element into a generic vector */
int CVectorSetElement(const CVectorHandle vectorhandle, const void CVECTOR_FAR * element, const size_t index) {
size_t newcap;
int errorcode;
if (vectorhandle==NULL) { return CVECTOR_BAD_ARGUMENT; }
if (index >= vectorhandle->capacity) {
newcap = index+vectorhandle->capacity+1;
errorcode = CVectorSetCapacity(vectorhandle, newcap);
if (errorcode != 0 ) {
newcap = index*1.2;
if (newcap < index+128) { newcap = index+128; }
errorcode = CVectorSetCapacity(vectorhandle, newcap);
if (errorcode != 0) { return errorcode; }
}
}
if (index < vectorhandle->capacity) {
CVECTOR_MEMMOVE(((char *)(vectorhandle->array))+index*vectorhandle->elementsize,(char *)element,
vectorhandle->elementsize);
if (index >= vectorhandle->size) { vectorhandle->size = index+1; }
return 0;
} else {
return CVECTOR_NOT_FOUND;
}
}
/* CVectorRemoveElement -- remove an element from a generic vector */
/* keeps elements 0 -- index-1, discards element index
moves elements index+1 through vectorhandle->size-1
into element index through vectorhandle->size-2
i.e. moves characters (index+1)*(vectorhandle->elementsize)
through (vectorhandle->size)*(vectorhandle->elementsize)-1
to index*(vectorhandle->elementsize)
*/
int CVectorRemoveElement(const CVectorHandle vectorhandle, const size_t index) {
if (vectorhandle==NULL) { return CVECTOR_BAD_ARGUMENT; }
if (vectorhandle->flags&CVECTOR_FLAGS_NO_RELOCATION) { return CVECTOR_NO_RELOCATION; }
if (index >= vectorhandle->size) { return CVECTOR_NOT_FOUND; }
if (index == vectorhandle->size-1) {
vectorhandle->size--;
return 0;
}
CVECTOR_MEMMOVE((char *)vectorhandle->array+index*(vectorhandle->elementsize),
(char *)vectorhandle->array+(index+1)*(vectorhandle->elementsize),
(vectorhandle->size-1-index)*(vectorhandle->elementsize));
vectorhandle->size--;
return 0;
}
/* CVectorClear -- clear a generic vector */
int CVectorClear(const CVectorHandle vectorhandle) {
if (vectorhandle==NULL) { return CVECTOR_BAD_ARGUMENT; }
if (vectorhandle->flags & CVECTOR_FLAGS_NO_RESIZE) { return CVECTOR_NO_RESIZE; }
vectorhandle->size = 0;
return 0;
}
/* CVectorFree -- remove a generic vector */
int CVectorFree(CVectorHandle CVECTOR_FAR * vectorhandle) {
if (vectorhandle==NULL) { return CVECTOR_BAD_ARGUMENT; }
if (*vectorhandle) {
if ((*vectorhandle)->flags & CVECTOR_FLAGS_NO_RESIZE) { return CVECTOR_NO_RESIZE; }
if ((*vectorhandle)->flags & CVECTOR_FLAGS_NO_RELOCATION) { return CVECTOR_NO_RELOCATION; }
if ((*vectorhandle)->array) {
CVECTOR_FREE((*vectorhandle)->array);
}
CVECTOR_FREE(*vectorhandle);
}
*vectorhandle = 0;
return 0;
}
/* CVectorGetCapacity - function to return the CVector capacity */
int CVectorGetCapacity(const CVectorHandle vectorhandle, size_t CVECTOR_FAR * capacity) {
if ((vectorhandle==NULL)||!(capacity)) { return CVECTOR_BAD_ARGUMENT; }
*capacity = vectorhandle->capacity;
return 0;
}
/* CVectorGetSize - function to return the CVector size */
int CVectorGetSize(const CVectorHandle vectorhandle, size_t CVECTOR_FAR * size) {
if ((vectorhandle==NULL)||!(size)) { return CVECTOR_BAD_ARGUMENT; }
*size = vectorhandle->size;
return 0;
}
/* CVectorGetFlags - function to return the CVector flags */
int CVectorGetFlags(const CVectorHandle vectorhandle, unsigned int CVECTOR_FAR * flags) {
if ((vectorhandle==NULL)||!(flags)) { return CVECTOR_BAD_ARGUMENT; }
*flags = vectorhandle->flags;
return 0;
}
/* CVectorSetCapacity - function to set the CVector capacity */
int CVectorSetCapacity(const CVectorHandle vectorhandle, const size_t capacity) {
void CVECTOR_FAR * temparray;
if ((vectorhandle==NULL) || capacity < vectorhandle->size) { return CVECTOR_BAD_ARGUMENT; }
if (capacity == vectorhandle->capacity) { return 0; }
if (vectorhandle->flags&CVECTOR_FLAGS_NO_RELOCATION) { return CVECTOR_NO_RELOCATION; }
if (capacity) {
temparray = CVECTOR_MALLOC(capacity*vectorhandle->elementsize);
if (!temparray) { return CVECTOR_MALLOC_FAILED; }
if (vectorhandle->size) {
CVECTOR_MEMMOVE((char *)temparray, (char *)vectorhandle->array, vectorhandle->size*vectorhandle->elementsize);
}
CVECTOR_FREE(vectorhandle->array);
} else {
temparray = NULL;
}
vectorhandle->array = temparray;
vectorhandle->capacity = capacity;
return 0;
}
/* CVectorSetSize - function to set the CVector size */
int CVectorSetSize(const CVectorHandle vectorhandle, const size_t size) {
int errorcode;
if ((vectorhandle==NULL) ) { return CVECTOR_BAD_ARGUMENT; }
if (size == vectorhandle->size) { return 0; }
if ((vectorhandle->flags & CVECTOR_FLAGS_NO_RESIZE)) { return CVECTOR_NO_RESIZE; }
if ( size > vectorhandle->capacity ) {
if ((vectorhandle->flags & CVECTOR_FLAGS_NO_RELOCATION) ) { return CVECTOR_NO_RELOCATION; }
errorcode = CVectorSetCapacity(vectorhandle,size);
if (errorcode != 0) { return errorcode; }
}
if ( size <= vectorhandle->capacity ) {
if (size > vectorhandle->size) {
CVECTOR_MEMSET(((char *)vectorhandle->array)+(vectorhandle->size)*(vectorhandle->elementsize),
0, (vectorhandle->size-size)*(vectorhandle->elementsize));
}
vectorhandle->size = size;
}
return 0;
}
/* CVectorSetFags - function to set the CVector flags */
int CVectorSetFlags(const CVectorHandle vectorhandle, const unsigned int flags) {
if ((vectorhandle==NULL) ) { return CVECTOR_BAD_ARGUMENT; }
vectorhandle->flags = flags;
return 0;
}
#ifdef __cplusplus
}
#endif
cvector-CVector-1.0.4.1/CVector.h 0000664 0000000 0000000 00000013532 13272032507 0016406 0 ustar 00root root 0000000 0000000 /*
* CVector.h
* CVector
*
* Created by Herbert J. Bernstein on 11/28/08.
* Copyright 2008 Herbert J. Bernstein. All rights reserved.
*
*/
/**********************************************************************
* *
* YOU MAY REDISTRIBUTE THE CVector API UNDER THE TERMS OF THE LGPL *
* *
**********************************************************************/
/************************* LGPL NOTICES *******************************
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free *
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
* MA 02110-1301 USA *
* *
**********************************************************************/
#ifndef CVECTOR_H_INCLUDED
#define CVECTOR_H_INCLUDED
#ifdef __cplusplus
extern "C" {
#endif
/* CVector flags */
#define CVECTOR_FLAGS_NO_RELOCATION 1
#define CVECTOR_FLAGS_NO_RESIZE 2
/* CVector error return values */
#define CVECTOR_MALLOC_FAILED -1
#define CVECTOR_BAD_ARGUMENT -2
#define CVECTOR_NOT_FOUND -4
#define CVECTOR_NO_RELOCATION -8
#define CVECTOR_NO_RESIZE -16
#ifdef CVECTOR_USE_FAR
#include
#include
#define CVECTOR_FAR __far
#define CVECTOR_MALLOC _fmalloc
#define CVECTOR_FREE _ffree
#define CVECTOR_MEMSET _fmemset
#define CVECTOR_MEMMOVE _fmemmove
#else
#include
#include
#define CVECTOR_FAR
#define CVECTOR_MALLOC malloc
#define CVECTOR_FREE free
#define CVECTOR_MEMSET memset
#define CVECTOR_MEMMOVE memmove
#endif
typedef struct {
size_t size; /* size of the vector */
size_t capacity; /* capacity of the vector */
size_t elementsize; /* size of an element */
void CVECTOR_FAR * array; /* the array of elements */
unsigned int flags; /* flags */
} CVector;
typedef CVector CVECTOR_FAR * CVectorHandle;
/* CVectorAddElement -- add an element to a CVector */
int CVectorAddElement(const CVectorHandle vectorhandle, const void CVECTOR_FAR * element);
/* CVectorCapacity -- macro to return the CVector capacity */
#define CVectorCapacity(vectorhandle) (vectorhandle)->capacity
/* CVectorClear -- clear a generic vector */
int CVectorClear(const CVectorHandle vectorhandle);
/* CVectorCreate -- create a CVector */
int CVectorCreate(CVectorHandle CVECTOR_FAR * vectorhandle, const size_t elementsize, const size_t capacity);
/* CVectorElementAt -- return the element at the given index as a void pointer without checking
and without protection against relocation */
#define CVectorElementAt(vectorhandle,index) ((void CVECTOR_FAR *)(((char *)((vectorhandle)->array))+(index)*((vectorhandle)->elementsize)))
/* CVectorFree -- remove a CVector */
int CVectorFree(CVectorHandle CVECTOR_FAR * vectorhandle);
/* CVectorGetCapacity - function to return the CVector capacity */
int CVectorGetCapacity(const CVectorHandle vectorhandle, size_t CVECTOR_FAR * capacity);
/* CVectorGetElement -- get a copy of an element from a CVector */
int CVectorGetElement(const CVectorHandle vectorhandle, void CVECTOR_FAR * element, const size_t index);
/* CVectorGetElementptr -- get a pointer to an element from a CVector */
int CVectorGetElementptr(const CVectorHandle vectorhandle, void CVECTOR_FAR ** elementptr, const size_t index);
/* CVectorGetFlags - function to return the CVector flags */
int CVectorGetFlags(const CVectorHandle vectorhandle, unsigned int CVECTOR_FAR * flags);
/* CVectorGetSize - function to return the CVector size */
int CVectorGetSize(const CVectorHandle vectorhandle, size_t CVECTOR_FAR * size);
/* CVectorRemoveElement -- remove an element from a generic vector */
int CVectorRemoveElement(const CVectorHandle vectorhandle, const size_t index);
/* CVectorSetCapacity - function to set the CVector capacity */
int CVectorSetCapacity(const CVectorHandle vectorhandle, const size_t capacity);
/* CVectorSetElement -- set a copy of an element into a CVector */
int CVectorSetElement(const CVectorHandle vectorhandle, const void CVECTOR_FAR * element, const size_t index);
/* CVectorSetFags - function to set the CVector flags */
int CVectorSetFlags(const CVectorHandle vectorhandle, const unsigned int flags);
/* CVectorSetSize - function to set the CVector size */
int CVectorSetSize(const CVectorHandle vectorhandle, const size_t size);
/* CVectorSize -- macro to return the CVector size */
#define CVectorSize(vectorhandle) (vectorhandle)->size
#ifdef __cplusplus
}
#endif
#endif
cvector-CVector-1.0.4.1/CVectorBasicTest.c 0000664 0000000 0000000 00000016223 13272032507 0020203 0 ustar 00root root 0000000 0000000 /*
* CVectorBasicTest.c
* CVector
*
* Created by Herbert J. Bernstein on 12/04/08.
* Copyright 2008 Herbert J. Bernstein. All rights reserved.
*
*/
/**********************************************************************
* *
* YOU MAY REDISTRIBUTE THE CVector API UNDER THE TERMS OF THE LGPL *
* *
**********************************************************************/
/************************* LGPL NOTICES *******************************
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free *
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
* MA 02110-1301 USA *
* *
**********************************************************************/
#ifdef USE_LOCAL_HEADERS
#include "CVector.h"
#else
#include
#endif
#include
typedef long unsigned int LUI;
CVectorHandle vectorhandle;
int main(int argc, char **argv) {
size_t index;
double element;
double * elementptr;
void * velementptr;
unsigned int flags;
size_t size, capacity;
FILE * out;
out = stdout;
if (argc > 1) {
out = fopen(argv[1],"w+");
if (!out) {
fprintf(stderr," Failed to open output file %s\n", argv[1]);
exit(1);
}
}
fprintf(out," Test 0: Create vector and load it with 1000000 elements.\n");
CVectorCreate(&vectorhandle,sizeof(double),1);
for (index=0; index<1000000; index++) {
element = (double)index;
CVectorAddElement(vectorhandle, &element);
if (!(index&0x1FFFF)) {
if (CVectorGetCapacity(vectorhandle,&capacity) ){
fprintf(stderr,"Call to CVectorGetCapacity failed.\n");
}
if (CVectorGetSize(vectorhandle,&size)) {
fprintf(stderr,"Call to CVectorGetSize failed.\n");
}
if (size != index+1) {
fprintf(stderr,"Size mismatch index+1 %lu, size %lu.\n",(LUI)index+1,(LUI)size);
}
fprintf(out,"Size %lu, capacity %lu.\n", (LUI)size, (LUI)capacity);
}
}
fprintf(out," Test 1: Reference elements of CVector as an array.\n");
for (index=0; index<1000000; index++) {
if ((double)index != ((double *)(vectorhandle->array))[index]) {
fprintf(stderr," data mismatch array[%lu] = %g\n",
(LUI)index, ((double *)(vectorhandle->array))[index]);
}
}
fprintf(out," Test 2: Reference elements of CVector via CVectorGetElement.\n");
for (index=0; index<(size_t)1000000; index++) {
if (CVectorGetElement(vectorhandle, &element, index))
fprintf(stderr," Failed CVectorGetElement, index = %lu\n",(LUI)index);
if ((double)index != element) {
fprintf(stderr," data mismatch array[%lu] = %g\n",
(LUI)index, element);
}
}
/* The next block will referenece the array via pointers
This will also set the CVECTOR_FLAGS_NO_RELOCATION
Will then reset the flag.
*/
fprintf(out," Test 3: Reference elements of CVector via CVectorGetElementptr.\n");
for (index=0; index<(size_t)1000000; index++) {
if (CVectorGetElementptr(vectorhandle, &velementptr, index))
fprintf(stderr," Failed CVectorGetElement, index = %lu\n",(LUI)index);
elementptr = (double *)velementptr;
if (velementptr != CVectorElementAt(vectorhandle, index)) {
fprintf(stderr," Failed CVectorGetElementAt, index = %lu\n",(LUI)index);
}
if ((double)index != *elementptr) {
fprintf(stderr," data mismatch array[%lu] = %g\n",
(LUI)index, *elementptr);
}
}
if (CVectorGetFlags(vectorhandle,&flags))
fprintf(stderr,"Call to CVectorGetFlags failed.\n");
if ( !(flags&CVECTOR_FLAGS_NO_RELOCATION) )
fprintf(stderr,"CVECTOR_FLAGS_NO_RELOCATION not set by CVectorGetElementptr.\n");
flags = 0;
if (CVectorSetFlags(vectorhandle,flags))
fprintf(stderr,"Call to CVectorSetFlags failed.\n");
fprintf(out," Test 4: Set elements of CVector as an array\n");
for (index=0; index<(size_t)1000000; index++) {
((double *)(vectorhandle->array))[index] = (double)(1000000-index);
}
for (index=0; index<(size_t)1000000; index++) {
if ((double)(1000000-index) != ((double *)(vectorhandle->array))[index]) {
fprintf(stderr," data mismatch array[%lu] = %g\n",
(LUI)index, ((double *)(vectorhandle->array))[index]);
}
}
fprintf(out," Test 5: Set elements of CVector via CVectorSetElement.\n");
for (index=0; index<(size_t)1000000; index++) {
element = (double)(index-1000000);
if (CVectorSetElement(vectorhandle,&element,1000000-index))
fprintf(stderr," Failed CVectorSetElement, index = %lu\n",(LUI)(1000000-index));
}
for (index=0; index<(size_t)1000000; index++) {
if ((double)(index-1000000) != ((double *)(vectorhandle->array))[1000000-index]) {
fprintf(stderr," data mismatch array[%lu] = %g\n",
(LUI)index, ((double *)(vectorhandle->array))[1000000-index]);
}
}
fprintf(out," Test 6: Remove elements of CVector via CVectorRemoveElement.\n");
for (index=0; index<(size_t)2000; index++) {
((double *)(vectorhandle->array))[index] = (double)(index);
}
for (index=0; index<(size_t)1000; index++) {
if (CVectorRemoveElement(vectorhandle,index))
fprintf(stderr," Failed CVectorRemoveElement, index = %lu\n",(LUI)index);
}
for (index=0; index<(size_t)1000; index++) {
if ((double)(index*2+1) != ((double *)(vectorhandle->array))[index]) {
fprintf(stderr," data mismatch array[%lu] = %g\n",
(LUI)index, ((double *)(vectorhandle->array))[index]);
}
}
fprintf(out," Test 7: Remove all elements of CVector via CVectorClear.\n");
if (CVectorClear(vectorhandle))
fprintf(stderr," Failed CVectorClear\n");
if (CVectorSize(vectorhandle) !=0) {
fprintf(stderr," CVectorClear failed to set size to zero\n");
}
return 0;
}
cvector-CVector-1.0.4.1/CVectorBasicTest_orig.lst 0000664 0000000 0000000 00000001264 13272032507 0021602 0 ustar 00root root 0000000 0000000 Test 0: Create vector and load it with 1000000 elements.
Size 1, capacity 1.
Size 131073, capacity 262144.
Size 262145, capacity 524288.
Size 393217, capacity 524288.
Size 524289, capacity 1048576.
Size 655361, capacity 1048576.
Size 786433, capacity 1048576.
Size 917505, capacity 1048576.
Test 1: Reference elements of CVector as an array.
Test 2: Reference elements of CVector via CVectorGetElement.
Test 3: Reference elements of CVector via CVectorGetElementptr.
Test 4: Set elements of CVector as an array
Test 5: Set elements of CVector via CVectorSetElement.
Test 6: Remove elements of CVector via CVectorRemoveElement.
Test 7: Remove all elements of CVector via CVectorClear.
cvector-CVector-1.0.4.1/Makefile 0000664 0000000 0000000 00000017432 13272032507 0016333 0 ustar 00root root 0000000 0000000 #
# Makefile
# CVector
#
# Created by Herbert J. Bernstein on 12/03/08.
# Copyright 2008 Herbert J. Bernstein. All rights reserved.
# rev 13 Jan 09 -- HJB
#
#
#**********************************************************************
#* *
#* YOU MAY REDISTRIBUTE THE CVector API UNDER THE TERMS OF THE LGPL *
#* *
#**********************************************************************/
#************************* LGPL NOTICES *******************************
#* *
#* This library is free software; you can redistribute it and/or *
#* modify it under the terms of the GNU Lesser General Public *
#* License as published by the Free Software Foundation; either *
#* version 2.1 of the License, or (at your option) any later version. *
#* *
#* This library is distributed in the hope that it will be useful, *
#* but WITHOUT ANY WARRANTY; without even the implied warranty of *
#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
#* Lesser General Public License for more details. *
#* *
#* You should have received a copy of the GNU Lesser General Public *
#* License along with this library; if not, write to the Free *
#* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
#* MA 02110-1301 USA *
#* *
#**********************************************************************/
# Version string
VERSION = 2:0:0
RELEASE = 1.0.4
#
# Compiler and compilation flags
#
CC = gcc
ifneq ($(MSYSTEM),MINGW32)
CFLAGS = -g -O2 -Wall -ansi -pedantic
else
CFLAGS = -g -O2 -Wall
endif
#
# libtool path if system default is not suitable
#
#LIBTOOL = $(HOME)/bin/libtool
ifndef LIBTOOL
LIBTOOL = libtool
endif
#
# If local headers must be quoted uncomment the next line
#
#USE_LOCAL_HEADERS = 1
#
# Directories
#
ROOT = .
LIB = $(ROOT)/lib
BIN = $(ROOT)/bin
SRC = $(ROOT)
INC = $(ROOT)
EXAMPLES = $(ROOT)
TESTDATA = $(ROOT)
ifndef INSTALL_PREFIX
INSTALL_PREFIX = /usr/local
#INSTALL_PREFIX = $(HOME)
endif
#
# Include directories
#
ifdef USE_LOCAL_HEADERS
INCLUDES = -DUSE_LOCAL_HEADERS
else
INCLUDES = -I$(INC)
endif
COMPILE_COMMAND = $(LIBTOOL) --mode=compile $(CC) $(CFLAGS) $(INCLUDES) $(WARNINGS) -c
LIBRARY_LINK_COMMAND = $(LIBTOOL) --mode=link $(CC) -version-info $(VERSION) -release $(RELEASE) -no-undefined -rpath $(INSTALL_PREFIX)/lib
BUILD_COMMAND_LOCAL = $(LIBTOOL) --mode=link $(CC) $(CFLAGS) $(INCLUDES)
BUILD_COMMAND_DYNAMIC = $(LIBTOOL) --mode=link $(CC) $(CFLAGS) -shared -I $(INSTALL_PREFIX)/include
BUILD_COMMAND_STATIC = $(LIBTOOL) --mode=link $(CC) $(CFLAGS) -static -I $(INSTALL_PREFIX)/include
INSTALL_COMMAND = $(LIBTOOL) --mode=install cp
INSTALL_FINISH_COMMAND = $(LIBTOOL) --mode=finish
OBJ_EXT = lo
LIB_EXT = la
######################################################################
# You should not need to make modifications below this line #
######################################################################
#
# Suffixes of files to be used or built
#
.SUFFIXES: .c .$(OBJ_EXT) .$(LIB_EXT)
#
# Common dependencies
#
COMMONDEP = Makefile
#
# Source files
#
SOURCE = $(SRC)/CVector.c
#
# Header files
#
HEADERS = $(INC)/CVector.h
# Default: instructions
#
default:
@echo ' '
@echo '***************************************************************'
@echo ' '
@echo ' PLEASE READ README_CVector.txt and lgpl.txt'
@echo ' '
@echo ' Before making the CVector library and example programs, check'
@echo ' that the chosen settings are correct'
@echo ' '
@echo ' The current compile command is:'
@echo ' '
@echo ' $(COMPILE_COMMAND)'
@echo ' '
@echo ' The current library link command is:'
@echo ' '
@echo ' $(LIBRARY_LINK_COMMAND)'
@echo ' '
@echo ' The current library local, dynamic and static build commands are:'
@echo ' '
@echo ' $(BUILD_COMMAND_LOCAL)'
@echo ' $(BUILD_COMMAND_DYNAMIC)'
@echo ' $(BUILD_COMMAND_STATIC)'
@echo ' '
@echo ' Before installing the CVector library and example programs, check'
@echo ' that the install directory and install commands are correct:'
@echo ' '
@echo ' The current values are :'
@echo ' '
@echo ' $(INSTALL_PREFIX) '
@echo ' $(INSTALL_COMMAND) '
@echo ' $(INSTALL_FINISH) '
@echo ' '
@echo ' To compile the CVector library and example programs type:'
@echo ' '
@echo ' make clean'
@echo ' make all'
@echo ' '
@echo ' To run a set of tests type:'
@echo ' '
@echo ' make tests'
@echo ' '
@echo ' To clean up the directories type:'
@echo ' '
@echo ' make clean'
@echo ' '
@echo ' To install the library and binaries type:'
@echo ' '
@echo ' make install'
@echo ' '
@echo '***************************************************************'
@echo ' '
#
# Compile the library and examples
#
all: $(LIB) $(BIN) $(SOURCE) $(HEADERS) \
$(LIB)/libCVector.$(LIB_EXT) \
$(BIN)/CVectorBasicTest
install: all $(INSTALL_PREFIX) $(INSTALL_PREFIX)/lib $(INSTALL_PREFIX)/include \
$(INC) $(LIB)/libCVector.$(LIB_EXT) $(INC)/CVector.h
$(INSTALL_COMMAND) $(LIB)/libCVector.$(LIB_EXT) $(INSTALL_PREFIX)/lib/libCVector.$(LIB_EXT)
$(INSTALL_FINISH_COMMAND) $(INSTALL_PREFIX)/lib/libCVector.$(LIB_EXT)
-cp $(INSTALL_PREFIX)/include/CVector.h $(INSTALL_PREFIX)/include/CVector_old.h
cp $(INC)/CVector.h $(INSTALL_PREFIX)/include/CVector.h
chmod 644 $(INSTALL_PREFIX)/include/CVector.h
echo "Testing final install dynamic"
$(BUILD_COMMAND_DYNAMIC) $(EXAMPLES)/CVectorBasicTest.c \
-L$(INSTALL_PREFIX)/lib -lCVector -o $(BIN)/CVectorBasicTest_dynamic
$(BIN)/CVectorBasicTest_dynamic > $(TESTDATA)/CVectorBasicTest_dynamic.lst
diff -b -c $(TESTDATA)/CVectorBasicTest_orig.lst \
$(TESTDATA)/CVectorBasicTest_dynamic.lst
echo "Testing final install static"
$(BUILD_COMMAND_STATIC) $(EXAMPLES)/CVectorBasicTest.c \
-L$(INSTALL_PREFIX)/lib -lCVector -o $(BIN)/CVectorBasicTest_static
$(BIN)/CVectorBasicTest_static > $(TESTDATA)/CVectorBasicTest_static.lst
diff -b -c $(TESTDATA)/CVectorBasicTest_orig.lst \
$(TESTDATA)/CVectorBasicTest_static.lst
#
# Directories
#
$(INSTALL_PREFIX):
mkdir -p $(INSTALL_PREFIX)
$(INSTALL_PREFIX)/lib: $(INSTALL_PREFIX)
mkdir -p $(INSTALL_PREFIX)/lib
$(INSTALL_PREFIX)/bin: $(INSTALL_PREFIX)
mkdir -p $(INSTALL_PREFIX)/bin
$(INSTALL_PREFIX)/include: $(INSTALL_PREFIX)
mkdir -p $(INSTALL_PREFIX)/include
$(LIB):
mkdir $(LIB)
$(BIN):
mkdir $(BIN)
#
# CVector library
#
$(LIB)/libCVector.$(LIB_EXT): $(SOURCE) $(HEADERS) $(COMMONDEP)
$(COMPILE_COMMAND) -c $(SOURCE)
$(LIBRARY_LINK_COMMAND) -o $(LIB)/libCVector.$(LIB_EXT) *.$(OBJ_EXT)
#
# CVectorBasicTest example program
#
$(BIN)/CVectorBasicTest: $(LIB)/libCVector.$(LIB_EXT) $(EXAMPLES)/CVectorBasicTest.c
$(BUILD_COMMAND_LOCAL) $(EXAMPLES)/CVectorBasicTest.c $(LIB)/libCVector.$(LIB_EXT) \
-o $@
#
# Tests
#
tests: $(LIB) $(BIN) $(BIN)/CVectorBasicTest \
$(TESTDATA)/CVectorBasicTest_orig.lst
$(BIN)/CVectorBasicTest > $(TESTDATA)/CVectorBasicTest.lst
diff -b -c $(TESTDATA)/CVectorBasicTest_orig.lst \
$(TESTDATA)/CVectorBasicTest.lst
#
# Remove all non-source files
#
empty:
@-rm -rf $(LIB)
@-rm -rf $(BIN)
@-rm -f $(TESTDATA)/CVectorBasicTest.lst
#
# Remove temporary files
#
clean:
@-rm -f core
@-rm -f *.o
@-rm -f *.$(OBJ_EXT)
@-rm -f *.c.*
@-rm -rf .libs
#
# Restore to distribution state
#
distclean: clean empty
cvector-CVector-1.0.4.1/README 0000777 0000000 0000000 00000000000 13272032507 0020662 2README_CVector.txt ustar 00root root 0000000 0000000 cvector-CVector-1.0.4.1/README_CVector.html 0000664 0000000 0000000 00000031224 13272032507 0020136 0 ustar 00root root 0000000 0000000
README CVector -- API for Dynamic Arrays
CVector is an ANSI C implementation of dynamic arrays to provide a crude approximation to the C++ vector class.
Release 1.0.4.1 is a move from Sourceforge to Github.
Release 1.0.4 adds a CMakeLists.txt file to the kit and changes the test program to allow normal output to be written to a specified file.
Release 1.0.3 was is a minor revision of release 1.0.0 of 8 January 2009 to update the Makefile for builds under MINGW,
on 26 February 2009 to add the macro CVectorElementAt, and on 7 July 2009 to change the macro to CVECTOR_FAR and
similarly change the macros that handle memmove and memset. There
are no changes to the code. The internal copy of libtool has been removed.
PLEASE READ README_CVector.txt and lgpl.txt
Before making the CVector library and example programs, check
that the chosen settings are correct
The current compile command is:
libtool --mode=compile gcc -g -O2 -Wall -ansi -pedantic -I. -c
The current library link command is:
libtool --mode=link gcc -version-info 2:0:0 -release 1.0.4 -rpath /usr/local/lib
The current library local, dynamic and static build commands are:
libtool --mode=link gcc -g -O2 -Wall -ansi -pedantic -I.
libtool --mode=link gcc -g -O2 -Wall -ansi -pedantic -shared -I /usr/local/include
libtool --mode=link gcc -g -O2 -Wall -ansi -pedantic -static -I /usr/local/include
Before installing the CVector library and example programs, check
that the install directory and install commands are correct:
The current values are :
/usr/local
libtool --mode=install cp
To compile the CVector library and example programs type:
make clean
make all
To run a set of tests type:
make tests
To clean up the directories type:
make clean
To install the library and binaries type:
make install
If these settings need to be changed, edit Makefile. On some systems, e.g. Mac OS X, the default
libtool is not appropriate. In that case you should install a recent version of libtool. The
CVector kit has been tested with libtool versions 1.3.5, 1.5.4 and 2.2.6. If the system libtool is
not to be used, define the variable LIBTOOL to be the path to the libtool executable, e.g.
in bash. On MINGW, use of gcc 4 and libtool 2.2.6 are highly recommended.
export LIBTOOL=$HOME/bin/libtool
of in the Makefie
LIBTOOL = $(HOME)/bin/libtool
If you need to include local header files using #include "..." instead of #include <...>,
define the variable USE_LOCAL_HEADERS
Synopsis
#include <CVector.h>
int CVectorCreate(CVectorHandle*vectorhandle, constsize_telementsize, constsize_tcapacity);
int CVectorAddElement(constCVectorHandlevectorhandle, constvoid*element);
int CVectorGetSize(constCVectorHandlevectorhandle, size_t*size);
int CVectorGetFlags(constCVectorHandlevectorhandle, int*flags);
int CVectorSetCapacity(constCVectorHandlevectorhandle, constsize_tcapacity);
int CVectorSetSize(constCVectorHandlevectorhandle, constsize_tsize);
int CVectorSetFlags(constCVectorHandlevectorhandle, constunsignedintflags);
Description
The CVector.h header file defines the CVectorHandle type as a dynamic array for elements in which each element uses elementsize
characters in a block of memory elementsize*capacity characters long. There are size elements used in the array
each with an index between 0 and size-1. CVectorCreate() is used to create the dynamic array. Elements may either be
added at the end of the dynamic array using CVectorAddElement(), or in arbitrary order with CVectorSetElement(). Elements may
be examined with copy semantics using CVectorGetElement() or as pointers directly into the dynamic array using CVectorGetElementptr().
The function CVectorElementAt() performs the same function as CVectorGetElementptr() for use in complex expressions, but
does no error checking.
Caution must be used in using pointers because expansion of the dynamic array can force relocation of all data in the dynamic array. Therefore,
any use of CVectorGetElementptr() sets a flag, CVECTOR_FLAGS_NO_RELOCATION, that disables any further array relocation, which will prevent
the extension of the size array beyond the capacity in effect at the time of the first CVectorGetElementptr() call. Calls
to CVectorElementAt() do not set CVECTOR_FLAGS_NO_RELOCATION and returning resulting values to calling routines is dangerous. The functions
CVectorGetFlags() and CVectorSetFlags() can be used to reset this flag (see Examples below). The function CVectorRemoveElement()
will remove the element of the indicated index, shifting all elements of higher indices down, unless the flag CVECTOR_FLAGS_NO_RELOCATION is set.
The capacity, size and flags may be examined or set with CVectorCapacity(), CVectorGetCapacity(), CVectorSize(),
CVectorGetSize(), CVectorGetFlags, CVectorSetCapacity(), CVectorSetSize() and CVectorSetFlags().
External declarations as well as the CVector structure definition, and the CVectorHandle definition are included in the <CVector.h>
include file. The CVector structure includes at least the following fields:
size_t size;
/* size of the vector */
size_t capacity;
/* capacity of the vector */
size_t elementsize;
/* size of an element */
void * array;
/* the array of elements */
int flags;
/* flags */
The valid flags are CVECTOR_FLAGS_NO_RELOCATION and CVECTOR_FLAGS_NO_RESIZE which may be ORed.
Returns
The functions CVectorCapacity() and CVectorSize() are implemented as macros and return the CVector capacity and
size directly. While they could be used as lvalues, such use is not recommended and is not guaranteed to work in future versions
of CVector. The function CVectorElementAt() is implemented as a macro and returns a void * pointer into the data array
with no bounds checking. All other functions return 0 for normal completion, or the sum of one or more of the following non-zero error codes:
Error Return
Numeric Value
Meaning
CVECTOR_MALLOC_FAILED
-1
/* A call to allocate memory failed */
CVECTOR_BAD_ARGUMENT
-2
/* An argument is not valid */
CVECTOR_NOT_FOUND
-4
/* An element could not be found */
CVECTOR_NO_RELOCATION
-8
/* Relocation is not permitted */
CVECTOR_NO_RESIZE
-16
/* A change is size is not permitted */
Examples
To create a vector of doubles, starting with minimal capacity, reporting failure to stderr: