Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

44
LINES

< > BotCompany Repo | #1034678 // OrderedMultiIterator [backup using TreeSet, works but is slow]

JavaX fragment (include)

1  
sclass OrderedMultiIterator<A extends Comparable<A>> extends ItIt<A> {
2  
  srecord noeq Stream<A extends Comparable<A>>(Iterator<A> iterator, A element) is Comparable<Stream<A>> {
3  
    public int compareTo(Stream<A> stream) {
4  
      ret element.compareTo(stream.element);
5  
    }
6  
    
7  
    bool advance() {
8  
      if (!iterator.hasNext()) false;
9  
      element = iterator.next();
10  
      true;
11  
    }
12  
  }
13  
  
14  
  new TreeSet<Stream<A>> streams;
15  
  
16  
  void add(Iterable<A> it) {
17  
    add(iteratorOrNull(it));
18  
  }
19  
  
20  
  void add(Iterator<A> iterator) {
21  
    if (iterator == null || !iterator.hasNext()) ret;
22  
    A element = iterator.next();
23  
    streams.add(new Stream<A>(iterator, element));
24  
  }
25  
  
26  
  void addAll(Iterable<? extends Iterator<A>> iterators) {
27  
    fOr (it : iterators) add(it);
28  
  }
29  
  
30  
  public bool hasNext() {
31  
    ret nempty(streams);
32  
  }
33  
  
34  
  public A next() {
35  
    var stream = first(streams);
36  
    A element = stream.element;
37  
    
38  
    streams.remove(stream);
39  
    if (stream.advance())
40  
      streams.add(stream);
41  
      
42  
    ret element;
43  
  }
44  
}

Author comment

Began life as a copy of #1034675

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034678
Snippet name: OrderedMultiIterator [backup using TreeSet, works but is slow]
Eternal ID of this version: #1034678/1
Text MD5: 92622b60fcd8868603c5e3238f048b9b
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-03-01 20:02:57
Source code size: 1079 bytes / 44 lines
Pitched / IR pitched: No / No
Views / Downloads: 166 / 179
Referenced in: [show references]