Palidrome Array : Array « Collections Data Structure « Java






Palidrome Array

      

/*
 * @(#)PalindromeArray.java  1.0 Apr 26, 2008
 *
 *  The MIT License
 *
 *  Copyright (c) 2008 Malachi de AElfweald <malachid@gmail.com>
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *  THE SOFTWARE.
 */
//package org.eoti.math;

import java.math.BigInteger;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;

public class PalidromeArray
  implements Iterable<BigInteger>
{
  protected static final BigInteger TWO = BigInteger.valueOf(2);
  protected ConcurrentHashMap<BigInteger,BigInteger> array;
  protected BigInteger totalLength, halfLength;
  protected boolean isEven = true;

  public static PalidromeArray fromString(String s)
  {
    String[] arr = s.split("\\|");
    BigInteger[] bi = new BigInteger[arr.length];
    for(int i=0; i<arr.length; i++)
      bi[i] = new BigInteger(arr[i]);

    return new PalidromeArray(bi);
  }

  public PalidromeArray(PalidromeArray array)
  {
    this.totalLength = array.totalLength;
    this.halfLength = array.halfLength;
    this.isEven = array.isEven;

    this.array = new ConcurrentHashMap<BigInteger,BigInteger>();
    for(BigInteger index : array.array.keySet())
      this.array.put(index, array.array.get(index));
  }

  public PalidromeArray(BigInteger[] array)
  {
    this.totalLength = BigInteger.valueOf(array.length);
    this.halfLength = totalLength.divide( TWO );
    if( MathUtil.isOdd(totalLength))
    {
      isEven = false;
      halfLength=halfLength.add(BigInteger.ONE);
    }
    this.array = new ConcurrentHashMap<BigInteger,BigInteger>();

    BigInteger index = BigInteger.ZERO;
    for(BigInteger bi : array)
    {
      this.array.put( index, bi );
      index = index.add(BigInteger.ONE);
    }
  }

  public PalidromeArray(BigInteger totalLength)
  {
    this.totalLength = totalLength;
    this.halfLength = totalLength.divide( TWO );
    if( MathUtil.isOdd(totalLength))
    {
      isEven = false;
      halfLength=halfLength.add(BigInteger.ONE);
    }
    array = new ConcurrentHashMap<BigInteger,BigInteger>();
  }

  public String toString()
  {
    StringBuilder sb = new StringBuilder();
    boolean first = true;
    for(BigInteger bi : this)
    {
      if(!first) sb.append("|");
      sb.append(bi);
      first = false;
    }

    return sb.toString();
  }

  public BigInteger halfLength(){return halfLength;}
  public BigInteger totalLength(){return totalLength;}
  public BigInteger get(BigInteger position)
  {
    /**
     * {1,4,6,4,1} would have {1,4,6} in our array
     * 0: return 1
     * 1: return 4
     * 2: return 6
     * 3: return 4
     * 4: return 1
     * totalLength = 5
     * halfLength = 3
     * get(0) returns #0
     * get(1) returns #1
     * get(2) returns #2
     * get(3) returns #1
     * get(4) returns #0
     *
     * {1,3,3,1} would have {1,3} in our array
     * array.length = 2
     * 0: return 1
     * 1: return 3
     * 2: return 3
     * 3: return 1
     * totalLength = 4
     * halfLength = 2
     * get(0) returns #0
     * get(1) returns #1
     * get(2) returns #1
     * get(3) returns #0
     */
    if(position.subtract(halfLength).signum() < 0)
      return array.get(position);

    BigInteger mid = halfLength.subtract(BigInteger.ONE);

    if(isEven)
      return array.get( mid.subtract( position.subtract(halfLength) ));

    return array.get( mid.subtract( position.subtract(mid) ));
  }
  public void set(BigInteger position, BigInteger value){array.put(position, value);}

  public Iterator<BigInteger> iterator()
  {
    return new PalidromeArrayIterator(this);
  }

  class PalidromeArrayIterator
  implements Iterator<BigInteger>
  {
    protected PalidromeArray array;
    protected BigInteger position = BigInteger.ZERO;

    public PalidromeArrayIterator(PalidromeArray array)
    {
        this.array = array;
    }

    public boolean hasNext()
    {
      return array.totalLength.subtract(position).signum() > 0;
    }

    public BigInteger next()
    {
      BigInteger index = position;
      position = position.add(BigInteger.ONE);
      return array.get(index);
    }

    public void remove()
    {
      throw new UnsupportedOperationException("Not supported");
    }
  }
}

   
    
    
    
    
    
  








Related examples in the same category

1.Initialize a static array
2.Initialization and re-assignment of arraysInitialization and re-assignment of arrays
3.Doubling the size of an arrayDoubling the size of an array
4.Timing array loop performance
5.Array 2DArray 2D
6.Can you change the .length of an array
7.Show Two-Dimensional Array of Objects
8.ArrayListDemo done over using an ArrayList
9.Array Hunt game
10.Multi Dimension Array
11.Clone Array
12.Associates keys with valuesAssociates keys with values
13.Arrays of primitives
14.Creating arrays with new
15.Array initialization
16.Creating an array of nonprimitive objects
17.Create multidimension arraysCreate multidimension arrays
18.Initializing Array ValuesInitializing Array Values
19.Creating a Two-Dimensional Array
20.Initializing a Two Dimensional ArrayInitializing a Two Dimensional Array
21.Using the length VariableUsing the length Variable
22.Triangular array
23.Grow arrayGrow array
24.Define array for class Define array for class
25.String array and output to consoleString array and output to console
26.Multiply two matrices
27.Array Of Arrays Demo 2Array Of Arrays Demo 2
28.Array Copy DemoArray Copy Demo
29.Copying Elements from One Array to Another
30.Java program to demonstrate multidimensional arraysJava program to demonstrate multidimensional arrays
31.Extend the size of an array
32.Copy an array
33.Initialize multidimensional array
34.Get array upperbound
35.To get the number of dimensions
36.Resize an array, System.arraycopy()
37.Dump array content: Convert the array to a List and then convert to String
38.java.utils.Arrays provides ways to dump the content of an array.
39.Dump multi-dimensional arrays
40.Use the new shorthand notation to iterate through an array
41.Create a repeated sequence of character
42.Reverse array elements order
43.Convert array of primitives into array of objects
44.Array Initializers
45.Reinitializes a byte array
46.Reinitializes an int array
47.Sum all elements in the array
48.Sums an array of numbers log(x1)...log(xn)
49.A class to iterate over all permutations of an array.
50.Set of utilities used to manipulate arrays.
51.ArrayUtils provides static methods for manipulating arrays when using a tool such as java.util.ArrayList is inconvenient.
52.Array Util
53.clone two dimensional array