from library.analysis import *


def padLeft(n, text, p=" "):
    while len(text) < n:
        text = text + p
    return text


class Table:
    def __init__(self, cols):
        self.rows = []
        self.cols = cols
        self.colSize = [len(c) for c in cols]

    def showHead(self):
        h = ""
        b = ""
        for (i,c) in enumerate(self.cols):
            if h != "":
                h += "|"
                b += "|"
            h += padLeft(2 + self.colSize[i], " " + c)
            b += padLeft(2 + self.colSize[i], "", "-")

        print(h)
        print(b)

    def showRow(self, row):
        r = ""
        for (i,c) in enumerate(row):
            if r != "":
                r += "|"
            r += padLeft(2 + self.colSize[i], " " + c)
        print(r)

    def show(self):
        self.showHead()
        for r in self.rows:
            self.showRow(r)

    def addRow(self, row):
        for i in range(len(row)):
            row[i] = str(row[i])
            self.colSize[i] = max(self.colSize[i], len(row[i]))
        self.rows.append(row)


class CheckTree:
    def __init__(self, data=None):
        self.tree = {}
        if data is not None:
            for x in data:
                self.add(x)

    def add(self, xs):
        t = self.tree
        for x in xs:
            if x not in t:
                t[x] = {}
            t = t[x]

    def isIn(self, xs):
        t = self.tree
        for x in xs:
            if x not in t:
                return False
            t = t[x]
        return True

def isSame(xs, ys):
    for (x, y) in zip(xs, ys):
        if x != y:
            return False
    return True

def countCommon(setA, setB):
    n = 0
    print("->")
    tree = CheckTree(setB)
    for x in setA:
        if tree.isIn(x):
            n += 1
    print("<-")
    tree = CheckTree(setA)
    for x in setB:
        if tree.isIn(x):
            n += 1
    return n

table = Table(["dataset", "features", "points total", "majority", "minority", "common"])
if __name__ == "__main__":
    for ds in testSets:
        d = loadDataset("data_input/" + ds)
        print((d.data0.shape[0], d.data1.shape[0]))
        table.addRow(
            [ ds
            , d.data0.shape[1]
            , d.data0.shape[0] + d.data1.shape[0]
            , d.data0.shape[0]
            , d.data1.shape[0]
            , countCommon(d.data0, d.data1)
            ])

table.show()
