class IntervalCache: # Tree is the L1 cache tree = IntervalTree() # L2 cache: frequent items frequentItems = {} # L2 eviction cache evictedItems = {} def __init__(self, size, threshold): self.size = size self.threshold = threshold self.l1 = ARC(size, self.evict) def evict(self, node): pass #self.tree.rbTree.remove(node) # TODO: check overlaps to update frequency on overlapping items def add(self, min, max): # Update the L1 cache and the interval tree when an item is added key = str(min) + " " + str(max) if not self.l1.contains(key): #node = self.tree.insert(Interval(min, max)) self.l1.add(key, True) # Check the support if the item should be moved to the L2 cache else: self.l1.add(key, None) if key in self.l1.t2: if self.l1.t2[key][0] > self.threshold: self.frequentItems[key] = time.time()
def main(): if (len(sys.argv)>1): filename = sys.argv[1] else: filename = "test_data.txt" infile = open(filename,'r') for line in infile: (name,start,end) = line.strip().split(' ') print "%s, %s, %s" % (name,start,end) i = IntervalTree() # 1,3,4,5,6,8,10 # use a mix of insert and load # to test the total functionality """ 5 3 8 1 4 6 10 """ i.insert(6) i.insert(3) i.insert(3) i.insert(4) i.load(1) i.load(5) i.load(8) i.load(10) i.unload() print "" i.inorder( debug2 ) # time to test insertions i.add(3,8,"yohann") print "" i.inorder( debug2 ) i.inorder( debug3 )
def main(): """ Function for testing interval tree """ # default filename if (len(sys.argv)>1): filename = sys.argv[1] else: filename = "composers.txt" # open our input text file infile = open(filename,'r') data = [] # process the file for line in infile: (name,start,end) = line.strip().split(' ') print "%s, %s, %s" % (name,start,end) data.append((int(start),int(end),name)) print "" # define interval-tree instance i = IntervalTree() """ # special insert i.insert(1874) i.insert(1779) i.insert(1951) i.insert(1672) i.insert(1828) i.insert(1907) i.insert(1971) i.insert(1585) i.insert(1756) i.insert(1791) i.insert(1843) i.insert(1888) ################ """ # load pts for tup in data: i.load(tup[0]) i.load(tup[1]) # don't forget to "unload" i.unload() # insert objects into interval-tree for tup in data: #s = StringWrapper(tup[2]) #i.add(tup[0],tup[1],s) i.add(tup[0],tup[1],tup[2]) # dump tree contents i.inorder( debug2 ) print "" i.inorder( debug3 ) print "" #while True: #ep = int(raw_input()) #print "date: %d" % ep, ["%s" % obj for obj in i.query_endpoint( ep )] # test endpoint for ep in [1400,1828,1830,1907,1908,1780,1888,1887,1757]: #print "date: %d" % ep, ["%s" % obj for obj.name in i.query_endpoint( ep )] print "date: %d" % ep, ["%s" % obj for obj in i.query_endpoint( ep )]