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)

sclass OrderedMultiIterator<A extends Comparable<A>> extends ItIt<A> {
  srecord noeq Stream<A extends Comparable<A>>(Iterator<A> iterator, A element) is Comparable<Stream<A>> {
    public int compareTo(Stream<A> stream) {
      ret element.compareTo(stream.element);
    }
    
    bool advance() {
      if (!iterator.hasNext()) false;
      element = iterator.next();
      true;
    }
  }
  
  new TreeSet<Stream<A>> streams;
  
  void add(Iterable<A> it) {
    add(iteratorOrNull(it));
  }
  
  void add(Iterator<A> iterator) {
    if (iterator == null || !iterator.hasNext()) ret;
    A element = iterator.next();
    streams.add(new Stream<A>(iterator, element));
  }
  
  void addAll(Iterable<? extends Iterator<A>> iterators) {
    fOr (it : iterators) add(it);
  }
  
  public bool hasNext() {
    ret nempty(streams);
  }
  
  public A next() {
    var stream = first(streams);
    A element = stream.element;
    
    streams.remove(stream);
    if (stream.advance())
      streams.add(stream);
      
    ret element;
  }
}

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: 164 / 177
Referenced in: [show references]