The Stack Overflow podcast is back! Listen to an interview with our new CEO.

Questions tagged [iterator]

An iterator is an object-oriented programming pattern which for the most part functions similarly to a pointer to an object inside a collection with the added ability to traverse through the collection in a defined manner, agnostic of the actual implementation or even object addresses in physical memory. Iterators may be further limited in particular traversal directions.

Filter by
Sorted by
Tagged with
63
votes
6answers
2k views

infix_iterator code

I've previously posted this on Stack Overflow, and am considering submitting it to Boost for wider distribution, but thought perhaps it would be best to put it up here for peer review first, and see ...
14
votes
3answers
616 views

Sieve31, my sieve of Eratosthenes returning IEnumerable<int>

This very fast, simple sieve quickly finds 31 bit primes. It uses a memory efficient BitArray for odd numbers. How a 32 bit int...
3
votes
1answer
213 views

Sieve32, a simple 32 bit sieve returning IEnumerable<uint> using C#

What a difference 1 bit makes! This very fast, simple sieve of Eratosthenes quickly finds 32 bit primes. It uses a memory efficient BitArray for odd numbers – but ...
27
votes
2answers
47k views

Custom iterator for a linked list class

I have made a LinkedList class. This is a singly-linked-list and I want to make a forward_iterator for this class without using ...
43
votes
4answers
168k views

Simple Example of an Iterable and an Iterator in Java

I was a TA for a first-year Java programming course this year. As a very simple example of iterables/iterators, I wrote the following code for the students. I am curious if any styling or other ...
7
votes
1answer
186 views

Preprocessing iterator

After tweaking semantics a lot, it ended up being a very poor object version of std::transform<>() that can be used with other std algorithms. Basically the ...
7
votes
1answer
2k views

Transforming iterator

The question is follow-up to preprocessing iterator. Specification: Given Functor functor (which is Callable) and ...
7
votes
2answers
424 views

Postponed Prime Sieve in Swift

