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

59
LINES

< > BotCompany Repo | #1034675 // OrderedMultiIterator

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (5469L/30K).

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 ArrayList<Stream<A>> streams;
15  
  bool sorted;
16  
  
17  
  void add(Iterable<A> it) {
18  
    add(iteratorOrNull(it));
19  
  }
20  
  
21  
  void add(Iterator<A> iterator) {
22  
    if (iterator == null || !iterator.hasNext()) ret;
23  
    A element = iterator.next();
24  
    streams.add(new Stream<A>(iterator, element));
25  
    sorted = false;
26  
  }
27  
  
28  
  void addAll(Iterable<? extends Iterator<A>> iterators) {
29  
    fOr (it : iterators) add(it);
30  
  }
31  
  
32  
  public bool hasNext() {
33  
    ret nempty(streams);
34  
  }
35  
  
36  
  public A next() {
37  
    if (!sorted) {
38  
      sortInPlace(streams);
39  
      set sorted;
40  
    }
41  
    
42  
    var stream = first(streams);
43  
    A element = stream.element;
44  
    
45  
    if (!stream.advance())
46  
      streams.remove(0);
47  
    else {
48  
      // put stream in the right place
49  
      
50  
      int i = 0, n = streams.size()-1;
51  
      Stream<A> stream2;
52  
      while (i < n && stream.compareTo(stream2 = streams.get(i+1)) >= 0)
53  
        streams.set(i++, stream2);
54  
      streams.set(i, stream);
55  
    }
56  
      
57  
    ret element;
58  
  }
59  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034675
Snippet name: OrderedMultiIterator
Eternal ID of this version: #1034675/8
Text MD5: 2dcf2978fc88b92fae975fb9fdf780dc
Transpilation MD5: 899e49dc2a90fcb752bf2393348a8590
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:13:51
Source code size: 1443 bytes / 59 lines
Pitched / IR pitched: No / No
Views / Downloads: 140 / 254
Version history: 7 change(s)
Referenced in: [show references]