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
68
votes
6answers
3k 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 ...
15
votes
3answers
773 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...
4
votes
1answer
275 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 ...
30
votes
2answers
55k 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 ...
45
votes
5answers
173k 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 ...
8
votes
2answers
476 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 ...
7
votes
1answer
218 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 ...
14
votes
1answer
2k 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 ...
9
votes
1answer
4k 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
117 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
14k 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: ...
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 ...
12
votes
1answer
242 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
466 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 ...
11
votes
2answers
37k 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: ...
9
votes
2answers
324 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 ...
7
votes
3answers
1k views

Merge Sort implemented using iterators

...
7
votes
2answers
4k 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
3answers
158 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
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. ...
6
votes
2answers
1k 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. ...
13
votes
2answers
27k 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 ...
8
votes
2answers
435 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
112 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
333 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 ...
6
votes
2answers
2k 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
0answers
136 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 ...
5
votes
1answer
187 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: <...
4
votes
2answers
418 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
119 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
112 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
0answers
136 views

Array slice type in Java - follow-up

See the previous iteration. I have essentially fixed most of the issues mentioned by maaartinus. However, I left the size field as it is. Namely, if the user wants ...
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 ...
21
votes
3answers
4k 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
958 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
3k 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. ...
5
votes
2answers
520 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
848 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 ...
4
votes
2answers
209 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 ...
4
votes
1answer
75 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
169 views

Sieve prime generator C++

I had some issues with the behavior of the iterator in my prime generator: ...
4
votes
0answers
236 views

Deep map, Python

Goal: apply fn to every element of an arbitrarily nested iterable (tuple, list, dict, np.ndarray), including to iterables. Ex: ...
4
votes
2answers
4k 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: ...
4
votes
3answers
3k views

Is ToIEnumerable<T> good practice?

I have an extension method that does this: ...
3
votes
4answers
4k views

Iterator pattern/iterator class

I have implemented a simple iterator. Just want to check if I have missed anything. ...
3
votes
1answer
89 views

Iterator for traversing a tree

What I want to achieve is the following: ...
3
votes
2answers
101 views

Deep len, Python

Goal: find total number of elements in a nested iterable of arbitrary depth. My shot: ...