iterator

Iterating over a HashMap of HashMaps in Java (or Scala)

I created a class Foo that has the method toArray() that returns an Array<Int>. Now, I have a HashMap mapping Strings to HashMaps, which map Objects to Foo. That is: HashMap<String,HashMap<Object,Foo>> And I want to create a new object of type: HashMap<String,HashMap<Object,Array<Int>>> That is obtained by calling the function toA...

Closing a java.util.Iterator

Hi all, I've implemented a custom java.util.Iterator using a resource that sould be released at the end using a close() method. That resource could be a java.sql.ResultSet, a java.io.InputStream etc... public interface CloseableIterator<T> extends Iterator<T> { public void close(); } Some external libraries using this iterator...

How do to multiple imports in Python ?

In Ruby, instead of repeating the "require" (the "import" in Python) word lots of times, I do %w{lib1 lib2 lib3 lib4 lib5}.each { |x| require x } So it iterates over the set of "libs" and "require" (import) each one of them. Now I'm writing a Python script and I would like to do something like that. Is there a way to, or do I need to ...

Can iterators be reset in Python?

Can I reset an iterator / generator in Python? I am using DictReader and would like to reset it (from the csv module) to the beginning of the file. thanks. ...

Java: SortedSet "cursor"-style iterator

I need to iterate both forwards and backwards in a sorted set. If I use NavigableSet, I get a strictly-forward iterator and a strictly-backward iterator (iterator() and descendingIterator()) but none that can move forward and backward. What's the time complexity of NavigableSet.lower() and higher() ? I can use those instead, but am relu...

iterator compound assignment operator

In C++ primer 4th edition by Lippman, compound assignment operators for iterator for vector and deque are given : iter1 += iter2 iter1 -= iter2 Compound-assignment versions of iterator addition and subtraction. Assigns the value of adding or subtracting iter1 and iter2 into iter1. But when I want to use them, it gives error. More...

What is happening in this Python program?

I'd like to know what is getting assigned to what in line 8. # Iterators class Fibs: def __init__(self): self.a = 0 self.b = 1 def next(self): self.a, self.b = self.b, self.a+self.b # <--- here return self.a def __iter__(self): return self fibs = Fibs() for f in fibs: if f > 100...

Architectural C++/STL question about iterator usage for O(1) list removal by external systems.

This is a pretty straightforward architectural question, however it's been niggling at me for ages. The whole point of using a list, for me anyway, is that it's O(1) insert/remove. The only way to have an O(1) removal is to have an iterator for erase(). The only way to get an iterator is to keep hold of it from the initial insert() or t...

PHP: Is it programatically possible to append iterator value to array key name in loop?

Hello, I have a multidimensional array object, and in a loop I would like to append an iterator to the key and obtain the value. Sample code for demonstration: $array_object->example1 = 1; $array_object->example2 = 2; $i = 1; while ($i <= 2) { echo ($array_object->example . $i); //this does not work //how to a...

Problems understanding iterators and operator overload in c++

We have a class example and I just don't get it. I don't quite understand how the operator() works in this case, and everything starting with sort. I looked at the output after running the program, and I don't see how those values are obtained. sort indices array: 2 8 10 4 1 7 5 3 0 9 6 11 replay numbers array: 37 33 29 36 32 35 39 ...

Explain Iterator Syntax on Ruby on Rails

I started learning Ruby on Rails and found myself confounded by the syntax, so I had to read about somet of the Ruby syntax. I learned the syntax from http://www.cs.auckland.ac.nz/references/ruby/doc_bundle/Manual/man-1.4/syntax.html: method_call do [`|' expr...`|'] expr...end They call it an Iterator. I understand an iterator runs th...

does advanced for each loop in java call method that returns an array to iterate over every single time?

His, I have a question to the advanced for loop in java. If there is a method call Method.returnArray() and I iterate over the array with for (ArrayElement e : Method.returnArray()) { //do smth } will the .returnArray() be called by every iteration? Thanks. ...

Fancy way to read a file in C++ : strange performance issue

The usual way to read a file in C++ is this one: std::ifstream file("file.txt", std::ios::binary | std::ios::ate); std::vector<char> data(file.tellg()); file.seekg(0, std::ios::beg); file.read(data.data(), data.size()); Reading a 1.6 MB file is almost instant. But recently, I discovered std::istream_iterator and wanted to try it in o...

How to add more than one piece of data to an array follow the .each method

I have the following method: def speciate chem_formula.each { |chemical| @chem_species = chemical.scan(/[A-Z][^A-Z]*/) puts @chem_species } end that produces: H2 S O4 @chem_species = ["S", "O4"] from: @chem_formula = ["H2" "SO4"] How do you set the array to include all iterations? That is how do you output ["H2", "S"...

can't convert Array into Integer

I'm trying to iterate through an array, @chem_species = ["H2", "S", "O4"] and multiply a constant times the amount of constants present: H = 1.01 * 2, S = 32.1 * 1 and so on. The constants are of course defined within the class, before the instance method. The code I've constructed to do this does not function: def fw x = @chem_specie...

CORBA exception causes an orphan iterator error, why would omniorb CORBA interfere with VC++ 2008 CRT?

We have a function that, when tested using MS VC++ 2008 using omniorb CORBA libraries causes an orphaned iterator error. If we recompile all of our source using the _HAS_ITERATOR_DEBUGGING=0, the error goes away. I cannot paste the exact code, but here is a facsimile: void funtion(CORBA::ANY& blah) { sometype::iterator itr; try...

Help Using RegexIterator in PHP

I have yet to find a good example of how to use the php RegexIterator to recursively traverse a directory. The end result would be I want to specify a directory and find all files in it with some given extensions. Say for example only html/php extensions. Furthermore, I want to filter out folders such of the type .Trash-0, .Trash-50...

Iterate across lines in two files simultaneously in Python

I have two files, and I want to perform some line-wise operation across both of them. (In other words, the first lines of each file correspond, as do the second, etc.) Now, I can think of a number of slightly cumbersome ways to iterate across both files simultaneously; however, this is Python, so I imagine that there is some syntactic sh...

Way of determining the size of the container

Is there any other way to determine size of the container than : //those are valid iterators from a container BidIt begin; BidIt end; std::size_t size = 0; while (begin != end) {//Here throug iterating I'm getting adventually the correct size ++size; ++begin; } but I wonder if I could check size of this container by for example...

Interview: Design an iterator for a collection of collections

Design an iterator for a collection of collections in java. The iterator should hide the nesting, allowing you to iterate all of the elements belonging to all of the collections as if you were working with a single collection ...