0

I have constructed a suffix array which is implemented by a ArrayList.

I want to use this list to search a suffix in the suffix array. For this I have sorted teh list and used binary search but the "search" function keeps returning -1

I don't know what I am doing wrong here. I have overrided Hashcode and equals too.

I also changed the default definition of "equals" but still I keep getting the same output

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SuffixArrayNaive   {


/**
 * This class represents the elements in the suffix array with their respective
 * locations
 * @author Aneesh
 *
 */
private class Elements implements Comparator<Elements>{
    String value;
    int position;

    public Elements() {
        // TODO Auto-generated constructor stub
    }
    public Elements(String value, int position) {
        //super();
        this.value = value;
        this.position = position;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + getOuterType().hashCode();
        result = prime * result + position;
        result = prime * result + ((value == null) ? 0 : value.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        Elements other = (Elements) obj;
        if (!getOuterType().equals(other.getOuterType()))
            return false;
        if (value == null) {
            if (other.value != null)
                return false;
        } else if (!value.equals(other.value))
            return false;
        return true;
    }

    private SuffixArrayNaive getOuterType() {
        return SuffixArrayNaive.this;
    }

    @Override
    public int compare(Elements o1, Elements o2) {
        // TODO Auto-generated method stub
        if (o1.value.compareTo(o2.value)>0){
            return 1;
        }
        if (o1.value.compareTo(o2.value)<0){
            return -1;
        }
        return 0;
    }
    @Override
    public String toString() {
        return "value=" + value + ", position=" + position + "\n";
    }
}

List<Elements> suffixArray = new ArrayList<>();

public static void main(String[] args) {
    // TODO Auto-generated method stub
    String baseString="banana";
    new SuffixArrayNaive().buildSuffixArray(baseString);
    String query="na";
    new SuffixArrayNaive().search(query);
}


private int search(String query) {
    // TODO Auto-generated method stub
    int result = -1;
    int res = Collections.binarySearch(suffixArray, new Elements(query, -1), new Elements());
    //printing -1 always!!
    //what is wrong?
    System.out.println(res);
    result = res;
    return result;
}

private void buildSuffixArray(String baseString) {
    // TODO Auto-generated method stub
    //generate all suffixes of the baseString
    int length= baseString.length();

    for (int i=0;i<length;++i){
        suffixArray.add(new Elements(baseString.substring(i), i));
    }

    Collections.sort(suffixArray, new Elements());
}
}
Aneesh K
  • 147
  • 2
  • 10

1 Answers1

2

Issue is over here:

new SuffixArrayNaive().buildSuffixArray(baseString);
String query="na";
new SuffixArrayNaive().search(query);

In one Object of SuffixArrayNaive, you are creating suffix array (by populating list) while searching on another instance of SuffixArrayNaive, array (list) which is empty.

Remember your list is define as non static:

List<Elements> suffixArray = new ArrayList<>();

Meaning you will have one list associated with every object that you create with new keyword and it would be empty when you create the object.

You can resolve this by creating suffix array in one object and searching in the same object where you builded the suffix array:

SuffixArrayNaive suffixArrayNaive = new SuffixArrayNaive();
suffixArrayNaive.buildSuffixArray(baseString);
String query="na";
suffixArrayNaive.search(query);
SMA
  • 36,381
  • 8
  • 49
  • 73