I am trying to write an iterator for a std::set that allows the
iterator target to be modified. Here is some relvant code:
template <class Set> // Set is an instance of std::set<>
class Iterator
{
public :
typedef typename Set::value_type T;
typedef typename Set::iterator SetIterator;
Iterator(Set& container, const SetIterator& it);
Iterator& operator=(const Iterator& rhs);
T& operator*() { return m_proxy; }
T* operator->() { return &m_proxy; }
Iterator& operator++();
~Iterator() { sync(); }
private :
void sync();
Set& m_container;
SetIterator m_iterator;
T m_proxy;
};
template <class Set>
Iterator<Set>:: Iterator(Set& container, const SetIterator& it) :
m_container(con tainer),
m_iterator(it)
m_proxy(*it) {}
template <class Set>
Iterator<Set>& Iterator<Set>:: operator++()
{
sync();
++m_iterator;
return *this;
}
template <class Set>
Iterator<Set>& Iterator<Set>:: operator=(const Iterator& rhs)
{
sync();
m_container = rhs.m_contaner;
m_iterator = rhs.m_iterator;
m_proxy = rhs.m_proxy;
return *this;
}
template <class Set>
void Iterator<Set>:: sync()
{
typedef Set::key_compar e Compare;
if (Compare(*m_ite rator, m_proxy) || Compare(m_proxy , *m_iterator))
{
// sort order will be changed
container.erase (m_iterator);
m_iterator = container.inser t(m_proxy);
}
else
{
// modify set element directly (should be faster than having to do
// an insert)
const_cast<T&>( *m_iterator) = m_proxy;
}
return;
}
Am I on the right track with this, or is this a really bad idea? One
concern I have is concurrency. If more than one iterator points to
the same element it is possible for that element to get out of sync. 26 8796
"Michael Klatt" <md*****@ou.edu > wrote in message
news:2c******** *************** ***@posting.goo gle.com... I am trying to write an iterator for a std::set that allows the iterator target to be modified. Here is some relvant code:
..... Am I on the right track with this, or is this a really bad idea? One
It's a really bad idea.
Sets are ordered collections.
Changing a member potentially changes its position in the set and therefore
must be banned.
If you wish to argue that your operartor< only compares some fields and you
were only going to change some others then I say:
1. How is the compiler supposed to know and enforce that.
2. Your comparison operator is flawed.
3. What you really want is a map
concern I have is concurrency. If more than one iterator points to the same element it is possible for that element to get out of sync.
"Michael Klatt" <md*****@ou.edu > wrote in message
news:2c******** *************** ***@posting.goo gle.com... I am trying to write an iterator for a std::set that allows the iterator target to be modified. Here is some relvant code:
..... Am I on the right track with this, or is this a really bad idea? One
It's a really bad idea.
Sets are ordered collections.
Changing a member potentially changes its position in the set and therefore
must be banned.
If you wish to argue that your operartor< only compares some fields and you
were only going to change some others then I say:
1. How is the compiler supposed to know and enforce that.
2. Your comparison operator is flawed.
3. What you really want is a map
concern I have is concurrency. If more than one iterator points to the same element it is possible for that element to get out of sync.
On Mon, 5 Apr 2004 17:18:40 +0100, "Nick Hounsome" <nh***@blueyond er.co.uk>
wrote: "Michael Klatt" <md*****@ou.edu > wrote in message news:2c******* *************** ****@posting.go ogle.com... I am trying to write an iterator for a std::set that allows the iterator target to be modified. Here is some relvant code: .... Am I on the right track with this, or is this a really bad idea? One
It's a really bad idea. Sets are ordered collections. Changing a member potentially changes its position in the set and therefore must be banned. If you wish to argue that your operartor< only compares some fields and you were only going to change some others then I say: 1. How is the compiler supposed to know and enforce that. 2. Your comparison operator is flawed. 3. What you really want is a map
I'm not sure it is /that/ bad an idea, although I'm not yet sure it is a
/good/ one. Upon first reading the OP's idea I immediately thought about
the ordering problem, but then I took a look at his code. It looks like his
special Iterator tests to see whether assignment through the iterator
would cause a change in the ordering, and special-cases the two
possibilities. If it does change the ordering, it just erases and inserts
(and for this purpose it stores a ref to the entire container). If not,
then it performs an in-place copy into the element (casting away constness
to make it compile). I tried this to test it, but got a slew of errors from
the template code:
int main()
{
set<int> s;
// fill up s... including the value 6
// (I used InitUtil, so I'll omit all of that)
set<int>::itera tor it;
if ((it = find(s.begin(), s.end(), 6)) != s.end())
cout << "Found it!" << endl;
else
{
cout << "Didn't find 6..." << endl;
exit(1);
}
Iterator<set<in t> > ncit(s, it);
*ncit = 75;
cout << "after replacing 6 with 75: " << endl;
// dump out the values
return 0;
}
He didn't say it was supposed to work yet ;-)
I think when it does, we may have something at least a little bit
interesting to talk about.
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at: www.bdsoft.com/tools/stlfilt.html
On Mon, 5 Apr 2004 17:18:40 +0100, "Nick Hounsome" <nh***@blueyond er.co.uk>
wrote: "Michael Klatt" <md*****@ou.edu > wrote in message news:2c******* *************** ****@posting.go ogle.com... I am trying to write an iterator for a std::set that allows the iterator target to be modified. Here is some relvant code: .... Am I on the right track with this, or is this a really bad idea? One
It's a really bad idea. Sets are ordered collections. Changing a member potentially changes its position in the set and therefore must be banned. If you wish to argue that your operartor< only compares some fields and you were only going to change some others then I say: 1. How is the compiler supposed to know and enforce that. 2. Your comparison operator is flawed. 3. What you really want is a map
I'm not sure it is /that/ bad an idea, although I'm not yet sure it is a
/good/ one. Upon first reading the OP's idea I immediately thought about
the ordering problem, but then I took a look at his code. It looks like his
special Iterator tests to see whether assignment through the iterator
would cause a change in the ordering, and special-cases the two
possibilities. If it does change the ordering, it just erases and inserts
(and for this purpose it stores a ref to the entire container). If not,
then it performs an in-place copy into the element (casting away constness
to make it compile). I tried this to test it, but got a slew of errors from
the template code:
int main()
{
set<int> s;
// fill up s... including the value 6
// (I used InitUtil, so I'll omit all of that)
set<int>::itera tor it;
if ((it = find(s.begin(), s.end(), 6)) != s.end())
cout << "Found it!" << endl;
else
{
cout << "Didn't find 6..." << endl;
exit(1);
}
Iterator<set<in t> > ncit(s, it);
*ncit = 75;
cout << "after replacing 6 with 75: " << endl;
// dump out the values
return 0;
}
He didn't say it was supposed to work yet ;-)
I think when it does, we may have something at least a little bit
interesting to talk about.
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at: www.bdsoft.com/tools/stlfilt.html
"Leor Zolman" <le**@bdsoft.co m> wrote in message
news:qt******** *************** *********@4ax.c om... On Mon, 5 Apr 2004 17:18:40 +0100, "Nick Hounsome"
<nh***@blueyond er.co.uk> wrote:
I'm not sure it is /that/ bad an idea, although I'm not yet sure it is a /good/ one. Upon first reading the OP's idea I immediately thought about the ordering problem, but then I took a look at his code. It looks like
his special Iterator tests to see whether assignment through the iterator would cause a change in the ordering, and special-cases the two possibilities. If it does change the ordering, it just erases and inserts (and for this purpose it stores a ref to the entire container). If not, then it performs an in-place copy into the element (casting away constness to make it compile). I tried this to test it, but got a slew of errors
from the template code:
It is an interesting idea but I think it has a serious difficulty.
A range represented by a pair of these iterators will act in an unexpected
(i.e. bad) way if their are used to modify elements.
Whilst every element in the range will be encountered at least once, some
elements may be encountered more than once. This occurs when modifying a
element repositions it beyond the current iterator rather than before it.
The problem is not so much in the software but in the concept of what it
means to iterate through a range in a set whilst modifying it. As soon as it
is modified it is no longer the same range as it may contain a different
number of different elements in a different order.
Richard
"Leor Zolman" <le**@bdsoft.co m> wrote in message
news:qt******** *************** *********@4ax.c om... On Mon, 5 Apr 2004 17:18:40 +0100, "Nick Hounsome"
<nh***@blueyond er.co.uk> wrote:
I'm not sure it is /that/ bad an idea, although I'm not yet sure it is a /good/ one. Upon first reading the OP's idea I immediately thought about the ordering problem, but then I took a look at his code. It looks like
his special Iterator tests to see whether assignment through the iterator would cause a change in the ordering, and special-cases the two possibilities. If it does change the ordering, it just erases and inserts (and for this purpose it stores a ref to the entire container). If not, then it performs an in-place copy into the element (casting away constness to make it compile). I tried this to test it, but got a slew of errors
from the template code:
It is an interesting idea but I think it has a serious difficulty.
A range represented by a pair of these iterators will act in an unexpected
(i.e. bad) way if their are used to modify elements.
Whilst every element in the range will be encountered at least once, some
elements may be encountered more than once. This occurs when modifying a
element repositions it beyond the current iterator rather than before it.
The problem is not so much in the software but in the concept of what it
means to iterate through a range in a set whilst modifying it. As soon as it
is modified it is no longer the same range as it may contain a different
number of different elements in a different order.
Richard
On Mon, 5 Apr 2004 18:34:07 +0100, "richard.forres t1"
<ri************ **@ntlworld.com > wrote: A range represented by a pair of these iterators will act in an unexpected (i.e. bad) way if their are used to modify elements. Whilst every element in the range will be encountered at least once, some elements may be encountered more than once. This occurs when modifying a element repositions it beyond the current iterator rather than before it.
The problem is not so much in the software but in the concept of what it means to iterate through a range in a set whilst modifying it. As soon as it is modified it is no longer the same range as it may contain a different number of different elements in a different order.
Richard
Yup. So there's at least one way of using it down the drain.
I doubt anyone (including the OP) was planning to go proposing one of these
as an addition to the Standard ;-) but perhaps the technique, possibly
generalized to all associative containers, might find a niche as a simple
shorthand for the otherwise-required rigmarole to "change" the value of an
element of one of those containers. It (or something along those lines)
sounds like it mght still be useful.
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at: www.bdsoft.com/tools/stlfilt.html
On Mon, 5 Apr 2004 18:34:07 +0100, "richard.forres t1"
<ri************ **@ntlworld.com > wrote: A range represented by a pair of these iterators will act in an unexpected (i.e. bad) way if their are used to modify elements. Whilst every element in the range will be encountered at least once, some elements may be encountered more than once. This occurs when modifying a element repositions it beyond the current iterator rather than before it.
The problem is not so much in the software but in the concept of what it means to iterate through a range in a set whilst modifying it. As soon as it is modified it is no longer the same range as it may contain a different number of different elements in a different order.
Richard
Yup. So there's at least one way of using it down the drain.
I doubt anyone (including the OP) was planning to go proposing one of these
as an addition to the Standard ;-) but perhaps the technique, possibly
generalized to all associative containers, might find a niche as a simple
shorthand for the otherwise-required rigmarole to "change" the value of an
element of one of those containers. It (or something along those lines)
sounds like it mght still be useful.
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at: www.bdsoft.com/tools/stlfilt.html
Leor Zolman <le**@bdsoft.co m> wrote in message news:<qt******* *************** **********@4ax. com>... He didn't say it was supposed to work yet ;-) I think when it does, we may have something at least a little bit interesting to talk about. -leor
No, the sample I posted is definitely not ready for prime time. There
are a few syntax errors, and at least one logic error. There are also
some member functions that need to be added. Fixing these is left as
an exercise for the reader. :)
Actually, I've got a version that compiles, and more or less (more on
that later) works, but there's a problem in constructor Iterator(Set*
container, const SetIterator& it). There was a problem with
deferencing container->end(), but even accounting for this I still get
a segmentation fault. I'm also not sure about the semantics of the
boolean operators. Are two Iterators eqivalent merely if they
reference the same set element, or does the local proxy object have to
be the same as well? Also, there is still the problem of concurrency
if there are multiple iterators pointing to the same element.
For now, I think I'm going to be less ambitious and develop a simple
replace() function that operates directly on a container rather than
via an iterator. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Leif K-Brooks |
last post by:
Is there a word for an iterable object which isn't also an iterator, and
therefor can be iterated over multiple times without being exhausted?
"Sequence" is close, but a non-iterator iterable could technically
provide an __iter__ method without implementing the sequence protocol,
so it's not quite right.
|
by: Grant Edwards |
last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273
Alan Kay said something I really liked, and I think it applies
equally well to Python as well as the languages mentioned:
I characterized one way of looking at languages in this
way: a lot of them are either the agglutination of features
or they're a crystallization of style. Languages such as
APL, Lisp, and Smalltalk are what you might call style...
|
by: Jacob Page |
last post by:
I have released interval-0.2.1 at
http://members.cox.net/apoco/interval/. IntervalSet and
FrozenIntervalSet objects are now (as far as I can tell) functionality
equivalent to set and frozenset objects, except they can contain
intervals as well as discrete values.
Though I have my own unit tests for verifying this claim, I'd like to
run my code through actual set and frozenset unit tests. Does any such
code exist? Is it in pure...
|
by: Alexander Stippler |
last post by:
Hi,
I've got trouble with some well known issue. Iterator invalidation. My
situation:
for (it=v.begin(); it!=v.end(); ++it) {
f(*it);
}
Under some circumstances, f may alter the container by removing the current
|
by: andreas |
last post by:
Hello,
I have a problem with iterators in a fairly complex polygonal mesh data
structure which is implemented using lists of geometric entities.
However, the problem in itself is fairly simple:
I need to define special iterator values. In particular, I want a null
iterator. This is different from end() which means the end of this
list. I want an iterator value that is known to just not correspond to
any element. It must be possible to...
| |
by: fungus |
last post by:
I mentioned earlier to day that I was moving some code
from VC++6 to VC++2005 and having trouble with the
new iterators. There's all sorts of problems cropping
up in the code thanks to this change.
a) With pointer-iterators you could set an iterator
to null to mark it as invalid, you can't do that
any more.
b) You can't use const_cast with iterators.
|
by: Steven D'Aprano |
last post by:
I thought that an iterator was any object that follows the iterator
protocol, that is, it has a next() method and an __iter__() method.
But I'm having problems writing a class that acts as an iterator. I have:
class Parrot(object):
def __iter__(self):
return self
def __init__(self):
self.next = self._next()
|
by: tonylamb |
last post by:
Hi All,
I seem to be getting an error with my code when running Intel C++ compiler via Visual Studio 2005. The error generated is "Expression:map/set iterator not dereferencable"
I've given my code below in the hope that someone can spot the problem.
Many Thanks.......
set<stl_index>::iterator eit1, eit2=elmt_ids.begin();
bool non_consecutive(false);
// 0. checking whether the elements...
|
by: Yosifov Pavel |
last post by:
Whats is the way to clone "independent" iterator? I can't use tee(),
because I don't know how many "independent" iterators I need. copy and
deepcopy doesn't work...
--pavel
|
by: Luis Zarrabeitia |
last post by:
Hi there.
For most use cases I think about, the iterator protocol is more than enough.
However, on a few cases, I've needed some ugly hacks.
Ex 1:
a = iter() # assume you got the iterator from a function and
b = iter() # these two are just examples.
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |