Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please update Collection example in README.md #1

Closed
janckerchen opened this issue May 11, 2017 · 6 comments
Closed

Please update Collection example in README.md #1

janckerchen opened this issue May 11, 2017 · 6 comments

Comments

@janckerchen
Copy link

janckerchen commented May 11, 2017

The doc looks outdated, I checked the codebase, there is no Iter<T> at all.

for(Iter<IndicatorValue*> iter(list); !iter.end(); iter.next()){
...
}

I wan to iterate the list and find the target node, and insert a new T before the target node. maybe like this

foreach(T, list) {
  if (it.current match condistion) {
  list.insert(it,  new T()); // how to code ?
 }
}
@dingmaotu
Copy link
Owner

@janckerchen Thank you for the feedback. Doc has been updated.

The Iter class had been deprecated for a while, as I found a new trick to implement a foreach macro. Do check out the foreach macro, it works just as you wanted.

It is generally not recommended to do insertion while in iteration. The iterator will be invalidated due to size change. A temporary solution might be:

Vector<int> idx;
int i=-1;
foreach(T, list)
{
++i;
T e = it.current();
if(e match condition) idx.push(i+idx.size()); // note that index will change after previous insertions
}
foreach(int,idx) list.insert(it.current(), new T);

I know this is verbose but this is currently the only safe way to do what you want.
I have plans to make iterator support element removal and insertion. As we are in rapid development of a complex product (which uses this library), the collection code will see constant improvements. Any feedback is welcome.

@janckerchen
Copy link
Author

janckerchen commented May 12, 2017

I just found a insert() method in LinkedList, but the params type is different from your example, further more it is under protected limit, cant access by list.insert().

protected:
   void              insert(LinkedNode<T>*ref,LinkedNode<T>*node);

@dingmaotu
Copy link
Owner

Use public insert of ArrayList. You use insertion position by index, not by element reference.

@janckerchen
Copy link
Author

janckerchen commented May 16, 2017

foreach is a macro, as your code style, I think it should be UPPER_CASE.

If I just wanna find something then break in iteration, SafeDelete(it) at the end of loop, as your foreach macro defined, have no chance to be executed then memory leak show up.

@dingmaotu
Copy link
Owner

Normally macros should be upper case, but I want foreach to be a language construct just like a keyword.

Yes, the foreach macro can not handle exceptional control flow where it pointer has no chance to be deleted. Afraid that Iter class has to be bring back 😜 . This bug will be fixed soon. Thanks for the bug report . You can open new issues for easier tracking.

@janckerchen
Copy link
Author

ok, I will open new issue, thanks to your effort.

dingmaotu pushed a commit that referenced this issue Jul 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants