// special data structure to get store by specific filters
var BSharpTree = function()
{
    this.root = null;
    this.root = [];
    
    this.comparator = function(x)
    {
        return x;
    };

    this.add = function(text, object, node)
    {
        var node = node || this.root;

        node.objects = node.objects || [];
        if (node != this.root)
        {
            node.objects.push(object);
        }

        if (text.length > 0)
        {
            var chr = text.charCodeAt(0) - 44;
            var next = node[chr] = chr == 0 ? this.root : node[chr] || [];
            this.add(text.substring(1), object, next);
        }
    };

    this.search = function(text, objects)
    {
        var node = this.root;
        var result = null;

        while (text.length > 0)
        {
            var chr = text.charCodeAt(0) - 44;
            text = text.substring(1);

            if (chr == 0)
            {
                result = this.search(text, result);
                
                break;
            }
            else
            {
                node = node[chr];
                
                if (!node)
                {
                    return [];
                }
                
                result = node.objects;
            }
        }

        if (objects)
        {
            if (node == this.root)
            {
                return objects;
            }
            else
            {
                return this.intersect(result, objects);
            }
        }
        else
        {
            if (node == this.root)
            {
                return [];
            }
            else
            {
                return result;
            }
        }
    };

    this.intersect = function(a, b)
    {
        var i = 0;
        var j = 0
        var intersect = [];

        a = a.sort();
        b = b.sort();

        while (i < a.length && j < b.length)
        {
            if (a[i] == b[j])
            {
                intersect.push(a[i]);
                i++;
                j++;
            }
            else if (a[i] > b[j])
            {
                j++;
            }
            else
            {
                i++;
            }
        }

        return intersect;
    };
};
