Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.std.c > #1396

Re: The C Containers Library

From jacob navia <jacob@spamsink.net>
Newsgroups comp.std.c
Subject Re: The C Containers Library
Date 2012-07-15 23:10 +0200
Organization lccwin
Message-ID <jtvbjd$lhm$2@speranza.aioe.org> (permalink)
References <4FF9E523.6000006@spamsink.net> <e2ec6f9a-e019-4388-8293-22109594aad9@googlegroups.com>

Show all headers | View raw


Le 15/07/12 22:13, agottem@gmail.com a écrit :
>
> Looking at your deque.c implementation, I'm wondering why you decided to depend on 'Allocators' for your containers.

   For example, why is the deque Add function invoking malloc at all?
>
> Why not make use of the "container_of()" concept, and define the Add function as:
>
> Add (struct deque* container, struct deque_node* node_to_add);
>
>
> Someone needing a deque could then make use of the API via:
>
> struct my_type
> {
>      int my_data;
>
>      struct deque_node my_node;
> };
>
> struct deque container;
>
> ...
>
> Add(&container, &my_type.my_node);
>
>
> This would eliminate the need for STL-ish "allocators", would eliminate pointer

chasing in the added elements, and would enable each element to be 
contained in

multiple containers.  Furthermore, this style would be much closer to C 
philosophy.
>


Because I decided that I would follow the same way as the C++ STL does:

The container owns the memory of the stored objects.

This explains why allocators are needed.

Now, it is possible for the list and dlist containers to work with the
list elements themselves (your "nodes") for a limited number of
operations.

That is not the case for deque. Why?

Because deque has a limited API compared to lists or vectors since what
you are interested in, when using a deque, is the way you store and
retrieve the data.


What is the problem of storing an element in multiple containers that is
feasible without problems in the proposed library: you store just a
pointer to the objectinstead of the object itself. The  you can
store the object in as many containers as you wish.


jacob

Back to comp.std.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

The C Containers Library jacob navia <jacob@spamsink.net> - 2012-07-08 21:53 +0200
  Re: The C Containers Library agottem@gmail.com - 2012-07-15 13:13 -0700
    Re: The C Containers Library jacob navia <jacob@spamsink.net> - 2012-07-15 23:10 +0200
      Re: The C Containers Library jacob navia <jacob@spamsink.net> - 2012-07-15 23:12 +0200
      Re: The C Containers Library agottem@gmail.com - 2012-07-15 16:15 -0700
        Re: The C Containers Library jacob navia <jacob@spamsink.net> - 2012-07-16 10:22 +0200

csiph-web