examples/pl-functions/cube.py
author Dmitriy Morozov <dmitriy@mrzv.org>
Mon, 06 Mar 2023 12:48:28 -0800
changeset 301 41d5aeb87565
parent 191 2d8fba6d1d58
permissions -rw-r--r--
Change all http to https in doc/get-build-install.rst

def log2(x):
    i = 0
    while x:
        x >>= 1
        i += 1
    return i-1        

class Cube:
    def __init__(self, vertices):
        self.vertices = vertices

    def dimension(self):
        return log2(len(self.vertices))

    def boundary(self):
        for i in xrange(self.dimension()):
            for side in [0,1]:
                vertices = []
                for idx in xrange(len(self.vertices)/2):
                    # Insert i-th bit equal to side
                    v = (idx & ~(2**i-1)) << 1
                    if side: v |= 2**i
                    v |= (idx & (2**i - 1))
                    vertices.append(self.vertices[v])
                yield Cube(vertices)    

    def __hash__(self):
        return hash(tuple(self.vertices))

    def __eq__(self, other):
        return self.vertices == other.vertices

    def __repr__(self):
        return " ".join(map(str, self.vertices))


if __name__ == '__main__':
    c = Cube(['a', 'b', 'c', 'd'])
    print c
    for sb in c.boundary(): print sb