Questions tagged [iterator]

An iterator is an object-oriented programming pattern that allows traversal through a collection, agnostic of the actual implementation or object addresses in physical memory. It is one of the Gang of Four's behavioral design patterns.

iterator
2,085 questions with no upvoted or accepted answers
Filter by
Sorted by
Tagged with
13 votes
2 answers
478 views

Cannot iterate over iterator with array_walk since php 7.4

$iterator = new ArrayIterator([1, 2]); array_walk($iterator, function($item) {echo $item . PHP_EOL;}); This piece of php code outputs the items (1 and 2) in php 7.3 but it outputs nothing in php 7.4. ...
Tadas's user avatar
  • 331
11 votes
1 answer
1k views

How can I access a Rust Iterator from Python using PyO3?

I'm quite new with Rust, and my first 'serious' project has involved writing a Python wrapper for a small Rust library using PyO3. This has mostly been quite painless, but I'm struggling to work out ...
thesketh's user avatar
  • 319
8 votes
1 answer
386 views

Parallelizing a simple loop with c++17 algorithms

I have a parallel code that can be reduced to basically: #include <algorithm> #include <vector> struct TKeyObjPtr; class TObj { public: virtual void Calculate(TKeyObjPtr const &) =...
metalfox's user avatar
  • 6,531
8 votes
0 answers
554 views

What should be iterator_category for a stashing iterator?

Context: I am trying to build a container that will behave as a wrapper around a multi-dimensional array of run time defined dimensions - in fact the underlying array is of course a 1D array of the ...
Serge Ballesta's user avatar
8 votes
0 answers
1k views

When using ElasticSearch scroll api, can I skip to page n?

I'm using the elasticsearch scroll api. In some cases I'd like to return the hits on page n without returning the previous pages' hits. I believe this should be like an iterator. So I'd like to just ...
travelingbones's user avatar
8 votes
1 answer
308 views

Fastest way to apply arithmetic operations to System.Array in IronPython

