package com.k13n.lt_codes;

import java.util.Random;
import java.util.List;
import java.util.ArrayList;

public final class Decoder {
  
  private final Random uniformRNG;
  private final int nPackets;
  private final List<Packet> undecodedPackets;
  private final List<Packet> decodedPackets;

  private final class Packet {
    private final byte[] data;
    private final int[] neighbours;

    public Packet(byte[] data, int[] neighbours) {
      this.data = data;
      this.neighbours = neighbours;
    }

    public byte[] getData() {
      return this.data;
    }

    public int[] getNeighbours() {
      return this.neighbours;
    }
    
  }

  public Decoder(long seed, int nPackets) {
    this.uniformRNG = new Random(seed);
    this.nPackets = nPackets;
    this.undecodedPackets = new ArrayList<Packet>();
    this.decodedPackets = new ArrayList<Packet>();
  }

  private int getNReceivedPackets() {
    return this.undecodedPackets.size() + this.decodedPackets.size();
  }

  public byte[] receive(byte[] data, int[] neighbours)
  {
    if(neighbours.length > 1)
      this.undecodedPackets.add(new Packet(data, neighbours));
    else
      this.decodedPackets.add(new Packet(data, neighbours));

    /* No way we could encode anything */
    if(this.getNReceivedPackets() < this.nPackets) {

      return null;
    } else {
      /* we need a great deal more than k = this.nPackets,
       * but for anything > k, we give it a try.
       * Yes, this means that work is done for nothing
       * FIXME: use exact lower bounds here
       *
       */

      /* ... */

      return null;
    }

  }

}

@@ -636,16 +636,251 @@
 s;%0A    %7D
+%0A%0A    public boolean hasNeighbour(int packetId)%0A    %7B%0A      for(int i = 0; i %3C this.neighbours.length; i++)%0A      %7B%0A        if(this.neighbours%5Bi%5D == packetId)%0A        %7B%0A          return true;%0A        %7D%0A      %7D%0A      return false;%0A    %7D
 %0A    %0A  

Add hasNeighbour to Packet