iterator

How can I specialize an algorithm for iterators that point to complex values?

北战南征 提交于 2021-01-29 13:26:54
问题 I am trying to write an algorithm that works on iterators (similar to the STL algorithms) however I need to write a specialization of the algorithm to act differently when the iterators point to complex values vs regular double values. Here is a basic example: #include <complex> #include <iostream> #include <vector> using namespace std; template <typename InputIt> void DoSomething(InputIt first, InputIt last) { cout << "Regular Double" << endl; for (; first != last; ++first) { cout << *first

Create closure returning iterator on string

青春壹個敷衍的年華 提交于 2021-01-29 10:57:35
问题 I want to write a closure that takes an object and returns an iterator from it. The idea is to store the closure in a structure and apply as needed: fn main() { let iter_wrap = |x: &String| Box::new(x.chars()); let test = String::from("test"); for x in iter_wrap(&test) { println!("{}", x); } } This causes the error: error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in function call due to conflicting requirements --> src/main.rs:2:45 | 2 | let iter_wrap = |x: &String|

Java: Iterator is supposed to always return the same object, but should change it between calls of next()

邮差的信 提交于 2021-01-29 09:36:49
问题 The Iterator I'm writing is supposed to iterate through all fixed-sized connected subgraphs of another given graph. As these subgraphs are all very similar (only minor changes in a search-tree based algorithm), I don't want to create a new object to return on each call of next() because of runtime concerns. Therefore I have an object subGraph that is returned every time, but is changed by a function update() between calls of next() , so that the object works like a view of the current state

How can I extend the lifetime of a temporary variable inside of an iterator adaptor in Rust?

雨燕双飞 提交于 2021-01-29 07:51:09
问题 I have a method make_iter() which creates an Iterator with multiple adapters in Rust, which can be simplified as the following MCVE: fn make_iter(first: &First) -> Box<dyn Iterator<Item = String> + '_> { Box::new(first.make_objects().flat_map(|second| { second .iter() .filter(|third| third.as_str() != "t2") .flat_map(|third| vec![format!("{}.A", third), format!("{}.B", third)].into_iter()) .chain( vec![ format!("{}.A", second.name()), format!("{}.B", second.name()), ] .into_iter(), ) })) }

Overflow evaluating the requirement when returning a recursive iterator using impl trait

孤人 提交于 2021-01-29 07:18:42
问题 I'm trying to iterate depth-first over a tree structure in Rust. I thought I had a really nice concise solution for this, but I can't get it to compile. Conceptually it's pretty simple: iterate over the children, get each child's depth first iterator, flatten them, and chain the current node's metadata iterator to it. #[derive(Debug, Eq, PartialEq)] struct Node { metadata: Vec<i64>, children: Vec<Box<Node>>, } impl Node { fn depth_first_metadata_iter(&self) -> impl Iterator<Item = &i64> + '_

Implementation of methods of interface

空扰寡人 提交于 2021-01-29 07:17:31
问题 I want to implement a representation of matrices. for that I have two types of matrices - regular and sparse, which differ in their implementation - one holds a vector, and the second a map of indices and value, both inherit from Matrix class. For that, I'm using the strategy pattern, where I create the base abstract class Matrix , two classes that inherit from Matrix - RegMatrix and SparseMatrix , and MyMatrix that holds a pointer to a Matrix . I want to implement the + operator, which

Is it!=container.end() design mistake/feature or just necessity?

孤者浪人 提交于 2021-01-28 23:36:50
问题 recently I was thinking about how it would be nice if iterators implicitly converted to bool so you could do auto it = find(begin(x),end(x), 42); if (it) //not it!=x.end(); { } but thinking about it I realized that this would mean that either it would had to be set to "NULL", so that you couldnt use the it directly if you want to do something with it (you would have to use x.end() ) or you could use it but iter size would have to be bigger(to store if what it points to was .end() or not). So

Is it!=container.end() design mistake/feature or just necessity?

爱⌒轻易说出口 提交于 2021-01-28 23:32:16
问题 recently I was thinking about how it would be nice if iterators implicitly converted to bool so you could do auto it = find(begin(x),end(x), 42); if (it) //not it!=x.end(); { } but thinking about it I realized that this would mean that either it would had to be set to "NULL", so that you couldnt use the it directly if you want to do something with it (you would have to use x.end() ) or you could use it but iter size would have to be bigger(to store if what it points to was .end() or not). So

Is it!=container.end() design mistake/feature or just necessity?

和自甴很熟 提交于 2021-01-28 23:09:47
问题 recently I was thinking about how it would be nice if iterators implicitly converted to bool so you could do auto it = find(begin(x),end(x), 42); if (it) //not it!=x.end(); { } but thinking about it I realized that this would mean that either it would had to be set to "NULL", so that you couldnt use the it directly if you want to do something with it (you would have to use x.end() ) or you could use it but iter size would have to be bigger(to store if what it points to was .end() or not). So

Rust Inspect Iterator: cannot borrow `*` as immutable because it is also borrowed as mutable

試著忘記壹切 提交于 2021-01-28 18:27:13
问题 Why can't I push to this vector during inspect and do contains on it during skip_while ? I've implemented my own iterator for my own struct Chain like this: struct Chain { n: u32, } impl Chain { fn new(start: u32) -> Chain { Chain { n: start } } } impl Iterator for Chain { type Item = u32; fn next(&mut self) -> Option<u32> { self.n = digit_factorial_sum(self.n); Some(self.n) } } Now what I'd like to do it take while the iterator is producing unique values. So I'm inspect -ing the chain and