lets say its common situation when object have subobjects in container
and receives callbacks from contained items. and object want to move
objects in containers on signal(callback).
iterator is needed for container modifications, item cannot know its
iterator, at least its not easy to do so and i think cannot be done
type-safe avoiding virtual functions and stuff.
'this' pointer from items is a freeby :>.
the problem is, std containers want full linear scan to find iterator
from pointer, while from implemention side converting pointer to
std::list or std::map iterator is free (substracting offset to get
std::list::node from pointer and then construct iterator from node).
why this is done so? this makes std containers unusable in described
child to container callback scenarios - huge overhead trying to move
item in container on callback. 16 2662
forester wrote: lets say its common situation when object have subobjects in container and receives callbacks from contained items. and object want to move objects in containers on signal(callback).
iterator is needed for container modifications, item cannot know its iterator, at least its not easy to do so and i think cannot be done type-safe avoiding virtual functions and stuff.
'this' pointer from items is a freeby :>. the problem is, std containers want full linear scan to find iterator from pointer, while from implemention side converting pointer to std::list or std::map iterator is free (substracting offset to get std::list::node from pointer and then construct iterator from node).
this cannot be done without using reinterpret_cast or c-style cast and
invoking implementation-defined behavior.
why this is done so? this makes std containers unusable in described child to container callback scenarios - huge overhead trying to move item in container on callback.
have the container be std::set<child*> or perhaps a non-standard
hash_set. in those cases, retrieving an iterator from a child* would be
sufficiently fast for most needs.
reinterpret_cast is not needed, static_cast well be ok, node from
list/tree can look this:
template <class T>
struct node : T {
node<T> *prev_;
node<T> *next_;
};
T *ptr;
node<T> *node_ptr = static_cast<node<T>* >(ptr);
using std::set<T*> or non-standart hash to perform operation that can
be done by one processor instuction looks insane, imho.
forester wrote: reinterpret_cast is not needed, static_cast well be ok, node from list/tree can look this:
template <class T> struct node : T { node<T> *prev_; node<T> *next_; }; T *ptr; node<T> *node_ptr = static_cast<node<T>* >(ptr);
using std::set<T*> or non-standart hash to perform operation that can be done by one processor instuction looks insane, imho.
I was going to reply to this, until I saw the assertion that not doing
it is insane. That's not a good way to encourage discussion. I'll just
say that while your hack might work in some circumstances, it's
incomplete, and it won't work for other kinds of containers.
--
Pete Becker
Dinkumware, Ltd. ( http://www.dinkumware.com)
well for other types of containers unsupported conversion will result
in nice compiling error. for what are iterators traits at the end?
i am sorry if my message was looking wrong way, this is not my child
language and i used nearest word to express my feelings :).
forester wrote: i am sorry if my message was looking wrong way, this is not my child language and i used nearest word to express my feelings :).
I overreacted. Sorry.
--
Pete Becker
Dinkumware, Ltd. ( http://www.dinkumware.com)
> reinterpret_cast is not needed, static_cast well be ok, node from list/tree can look this:
template <class T> struct node : T { node<T> *prev_; node<T> *next_; }; T *ptr; node<T> *node_ptr = static_cast<node<T>* >(ptr);
You are going against the entire principle of data abstraction, etc.
etc.:-) Thats not a standard way of doing it as the language doesn't
say anything abt the way lists and maps are implemented. using std::set<T*> or non-standart hash to perform operation that can be done by one processor instuction looks insane, imho.
Not really, without knowing abt the way the containers are implemented.
Ram
forester wrote: lets say its common situation when object have subobjects in container and receives callbacks from contained items. and object want to move objects in containers on signal(callback).
iterator is needed for container modifications, item cannot know its iterator, at least its not easy to do so and i think cannot be done type-safe avoiding virtual functions and stuff.
'this' pointer from items is a freeby :>. the problem is, std containers want full linear scan to find iterator from pointer, while from implemention side converting pointer to std::list or std::map iterator is free (substracting offset to get std::list::node from pointer and then construct iterator from node).
why this is done so? this makes std containers unusable in described child to container callback scenarios - huge overhead trying to move item in container on callback.
Standard containers are no silver bullets, they are just usual pieces
of code suitable for reuse in some circumstances and not suitable in
others. Writing your own container may well save you from troubles
trying to put a square standard container in a round hole.
> You are going against the entire principle of data abstraction, etc. etc.:-) Thats not a standard way of doing it as the language doesn't say anything abt the way lists and maps are implemented.
this was just example on reply that this cannot be done compatibly.
maybe language does not say how to implement lists, but it say how they
must behave.
like contant time insertions/deletions for lists, constant time access
by index for vector.
and i am talking about constant time pointer to iterator conversion.
> Standard containers are no silver bullets, they are just usual pieces of code suitable for reuse in some circumstances and not suitable in others. Writing your own container may well save you from troubles trying to put a square standard container in a round hole.
with such approach all that rare used allocators stuff is not needed in
STL - anyone who need to tune allocations can go to hell and write his
own containers, because, you know, silver bullet and round hole stuff.
please note that this is common scenario, where template library,
designed to be flexible, does not behave effectivily.
forester wrote: Standard containers are no silver bullets, they are just usual pieces of code suitable for reuse in some circumstances and not suitable in others. Writing your own container may well save you from troubles trying to put a square standard container in a round hole.
with such approach all that rare used allocators stuff is not needed in STL - anyone who need to tune allocations can go to hell and write his own containers, because, you know, silver bullet and round hole stuff.
please note that this is common scenario, where template library, designed to be flexible, does not behave effectivily.
I believe that STL has been showing its age for quite a while. We know
well its strong and weak points now. With each new project I see STL
containers are coming out of use and new custom and highly specialized
and efficient containers are coming in, such as intrusive lists and
sets, search trees, hash tables and optimized arrays.
i am still dont see a reason why STL containers cannot have constant
time pointer to iterator conversion.
age is not excuse.
anyone? :)
forester wrote: i am still dont see a reason why STL containers cannot have constant time pointer to iterator conversion.
age is not excuse.
anyone? :)
T* ptr;
Container c;
Container::iterator iter = std::find(c.begin(), c.end(), *ptr);
well this is definitely not a constant time operation.
callback singaling is frequent, list can have big number of elements,
objects can be complex and take significant time to compare. isn't it a
"bit" innefective? thats more insane than building a hash of pointers
:) - linear search comparing whole objects.
this can be done by one pointer instruction!
forester wrote: lets say its common situation when object have subobjects in container and receives callbacks from contained items. and object want to move objects in containers on signal(callback).
iterator is needed for container modifications, item cannot know its iterator, at least its not easy to do so and i think cannot be done type-safe avoiding virtual functions and stuff.
'this' pointer from items is a freeby :>. the problem is, std containers want full linear scan to find iterator from pointer, while from implemention side converting pointer to std::list or std::map iterator is free (substracting offset to get std::list::node from pointer and then construct iterator from node).
why this is done so? this makes std containers unusable in described child to container callback scenarios - huge overhead trying to move item in container on callback.
I am not sure I understand: in order for the object in the container to take
action, you need to access it in the first place (say by invoking somthing
like iter->member_function();). Now, in this case, there is an iterator
already available, so why would the object need to know? You can just pass
it as an argument.
Alternatively, you are accessing the elements of the container from the
outside by means of a pointer to that object, right? How do you get that
pointer in the first place? If by means of an iterator to that object, then
you already have an iterator, so need for a call-back. Otherwise, maybe you
stored a pointer to that object at the time you inserted it into the
container, well in that case, you should store an iterator instead.
Clearly, I am missing something. Could you, maybe, post a piece of code that
demonstrates how your need arises? I would appreciate.
Best
Kai-Uwe Bux
forester wrote: i am still dont see a reason why STL containers cannot have constant time pointer to iterator conversion.
age is not excuse.
anyone? :)
That seems like a challenge.:-)
Forget about having constant time pointer to iterator conversion. Such
a conversion may not be possible or meaningful for all containers.
Consider vector<bool> or bitset. Pointer to a bit doesn't make any
sense on any machine which I have come across till now. For such
containers or for containers which doesn't store elements as some
sequence (arrays, trees, lists) of its elements, the elements
themselves may not be accessible though its possible to get/set their
values. I can provide a specific example of such a container if you
wish.
Thats the reason I think STL doesn't say anything on
relationship/conversion between iterators and pointers.
Ram
In article <11**********************@g49g2000cwa.googlegroups .com>,
"forester" <fo******@hacker.lv> wrote: i am still dont see a reason why STL containers cannot have constant time pointer to iterator conversion.
age is not excuse.
anyone? :)
They just don't. Perhaps the need wasn't anticipated. Perhaps the
functionality was considered too dangerous. There are other things they
don't do too. But the STL is more than just containers, so you have
some attractive options:
1. Pass the iterator information along with the contained object into
the callback code, effectively creating a new object that always knows
where it is in the container, e.g. tuple<T&, list<T>::iterator>.
2. Create a container that follows the STL requirements and adds the
functionality you want. As long as you follow the requirements you
still benefit from plug 'n play functionality with existing algorithms
and containers:
sort(my_container.begin(), my_container.end());
std::list<T> list(my_container.begin(), my_container.end());
etc.
The shiny part of the STL is the containers. The valuable part of the
STL is the standardized concepts and interfaces capable of binding N
containers to M algorithms with only N+M source code (as opposed to
N*M). This means you can make an incremental extension to the STL with
only O(1) effort (as opposed to recoding M algorithms to work with your
1 new container, or N new containers to work with your 1 new algorithm).
-Howard This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: kazio |
last post by:
Hello,
So, I need to have double linked, circular list (last element point to the
first one and first one points to the last one). I thought maybe I could
use list container from STL, but...
|
by: Vivi Orunitia |
last post by:
Hi all,
I tried looking this up in the sgi docs but it didn't provide any concrete
answer to what I'm looking for. Basically, is there any difference between
using ::iterator for a container vs...
|
by: AngleWyrm |
last post by:
I have created a new container class, called the hat.
It provides random selection of user objects, both with and without
replacement, with non-uniform probabilities. Uniform probabilities are a...
|
by: Matthias |
last post by:
Hi,
I am just reading that book by Scott Meyers. In Item 4 Meyers suggests
to always use empty() instead of size() when probing for emptyness of
STL containers. His reasoning is that size()...
|
by: Patrick Kowalzick |
last post by:
Dear NG,
I have two containers (standard library) which are nested, e.g.:
std::vector< std::vector <int> > A;
std::list< std::vector<int> > B;
These structures where put in another class...
|
by: PengYu.UT |
last post by:
In the following program, I want an iterator contain pointer pointing
to constant object not const pointer. If it is possible would you
please let me know how to do it?
#include...
|
by: Frederick Gotham |
last post by:
Inspired by page 219 of Nicolai M. Josuttis's book, I set out to write a
class for an intrinsic array which would behave, to as far an extent as
possible, like a container. Also though, I wanted no...
|
by: food4uk |
last post by:
Dear all :
I am not good at programming, please give a hand.
My data structure is very similar as an array. I actually can use the
std::vector as container to organize my data objects. However,...
|
by: ademirzanetti |
last post by:
Hi there !!!
I would like to listen your opinions about inherit from a STL class
like list. For example, do you think it is a good approach if I
inherit from list to create something like...
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
| |