srdja / Collections-C

A library of generic data structures for the C language.
http://srdja.github.io/Collections-C
GNU Lesser General Public License v3.0
2.8k stars 328 forks source link

is a Vector data structure available? #134

Closed Pascal-Ortiz closed 4 years ago

Pascal-Ortiz commented 4 years ago

Does the library provide a vector data structure? By a vector, I mean a similar data structure to C++/STL vector, ie a right-extensible array containing elements placed in contiguous storage and you can access by integer indexing.

The library provide a deque data structure ; however elements (not their address) are not placed in a contiguous sequence of bytes.

EDIT The issue is solved since vectors are implemented in array.c.

EDIT 2

After checking more closely, it appears that an array objetct is not a true container: the array stores only a reference to your data element:

enum cc_stat array_add(Array *ar, void *element)
{
    if (ar->size >= ar->capacity) {
        enum cc_stat status = expand_capacity(ar);
        if (status != CC_OK)
            return status;
    }

    ar->buffer[ar->size] = element;
    ar->size++;

    return CC_OK;
}

And the hash table has the same problem. I see little use cases where this kind of implementation could be of interest.

srdja commented 4 years ago

And the hash table has the same problem. I see little use cases where this kind of implementation could be of interest.

I'm not sure where you're coming from here. If you look at any other container implementation for the C language, you'll see that they all use the same method. I've seen plenty of programs in which storing pointers inside containers was a useful thing to do. Now, of course, sometimes you actually do want to store things directly, but it's hard to write a generic container in C that doesn't use void*. It's not great, but it technically gives you the ability to store anything indirectly.

To actually do what you're proposing, some kind of template system would need to be devised. C++ does this automagically, but C has nothing like it. I guess maybe something could be done with macros, but I don't see any non ugly and awkward solution there.

Maybe you had something on mind?

Pascal-Ortiz commented 4 years ago

In fact, you can find some container C libraries implementing what I'm looking for, that is a true container, not a shallow one ie containing only reference to data:

I was looking for a lightweight library (glib and qlibc are not) and implementing true vector and hastable containers by means of generic pointers.

Your implementation has a narrow use case because it doesn't manage the data it points to. A function cannot easily return an Array :

srdja commented 4 years ago

Ah okay, I got it. You want a container that copies the data into it and I agree it would be a nice addition. I might add it eventually. However, you are always welcome to send a pull request if you feel like contributing!

bkthomps commented 4 years ago

Hi @Pascal-Ortiz I see you linked to my Containers library.

In my implementation, you can either copy the data or copy the pointer, up to you. If you pass in a pointer to your data, it copies the data, but if you pass in a pointer to a pointer, it copies the pointer. I'm optimizing the library to be more efficient, and am about half done with that, and afterwards I'll add some more documentation on how to use the functions, etc.

Pascal-Ortiz commented 4 years ago

Hi Bailey!

Hi @Pascal-Ortiz I see you linked to my Containers library.

Indeed, your vector holds the data and is 100% OK for me.

I was talking about the hashtable implementation and to me more precise i was referring to this line of code from the unordered_map_add_item function:

        me->buckets[index] = add;

where add is a pointer.

In my implementation, you can either copy the data

Ah, ok, perhaps are you referring to the unordered_map_put function?

bkthomps commented 4 years ago

@Pascal-Ortiz that function is used internally (it is possible to put the pointer into the bucket because prior to that in the put function, a copy is made). The put function is the one which users of the library would use.

Also I don't want it to look like I'm hijacking this repository's issue, so maybe we should move this discussion to an issue in bkthomps/Containers or in email?

Pascal-Ortiz commented 4 years ago

Also I don't want it to look like I'm hijacking this repository's issue, so maybe we should move this discussion to an issue in bkthomps/Containers or in email?

Certainly better.