Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright The Sett Ltd, 2005 to 2014.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.thesett.aima.logic.fol.wam.indexing;
 
IntIntOpenLinearTable implements a CodeBufferTable using open addressing and linear probing with a step size of one.

CRC Card
Responsibilities Collaborations
Establish a table on a ByteBuffer.
Associate an address with a key.
Look up an address by key.

Author(s):
Rupert Smith
 
 public class IntIntOpenLinearTable implements CodeBufferTable
 {
    
The byte buffer holding the table data.
 
     private ByteBuffer buffer;

    
The offset of the base of the table within the byte buffer.
 
     private int offset;

    
The size of the table in number of entries (not bytes or integers).
 
     private int size;

    
 
     public void setup(ByteBuffer bufferint tint n)
     {
         this. = buffer;
         this. = t;
         this. = (n >> 3); // Shift by 3 as key and value must be stored as a pair, making 8 bytes per entry.
     }

    
 
     public int get(int key)
     {
         int addr = addr(hash(key));
 
         while (true)
         {
             int tableKey = .getInt(addr);
 
             if (key == tableKey)
             {
                 return .getInt(addr + 4);
             }
 
             if (tableKey == 0)
             {
                 return 0;
             }
 
             addr += 8;
         }
     }

    
 
     public void put(int keyint val)
     {
         int addr = addr(hash(key));
 
         while (true)
         {
             int tableKey = .getInt(addr);
 
             if (key == tableKey)
             {
                 break;
             }
 
             if (tableKey == 0)
             {
                 break;
             }
 
             addr += 8;
         }
 
         .putInt(addrkey);
         .putInt(addr + 4, val);
     }

    
Converts an entry offset into the table, into an address within the table, relative to the zero offset of the underlying byte buffer.

Parameters:
entry The entry offset.
Returns:
The address within the byte buffer at which the entry can be found.
    private int addr(int entry)
    {
        return  + ((entry % ) << 3);
    }

    
Computes a hash of the key.

Parameters:
key The key to hash.
Returns:
The hashed key.
    private int hash(int key)
    {
        return key;
    }
New to GrepCode? Check out our FAQ X