Giter Site home page Giter Site logo

Comments (7)

srdja avatar srdja commented on May 31, 2024

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?

from collections-c.

Pascal-Ortiz avatar Pascal-Ortiz commented on May 31, 2024

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:

  • a well known example is the glib library whose implementation use the void* generic pointer.
  • the same for the qlibc library but the hash table is only string-capable.
  • the same for Containers but unfortunately the hash table has the same problem your hastable has (only references to the data).
  • another example is the klib library with a macro-based implementation (klib is a great library and the khash hashtable provided competes with the fastest libraries as google dense_hash_map and others).
  • another macro based one is GenericDataStructure.

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 :

  • this is undefined behavior if the data the array points to is allocated on the stack
  • if the data is allocated on the heap, the function has to return also pointer(s) to the dynamic memory and all the burden of managing memory falls back to the user.

from collections-c.

srdja avatar srdja commented on May 31, 2024

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!

from collections-c.

bkthomps avatar bkthomps commented on May 31, 2024

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.

from collections-c.

Pascal-Ortiz avatar Pascal-Ortiz commented on May 31, 2024

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?

from collections-c.

bkthomps avatar bkthomps commented on May 31, 2024

@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?

from collections-c.

Pascal-Ortiz avatar Pascal-Ortiz commented on May 31, 2024

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.

from collections-c.

Related Issues (20)

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.