package io.sigpipe.sing.graph;

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

import io.sigpipe.sing.dataset.feature.Feature;

public class GeoTrie {

    private Vertex root = new Vertex();

    public GeoTrie() {

    }

    public void addHash(String geohash, CountContainer payload) {
        List<Vertex> path = hashToPath(geohash.toLowerCase());
        path.get(path.size() - 1).setData(payload);
        root.addPath(path.iterator());
    }

    public CountContainer query(String geohash) {
        List<Vertex> path = hashToPath(geohash.toLowerCase());
        CountContainer cc = new CountContainer();
        query(root, path.iterator(), cc);
        return cc;
    }

    private void query(
            Vertex vertex, Iterator<Vertex> path, DataContainer container) {
        if (path.hasNext()) {
            Vertex queryVertex = path.next();
            Vertex neighbor = vertex.getNeighbor(queryVertex.getLabel());
            if (neighbor == null) {
                /* Specified hash character wasn't found, there are no matches
                 * for this query. */
                return;
            } else {
                query(neighbor, path, container);
            }
        } else {
            for (Vertex v : vertex.getAllNeighbors()) {
                query(v, path, container);
            }
        }

        if (vertex.hasData()) {
            container.merge(vertex.getData());
        }
    }

    private List<Vertex> hashToPath(String geohash) {
        List<Vertex> path = new ArrayList<>(geohash.length());
        for (char c : geohash.toCharArray()) {
            path.add(
                    new Vertex(
                        new Feature("geohash", String.valueOf(c))));
        }
        return path;
    }

    public static void main(String[] args) throws Exception {

        GeoTrie gt = new GeoTrie();
        gt.addHash("9xqj", new CountContainer(1, 1));
        gt.addHash("9xq2", new CountContainer(1, 1));
        gt.addHash("9xd3", new CountContainer(1, 0));
        gt.addHash("djjs", new CountContainer(0, 1));
        gt.addHash("djj9", new CountContainer(1, 1));
        gt.addHash("djj2", new CountContainer(1, 1));
        CountContainer cc = gt.query("9");

        System.out.println(gt.root.numDescendants());
        System.out.println(cc.a);
        System.out.println(cc.b);
    }
}

@@ -1,8 +1,1327 @@
+/*%0ACopyright (c) 2016, Colorado State University%0AAll rights reserved.%0A%0ARedistribution and use in source and binary forms, with or without modification,%0Aare permitted provided that the following conditions are met:%0A%0A1. Redistributions of source code must retain the above copyright notice, this%0A   list of conditions and the following disclaimer.%0A2. Redistributions in binary form must reproduce the above copyright notice,%0A   this list of conditions and the following disclaimer in the documentation%0A   and/or other materials provided with the distribution.%0A%0AThis software is provided by the copyright holders and contributors %22as is%22 and%0Aany express or implied warranties, including, but not limited to, the implied%0Awarranties of merchantability and fitness for a particular purpose are%0Adisclaimed. In no event shall the copyright holder or contributors be liable for%0Aany direct, indirect, incidental, special, exemplary, or consequential damages%0A(including, but not limited to, procurement of substitute goods or services;%0Aloss of use, data, or profits; or business interruption) however caused and on%0Aany theory of liability, whether in contract, strict liability, or tort%0A(including negligence or otherwise) arising in any way out of the use of this%0Asoftware, even if advised of the possibility of such damage.%0A*/%0A%0A
 package 

Add license