package mapped;

import java.util.Iterator;
import net.runelite.mapping.ObfuscatedName;
import net.runelite.mapping.ObfuscatedSignature;
import net.runelite.rs.api.RSIterableNodeHashTable;
import net.runelite.rs.api.RSNode;

@ObfuscatedName("tz")
public final class IterableNodeHashTable implements Iterable, RSIterableNodeHashTable {
   @ObfuscatedName("at")
   int size;
   @ObfuscatedName("ah")
   @ObfuscatedSignature(
      descriptor = "[Lsh;"
   )
   Node[] buckets;
   @ObfuscatedName("ar")
   @ObfuscatedSignature(
      descriptor = "Lsh;"
   )
   Node currentGet;
   @ObfuscatedName("ao")
   @ObfuscatedSignature(
      descriptor = "Lsh;"
   )
   Node current;
   @ObfuscatedName("ab")
   int index;

   public IterableNodeHashTable(int var1) {
      this.index = 0;
      this.size = var1;
      this.buckets = new Node[var1];

      for(int var2 = 0; var2 < var1; ++var2) {
         Node var3 = this.buckets[var2] = new Node();
         var3.previous = var3;
         var3.next = var3;
      }

   }

   @ObfuscatedName("at")
   @ObfuscatedSignature(
      descriptor = "(J)Lsh;"
   )
   public Node get(long var1) {
      Node var3 = this.buckets[(int)(var1 & (long)(this.size - 1))];

      for(this.currentGet = var3.previous; var3 != this.currentGet; this.currentGet = this.currentGet.previous) {
         if(this.currentGet.key == var1) {
            Node var4 = this.currentGet;
            this.currentGet = this.currentGet.previous;
            return var4;
         }
      }

      this.currentGet = null;
      return null;
   }

   @ObfuscatedName("ah")
   @ObfuscatedSignature(
      descriptor = "(Lsh;J)V"
   )
   public void put(Node var1, long var2) {
      if(var1.next != null) {
         var1.remove_base();
      }

      Node var4 = this.buckets[(int)(var2 & (long)(this.size - 1))];
      var1.next = var4.next;
      var1.previous = var4;
      var1.next.previous = var1;
      var1.previous.next = var1;
      var1.key = var2;
   }

   public void put(RSNode var1, long var2) {
      this.put((Node)var1, var2);
   }

   public void put(net.runelite.api.Node node, long hash) {
      this.put((Node)node, hash);
   }

   @ObfuscatedName("ar")
   public void clear_base() {
      for(int var1 = 0; var1 < this.size; ++var1) {
         Node var2 = this.buckets[var1];

         while(true) {
            Node var3 = var2.previous;
            if(var3 == var2) {
               break;
            }

            var3.remove_base();
         }
      }

      this.currentGet = null;
      this.current = null;
   }

   @ObfuscatedName("ao")
   @ObfuscatedSignature(
      descriptor = "()Lsh;"
   )
   public Node method8955() {
      this.index = 0;
      return this.method8951();
   }

   @ObfuscatedName("ab")
   @ObfuscatedSignature(
      descriptor = "()Lsh;"
   )
   public Node method8951() {
      Node var1;
      if(this.index > 0 && this.buckets[this.index - 1] != this.current) {
         var1 = this.current;
         this.current = var1.previous;
         return var1;
      } else {
         do {
            if(this.index >= this.size) {
               return null;
            }

            var1 = this.buckets[this.index++].previous;
         } while(var1 == this.buckets[this.index - 1]);

         this.current = var1.previous;
         return var1;
      }
   }

   public Iterator iterator() {
      return new IterableNodeHashTableIterator(this);
   }

   public void clear() {
      this.clear_base();
   }
}