I would like to add (arithmetics) two large System.Arrays element-wise in IronPython and store the result in the first array like this: for i in range(0:ArrA.Count) : arrA.SetValue(i, arrA....
c_k's user avatar
  • 1,766
7 votes
0 answers
356 views

Problem with Java Iterator created by Jackson breaks Scala Flink application

There is a Scala Flink application where I am parsing JSON using Jackson library. Parsing is handled by a custom method and it uses the lazy initiation concept to keep it fast. Now, for whatever ...
Atais's user avatar
  • 11.1k
6 votes
0 answers
584 views

How to wrap a non-object-safe Trait in an object-safe one?

I originally encountered this issue when attempting to create a Rayon ParallelIterator that a flat_map operation was applied to a dynamic number of times see this post on the Rayon issue tracker. ...
Qqwy's user avatar
  • 5,439
6 votes
1 answer
2k views

How to get a regular iterator from an asynchronous iterator?

Got an async iterable. Need a regular iterable. asyc def aiter2iter(aiter): l = [] async for chunk in aiter: l.append(chunk) return l regular_iterable = await aiter2iter(...
user avatar
6 votes
0 answers
136 views

I'm lying about being a RandomAccessIterator—does it matter?

I wrote a RandIt class (code below) that acts like an iterator, but returns random integers whenever dereferenced. The main use case is to initialize a vector with random data, as in std::vector<...
Nick Matteo's user avatar
  • 4,504
6 votes
0 answers
2k views

STL Iterator for nested containers (such as vector<vector<T> >)

Recently I had to replace a single container with a collection of similar containers. As a result, I needed a combined iterator retaining the semantics of the original iterator. The problem that I ran ...
bkxp's user avatar
  • 1,145
5 votes
1 answer
620 views

Implementing map & min that takes the tables.keys iterator as argument in Nim

I would like to define overloads of map and min/max (as originally defined in sequtils) that works for tables.keys. Specifically, I want to be able to write something like the following: import ...
Zecong Hu's user avatar
  • 2,934
5 votes
1 answer
379 views

How to access mutable iterables inside a nested loop

Every now and then, I run into the same issue with borrowing (or not borrowing) mutable variables inside a loop, and I finally sat down and compiled a minimal example. As a result, the code is a ...
Jesko Hüttenhain's user avatar
5 votes
0 answers
139 views

Is there any scenario where an iterator can do something an iteratee+enumerator cannot

I'm looking into using alternatives to iterators in my code base, and was recently introduced to iteratee as a potential solution. Iteratee come from functional programming and is an abstraction to ...
abden003's user avatar
  • 1,325
5 votes
1 answer
81 views

I receive a MemError when I interpolate data with generator expressions and iterators from a regular grid on a mesh >2000 times

this is my first question here at stackoverflow, because I started scripting with Python3. Application I made a Python3 script that writes the load definition of a moveable heat source for a finite ...
delta112358's user avatar
5 votes
1 answer
104 views

Will the creation of an iterator_traits<InIter>::value_type trigger a deference when passed? (Exception testing)

I'm having trouble purposely throwing exceptions(for testing purposes) for functions that do not specifically deference iterators. To get an idea of what I am doing, take my decorator_iterator struct: ...
Syntactic Fructose's user avatar
5 votes
1 answer
714 views

Groovy error when currying

The code which gives the error is on ideone here, sorry I haven't been able to give a more minimal example. Because it's a bit long, I'll explain it. The Unfoldr class, and unfoldr function ...
Clinton's user avatar
  • 22.8k
5 votes
2 answers
3k views

Complexity of iteration through std::set

I know that time complexity of iterating throught whole set takes O(n) time, where n is size of set. The question is, what is complexity of iterating between two iterators, itBegin and itEnd? Maybe ...
OneOne's user avatar
  • 131
4 votes
1 answer
208 views

Tuple wrapper that works with get, tie, and other tuple operations

I have written a fancy "zip iterator" that already fulfils many roles (can be used in for_each, copy loops, container iterator range constructors etc...). Under all the template code to work ...
rubenvb's user avatar
  • 75.6k
4 votes
2 answers
892 views

Clean up on for loop break

Can you create an iterable in python which runs clean up code when for loop exits? Something like: from random import randint class Iterable: def __iter__(self): return self def ...
user avatar
4 votes
0 answers
203 views

std::deque iterator vs reference invalidation

Why does pushing an element on one of the ends of a std::deque invalidate all the existing iterators (All references remain valid though)? I understand that deque is implemented as an array of arrays ...
nishantsingh's user avatar
  • 4,577
4 votes
1 answer
210 views

How to implement a unified interface of child classes's iterators in base class?

In C++, say I have a base class Base, and many child classes are derived from it. Each child class contains an array of some type and length. class Base { //... int baseData; virtual ...
Leedehai's user avatar
  • 3,840
4 votes
1 answer
2k views

Jinja/WTForms: Displaying a single radio button

How does one display a single radio button (out of a group) from a WTForms form in Jinja? Basically, I want to have a form on a webpage that looks like: o Radio option1 Various sub-options ...
mrputter's user avatar
4 votes
0 answers
671 views

EhCache in distributed mode

We are trying to use EhCache as a distributed cache in our application. EhCache instance are embedded in our server and we used a terracota cluster. All our server (and ehcache) instances ...
Thibaud's user avatar
  • 41
4 votes
0 answers
415 views

Why does list iterator point in between the elements returned by next() and previous() methods

I was going through JavaDocs and I found out that ListIterator in Java works slightly in a different manner from what we expect. I thought that after we have used next() or previous() methods the ...
Sanku's user avatar
  • 465
4 votes
1 answer
1k views

tensorflow - TFRecordWriter takes up too much memory when writing to a file?

I'm working on a large dataset, which has 306400 images to be processed. But the thing I'm to do is really simple: resize the image and then write to a .TFRecords file. However, I got an out of ...
jinglei's user avatar
  • 3,309
4 votes
2 answers
3k views

Open binary file in zip archive as ZipExtFile

I'm trying to access a binary stream (via a ZipExtFile object) from a data file contained in a Zip archive. To incrementally read in a text file object from the archive, this would be fairly ...
Gord Stephen's user avatar
  • 1,045
4 votes
2 answers
1k views

Cast of const_iterator to iterator& in definition of std::set

C++ STL uses a red-black tree to store data inside std::set and std::map. I noticed that set::iterator is actually a typedef of the const iterator of the red black tree: //All code snippets taken ...
Jing Li's user avatar
  • 639
4 votes
0 answers
904 views

Range based for loop index

A custom iterator on a std::vector returns an object of the following class: class chunk { public: int value_; size_t fake_index_; }; The iterator updates both the fake_index_ and value_. ...
dot dot dot's user avatar
4 votes
0 answers
795 views

Pandas DataFrame, Iterate through groups is very slow

I have a dataframe df with ~ 300.000 rows and plenty of columns: | COL_A | ... | COL_B | COL_C | -----+--------+-...--+--------+--------+ IDX -----+--------+-...--+--------+--------+ 'AAA'| ...
Hansi's user avatar
  • 137
4 votes
0 answers
195 views

What is the fastest way to get the number of items in an iterator?

I have a folder structure with something over 300.000 files and folders in it. When I iterate over this structure to get all files of a certain type, this take about half a second to complete: $path ...
user avatar
4 votes
3 answers
405 views

C++ custom iterator?

I have been trying to implement a custom forward iterator for a simple class. This class is a -pretty bad- abstraction for a fixed-array (I cannot change it unfortunately). Elements can be accessed ...
Stadium's user avatar
  • 61
4 votes
1 answer
180 views

PHP Memory Leak in class structure

hoping someone can help me out with this, bit stumped. So doing some mucking about with images and polygons, but getting a weird memory leak. So I have a wrapper class called canvas which just ...
user2481985's user avatar
4 votes
0 answers
1k views

ListIterator next and previous. Detail

So by reading this from http://docs.oracle.com/javase/6/docs/api/java/util/ListIterator.html#next%28%29 emphasized text E next() Returns the next element in the list. This method may be called ...
Rollerball's user avatar
  • 12.9k
4 votes
2 answers
737 views

How to create a past-the-end iterator?

I have created a binary tree structure to store a bounded volume hierarchy, to make it easier to use (and safer) I created two iterators to complement it: breadth-first and depth-first. The breadth-...
cmannett85's user avatar
3 votes
0 answers
91 views

Iterator semantics for class with wrapped C functions and no container data

I have a RAII-managed class which uses functions from a C library. Their signature usually is: int get_next_data(handle_type* handle, struct struct_type** output_param); and return success/failure ...
Nikolay Kosev's user avatar
3 votes
1 answer
328 views

Rust: additional trait bounds in conjunction with higher-ranked lifetime bounds

I want trait implementations in Rust to be able to return arbitrary iterators (of specific item type) that may reference the original object with a lifetime 'a without having to explicitly mention 'a ...
497e0bdf29873's user avatar
3 votes
0 answers
79 views

Iterator transformer to range adaptor in C++20 ranges library

I am looking into the ranges library of C++20 and I was wondering how easy it might be to build a range adaptor from a function template<Iterator I,Iterator J> I f(J) (or maybe a more ...
fakedrake's user avatar
  • 6,708
3 votes
1 answer
1k views

How to transform a generator function into an iterator object in python?

Often generator functions are simpler to write than iterators, e.g. consider this example generator function def generator_fn(): x = 0 for y in range(10): x += y yield x Is ...
flonk's user avatar
  • 3,786
3 votes
1 answer
2k views

Efficient way to compare two similar maps in java

Imagine I have two maps: HashMap<TestClass, Integer> map1, map2; with TestClass being defined as: class TestClass { int i; public TestClass(int i){ this.i = i; } } And the ...
Jojomatik's user avatar
  • 166
3 votes
1 answer
201 views

Buffer Overflow Error while using Iterators in C++

It is a question of leetcode : https://leetcode.com/explore/learn/card/fun-with-arrays/521/introduction/3237/ Given an array nums of integers, return how many of them contain an even number of digits. ...
Kaustubh Dwivedi's user avatar
3 votes
0 answers
6k views

Requests module ideal chunk size for large files?

I am working with the requests library to stream some large file and download , I want to set the chunk size to 1MB . so i am setting the chunk_size to 1000000 because 10^6 bytes should be 1 megabyte, ...
Sandiph Bamrel's user avatar
3 votes
1 answer
552 views

Will c++ range-based for loop call the destructor of iterator?

When trying to implement an iterator of a double-pointer, I found something interesting: The timing of the destructor being called confuses me. Unable to understand the memory addresses of the ...
AlaRduTP's user avatar
3 votes
0 answers
282 views

Why does executing iterator->end() in the immediate window give read access violation, even when the iterator is in scope - for C++ vector::iterator?

I have the following program: int main() { vector<vector<int> > vv; vector<int> v1; vector<int> v2; v1.push_back(1); v1.push_back(2); v1.push_back(3); ...
Vikram Singh's user avatar
3 votes
0 answers
3k views

Iterating over machine instructions in LLVM

Is there a way to iterate over machine instructions without using basic blocks? It is possible with plain Instructions using an instr_iterator within the context of a Function as shown here: Iterating ...
CodeHoarder's user avatar
3 votes
2 answers
2k views

Implementing an Iterator on Composite Design Pattern Through Separate Class - Is It programmatically valid?

I have implemented the Composite Design Pattern and then expanded the Composite class to also implement Iterable, however the iterator() method (which returns an iterator object) is also part of the ...
user avatar
3 votes
1 answer
360 views

Is it possible to determine if a passed iterator belongs to a associative std container?

I want to determine the std container type (at least associative or linear) from an iterator passed to a function. I have searched for appropriate iterator traits and type traits without success. ...
ttuna's user avatar
  • 111
3 votes
0 answers
288 views

Missing empty suffix iterator for std::regex_token_iterator?

I expect I'm misunderstanding the description at cppreference, but could someone explain why the following code: std::regex comma(","); std::string string("one,two,"); std::sregex_token_iterator ...
Uri Granta's user avatar
  • 1,864
3 votes
1 answer
729 views

Iterating over a ResultIterable object using jdbi

Yesterday I posted a question regarding retrieving data from a Db and iterating over it. Someone helpfully pointed my to JDBI and away from raw data types. Caveats: I am a tester forst and foremost ...
Steerpike's user avatar
  • 1,783

1
2 3 4 5
42