Motivated by this question, I looked out for other "infinite prime generators", i.e. functions which produce the list of prime numbers in increasing order and do not have an a-priori upper limit (such ...
11
votes
1answer
1k views

Merge n sorted iterators

Over the weekend I was curious about efficiently merging multiple sorted iterators together, and for them to be in sorted order. This is quite like a challenge on HackerRank: You’re given the ...
8
votes
1answer
3k views

Binary trees in Rust: iterators

This is a follow-up to Basic binary tree manipulation in Rust, where Shepmaster suggested that I implement the Iterator trait for the binary tree. This ended up ...
6
votes
1answer
2k views

An iterator returning all possible permutations of a list in Java

I have this class that iterates over all permutations of an input list: PermutationIterable.java: ...
2
votes
1answer
114 views

Growing potatoes in delayed sequences

In order to grow potatoes I'm implementing the retry pattern. If I fail to grow them, I'd like to try it again later. The first part I've implemented is a collection of delay generators. Their base ...
12
votes
3answers
9k views

Extension method replacing elements from an IEnumerable<T> collection

Today I needed a method to replace all 'x' values in a said collection. Since there is no such method by default in .NET I wrote my own: ...
12
votes
1answer
229 views

Array-like container for uints shorter than 8 bits

Follow-up: Array-like container for uints shorter than 8 bits (Rev 1) First of all: yes, I need this kind of array to generate data for a piece of hardware, and it must be stored and manipulated ...
11
votes
1answer
431 views

Max element of input iterator range

I came across this SO question on why std::max_element requires a ForwardIterator (as apposed to an ...
9
votes
2answers
246 views

Multi-dimensional container using operator[]

This is a late followup to a previous question of mine on same subject. Context: I want to have a tool that allows to use all the C++ goodies (algorithms, for loop on containers, etc.) on a ...
8
votes
1answer
2k views

Chain multiple containers

Python has itertools.chain(), but C++ has nothing. This is my attempt to implement such a thing. Looking mainly for comments about potentially smarter ways of doing ...
7
votes
3answers
1k views

Merge Sort implemented using iterators

...
7
votes
3answers
146 views

Endless stream: stream a range and return default value if the range is exhausted

Introduction: Motivation Sometimes there is a need to merge N > 1 ranges, and they don't have equal length, but there is a value which can be used in case the range is already exhausted. It is ...
6
votes
2answers
794 views

Iterator of Container of Container

Based on this question I though I would pull together an iterator that iterated over a container of containers. ...
5
votes
3answers
10k views

Flatten iterator for nested list

Given a list which can can contain elements as well as lists, write an iterator to flatten a nested list. Please make this code better and suggest any improvements. ...
13
votes
2answers
25k views

Self-made linked list iterator

I recently made a linked list for myself, the code of which was posted here, if anyone would be interested. Now, as the next task, I had to make an Iterator for my ...
9
votes
2answers
26k views

How can I avoid unchecked cast warning in my generic recursive Iterator?

It's somewhat odd that Java's collection framework has no iterator for recursive data structures. Since I needed something like this, I wrote my own. First off, I need recursive elements: ...
8
votes
2answers
371 views

Array-like container for uints shorter than 8 bits (Rev 1)

This is a follow-up to Array-like container for uints shorter than 8 bits In short: The PackedBitfieldArray should be a kind of container for packed (unsigned) ...
7
votes
2answers
3k views

QuickSort C++ Implementation using Iterators

I implemented quicksort using iterators. I am will be grateful, if someone can review my piece of code. BTW, I am just selecting the last element as the pivot (which I plan to randomize later) I am ...
7
votes
2answers
105 views

Pythonish integer range in Java - follow-up

(See the previous iteration.) I have refactored the Range; now it looks like this: Range.java: ...
6
votes
2answers
1k views

Generic in order traversal iterator for binary trees

This actually started off as member type for the binary_search_tree, but after realizing that it doesn't compare to values or use the value objects inside of it, I ...
6
votes
2answers
178 views

Walking directory tree like python does

I like python's os.walk and I was missing it in C#. It's a very convenient API so I thought I create my own utility that does something similar. The original API ...
5
votes
1answer
182 views

Pythonish integer range in C++

Python has that range: >>> range(10, -20, -3) [10, 7, 4, 1, -2, -5, -8, -11, -14, -17] I have this C++ implementation of exactly the same facility: range.h: <...
5
votes
0answers
101 views

General algorithm to calculate sums of all subsets of a given sequence of numbers

Background A recent question Print sums of all subsets made its way to the Hot Network Questions list. The problem is simple: Print sums of all subsets of a given set Given an array of ...
4
votes
2answers
238 views

Modern C++ compliant insertion sort

This is a kind of follow up to my previous sort implementation question with specific questions to insertion sort, and Modern C++ idioms. Anybody seeing this post can see how I got to this modern ...
3
votes
5answers
115 views

Product iteration while checking for overlaps

This is an extended question to the following question I posted Class scheduling tool with image output Context of previous question: I have a multilayered dictionary that contains information ...
3
votes
3answers
111 views

Linked list implementation - follow-up

Follow-up to this question as recommended. Can this code be improved from an OOP paradigm perspective? ...
1
vote
2answers
3k views

Combination generator in Java - 2nd iteration

I have refactored the previous combination generator, and now it is an iterator returning combinations as lists. It hides larger constant factors as the previous version, yet it is really easy to use: ...
1
vote
1answer
66 views

Phishing Project UserIterator Implementation

As I stated in an answer comment on my previous question, the UserIterator has a much larger footprint than I think most people realize. This question is here to further explain the project, the ...
19
votes
3answers
3k views

Pythonish integer range in Java

Introduction After getting stuck with a Pythonish integer range in C++, I rewrote the facility in Java. In Python you can say: ...
11
votes
1answer
471 views

iterating over the values of a list of ordered dictionaries

I am facing the following problem: I have a special data structure that is a dictionary whose keys are integers (dimensions). The values are also dictionaries whose keys are strings (geometric types) ...
7
votes
2answers
2k views

Sorted vector (aka flat_set) for objects (pointers) with custom embedded key (functor used)

BEFORE YOU READ: this link should be included when you just don't like the format of the question and for those that read this question for the first time, the link may give you the idea what happened ...
5
votes
1answer
736 views

Binary trees in Rust: more iterators! (non-consuming variant)

This is a follow-up to Binary trees in Rust: iterators, where Shepmaster suggested that I implement a non-consuming iterator. This was relatively straightforward. I chose to mirror the design of the ...
5
votes
2answers
117 views

C++ linked list iterator implementation

I'm a first year computer engineering student and would appreciate feedback on some code. I've created a linked-list with an iterator to be able to range for loops like: ...
5
votes
1answer
2k views

Convert a dictionary of iterables into an iterator of dictionaries

I have a user-editable dictionary of content used to fill in a string.Template and I wanted to allow the user to fill the same template with several values at once. ...
4
votes
1answer
59 views

General use multi-dimensional container using operator[]

This is a followup to a previous question of mine on same subject. Context I want to have a tool that allows to simulate as much as possible a C++ standard container for a multidimensional ...
4
votes
1answer
168 views

Sieve prime generator C++

I had some issues with the behavior of the iterator in my prime generator: ...
4
votes
3answers
2k views

Is ToIEnumerable<T> good practice?

I have an extension method that does this: ...
4
votes
2answers
174 views

Directed graph path enumerator in Java

I have this iterator class that expects in its constructor two directed graph nodes, source and target, and upon each ...
3
votes
1answer
3k views

Array slice type in Java

Edit See the next iteration at Array slice type in Java - follow-up. I have this "slice" type for managing array subranges. It is kind of the same thing as Pythons slice notation, yet I did not add ...
3
votes
0answers
144 views

An iterator returning all possible solutions to n-queen problem in Java

(See the next iteration.) Given a positive integer \$n\$, I have an iterator that returns all possible solutions to \$n\$-queens problem one by one: QueenIterableV1.java: ...
3
votes
1answer
69 views

Handling arguments in a Python-like range() function in JavaScript

I'm making a Python-like range() function (see Python docs) using JavaScript. This function can take from 1 to 3 arguments. There are 3 variables (...
3
votes
1answer
170 views

Efficiently determine every factor of a given natural number

It's not hard to find all of the factors of a given natural number. Below is a pretty fast python function that can do just that: (Modified version of Steinar Lima's function, https://stackoverflow....
3
votes
4answers
4k views

Iterator pattern/iterator class

I have implemented a simple iterator. Just want to check if I have missed anything. ...