'x: dimension of torus in x-direction
y: dimension of torus in y-direction
n: number of hosts per switch'

def build(self, x, y, n=1):

    if ((x < 3) or (y < 3)):
        raise Exception('Please    use    3x3    or    greater    for    compatibility    with    2.1')
    if (n == 1):
        genHostName = (lambda loc, k: ('h%s' % loc))
    else:
        genHostName = (lambda loc, k: ('h%sx%d' % (loc, k)))
    (hosts, switches, dpid) = ({}, {}, 0)
    for i in range(0, x):
        for j in range(0, y):
            loc = ('%dx%d' % ((i + 1), (j + 1)))
            dpid = (((i + 1) * 256) + (j + 1))
            switch = switches[(i, j)] = self.addSwitch(('s' + loc), dpid=('%x' % dpid))
            for k in range(0, n):
                host = hosts[(i, j, k)] = self.addHost(genHostName(loc, (k + 1)))
                self.addLink(host, switch)
    for i in range(0, x):
        for j in range(0, y):
            sw1 = switches[(i, j)]
            sw2 = switches[(i, ((j + 1) % y))]
            sw3 = switches[(((i + 1) % x), j)]
            self.addLink(sw1, sw2)
            self.addLink(sw1, sw3)
