Beispiel #1
0
class SegmentTreeGenerator(object):
    """
    Returns random elements with a probability proportional to the frequency
    distribution of each element in the population in O(log(N)) time. Uses up
    to 2N space but Supports O(log(N)) weight updates.
    """
    def __init__(self, population):
        """
        :type population: Dict[T, double]
        """
        data = list(
            map(lambda value: (value, float(population[value])),
                population.keys()))
        self._range_map = RangeMap(data)

    def random(self):
        """
        Returns an element from the original population with probability
        proportional to its relative frequency. O(log(N)) time.

        :rtype: T
        """
        i = random.random() * self._range_map.range()
        return self._range_map.get(i)

    def update(self, value, weight):
        """
        Updates the weight of the value in the population. O(log(N)) time.
        """
        self._range_map.update(value, weight)
Beispiel #2
0
class SegmentTreeGenerator(object):
    """
    Returns random elements with a probability proportional to the frequency
    distribution of each element in the population in O(log(N)) time. Uses up
    to 2N space but Supports O(log(N)) weight updates.
    """

    def __init__(self, population):
        """
        :type population: Dict[T, double]
        """
        data = list(map(lambda value: (value, float(population[value])),
                        population.keys()))
        self._range_map = RangeMap(data)

    def random(self):
        """
        Returns an element from the original population with probability
        proportional to its relative frequency. O(log(N)) time.

        :rtype: T
        """
        i = random.random() * self._range_map.range()
        return self._range_map.get(i)

    def update(self, value, weight):
        """
        Updates the weight of the value in the population. O(log(N)) time.
        """
        self._range_map.update(value, weight)
Beispiel #3
0
 def __init__(self, population):
     """
     :type population: Dict[T, double]
     """
     data = list(
         map(lambda value: (value, float(population[value])),
             population.keys()))
     self._range_map = RangeMap(data)
def test(test_case):
    rm = RangeMap(DATA[test_case])

    print("Testing query...")
    for (point, expect) in QUERIES[test_case]:
        assert expect == rm.get(point)

    print("Testing updates...")
    for (value, interval, point, expect) in UPDATES[test_case]:
        rm.update(value, interval)
        assert expect == rm.get(point)

    print("Test case %d passes!" % (test_case + 1))
Beispiel #5
0
 def __init__(self, population):
     """
     :type population: Dict[T, double]
     """
     data = list(map(lambda value: (value, float(population[value])),
                     population.keys()))
     self._range_map = RangeMap(data)
Beispiel #6
0
def test(test_case):
    rm = RangeMap(DATA[test_case])

    print('Testing query...')
    for (point, expect) in QUERIES[test_case]:
        assert expect == rm.get(point)

    print('Testing updates...')
    for (value, interval, point, expect) in UPDATES[test_case]:
        rm.update(value, interval)
        assert expect == rm.get(point)

    print('Test case %d passes!' % (test_case + 1))