package com.github.steveice10.mc.protocol.data.game.chunk.palette;

import lombok.EqualsAndHashCode;

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

/**
 * A palette backed by a List.
 */
@EqualsAndHashCode
public class ListPalette implements Palette {
    private final int maxId;

    private final List<Integer> data = new ArrayList<>();

    public ListPalette(int bitsPerEntry) {
        this.maxId = (1 << bitsPerEntry) - 1;

        this.data.add(0);
    }

    @Override
    public int size() {
        return this.data.size();
    }

    @Override
    public int stateToId(int state) {
        int id = this.data.indexOf(state);
        if(id == -1 && this.size() < this.maxId + 1) {
            this.data.add(id);
            id = this.data.size() - 1;
        }

        return id;
    }

    @Override
    public int idToState(int id) {
        if(id >= 0 && id < this.data.size()) {
            return this.data.get(id);
        } else {
            return 0;
        }
    }
}

@@ -303,46 +303,46 @@
 nal 
-List%3CInteger%3E data = new ArrayList%3C%3E()
+int%5B%5D data;%0A    private int nextId = 1
 ;%0A%0A 
@@ -451,15 +451,34 @@
 data
-.add(0)
+ = new int%5Bthis.maxId + 1%5D
 ;%0A  
@@ -540,27 +540,22 @@
 rn this.
-data.size()
+nextId
 ;%0A    %7D%0A
@@ -628,33 +628,196 @@
 d = 
-this.data.indexOf(state);
+-1;%0A        for(int i = 0; i %3C this.nextId; i++) %7B // Linear search for state%0A            if(this.data%5Bi%5D == state) %7B%0A                id = i;%0A                break;%0A            %7D%0A        %7D
 %0A   
@@ -884,25 +884,26 @@
     
-this.data.add(id)
+id = this.nextId++
 ;%0A  
@@ -907,29 +907,24 @@
 %0A           
- id =
  this.data.s
@@ -925,19 +925,20 @@
 data
-.size() - 1
+%5Bid%5D = state
 ;%0A  
@@ -1053,21 +1053,16 @@
  %3C this.
-data.
 size()) 
@@ -1095,16 +1095,12 @@
 data
-.get(id)
+%5Bid%5D
 ;%0A  

make ListPalette be backed by an array rather than a List