Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Type agnosticism, data structures and C

Name: Anonymous 2011-09-16 6:07

/prog/, I've been working on a C library with basic data structures and useful functions, and have implemented things like doubly-linked lists and vectors and so on. Now I'm working on Unicode-based string support. Here's my issue.

I've implemented type agnosticism on the vector type using void pointers (not preprocessor macros), so I want to use the vector type to represent a string. Each Unicode codepoint is an int32_t. Of course, I don't want to have to allocate four bytes on the heap for each codepoint, then point to that from each vector element; that's utterly stupid. On the other hand, doing typecasts to shove an integer in the void * type is kludgy and works very poorly, often requiring double explicit casts; once to expand/shrink the type's size, then once more to explicitly cast to a pointer or back.

How can I solve my issues and use my vector type for strings?

Name: Anonymous 2011-09-16 7:52

http://azabani.com/git/index.cgi/generalist/plain/src/vector.h

/**
 * \file vector.h
 * This file defines a data structure for vectors (one-dimensional arrays), as
 * well as functions to manipulate them.
 */

#ifndef GENERALIST_VECTOR_H
#define GENERALIST_VECTOR_H

#include <stdint.h>
#include <stdlib.h>
#include <string.h>

/**
 * The basic data structure for a vector.
 */

struct gvector_struct {

    /**
     * The pointer to the memory used for the vector. Integer values of the
     * same size and signedness as a void pointer can also be stored here,
     * but the void pointer data type varies between platforms, so this is not
     * good practice except under controlled, memory-tight conditions.
     */

    void **d;

    /**
     * The number of elements memory has been allocated for.
     */

    intmax_t a;

    /**
     * The number of elements currently stored.
     */

    intmax_t l;

};

/**
 * A vector should be passed around by a pointer to its structure.
 */

typedef struct gvector_struct *gvector;

/**
 * Allocate a new vector on the heap.
 * \return the new vector
 */

gvector gvector_alloc();

/**
 * Double the memory allocated to a vector.
 * \param v the vector
 * \return the vector
 */

void gvector_expand(gvector v);

/**
 * Add a new value to the end of a vector, with the data pointer provided.
 * \param v the vector
 * \param d the data pointer value
 */

void gvector_append(gvector v, void *d);

/**
 * Add a new value to the beginning of a vector, with the data pointer
 * provided.
 * \param v the vector
 * \param d the data pointer value
 */

void gvector_prepend(gvector v, void *d);

/**
 * Remove the first value of a vector, returning the removed value. If there
 * are no elements, return NULL.
 * \param v the vector
 * \return the data pointer, or NULL if there are no elements
 */

void *gvector_remove_first(gvector v);

/**
 * Remove the last value of a vector, returning the removed value. If there are
 * no elements, return NULL.
 * \param v the vector
 * \return the data pointer, or NULL if there are no elements
 */

void *gvector_remove_last(gvector v);

/**
 * Call a given function for each value, passing the value as the only argument
 * to the child function each time.
 * \param v the vector
 * \param f the function to be called for each value
 */

void gvector_each(gvector v, void (*f)(void *));

/**
 * Return the position index of the first element containing the given data
 * pointer as it is placed in the vector. If a value can not be found, return
 * -1.
 * \param v the vector
 * \param d the data pointer value
 * \return a non-negative position index if the value is found; -1 otherwise
 */

intmax_t gvector_index_value(gvector v, void *d);

/**
 * Return the position index of the first element in the vector that, when a
 * given comparator function is called with the value and the given data
 * pointer value as arguments, returns zero. If a value can not be found,
 * return -1.
 * \param v the vector
 * \param d the data pointer value
 * \param f the comparator function
 * \return a non-negative position index if the value is found; -1 otherwise
 */

intmax_t gvector_index_compare(gvector v, void *d, int (*f)(void *, void *));

/**
 * Reverses a vector in place.
 * \param v the vector
 */

void gvector_reverse(gvector v);

/**
 * Append one vector's contents to another vector.
 * \param v the vector to append to
 * \param w the vector to copy from
 */

void gvector_concat(gvector v, gvector w);

/**
 * Frees the memory used for a vector.
 * \param v the vector
 */

void gvector_destroy(gvector v);

#endif

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List