def union(p1, p2): if p1 is None and p2 is None: return None results = PostingsList() while p1 is not None or p2 is not None: if p1 is None: add_until_end(p2, results) p2 = None elif p2 is None: add_until_end(p1, results) p1 = None else: if p1.entry < p2.entry: results.add(p1.entry) p1 = next_or_none(p1) elif p1.entry == p2.entry: results.add(p1.entry) p1 = next_or_none(p1) p2 = next_or_none(p2) else: results.add(p2.entry) p2 = next_or_none(p2) return results
def new_list(self): if self._mode != 'w': raise Exception("cannot create a new list without 'write' mode") else: new_postings_list = PostingsList() self._postings.append(new_postings_list) return (self._postings[-1], len(self._postings) - 1)
def list_at_offset(self, offset): if self._mode == 'w': if offset < len(self._postings): l = self._postings[offset] l.reset() return l else: raise IndexError("offset out of bounds") else: self._f.seek(offset) line = self._f.readline()[:-1] # strip out newline p_list = PostingsList() p_list.add_from_string(line) if len(p_list._entries) == 0: return None return p_list
def __init__(self, file_name, mode='w'): self._file_name = file_name self._mode = mode if self._mode == 'w': self._postings = [PostingsList()] else: self._f = open(file_name)
def complement(p, c): results = PostingsList() while p is not None: # c cannot overtake p as c is everything, # and both move together when equal if p.entry == c.entry: p = next_or_none(p) c = next_or_none(c) else: results.add(c.entry) c = next_or_none(c) if c is not None: add_until_end(c, results) return results
def intersect(p1, p2): if p1 is None or p2 is None: return None results = PostingsList() while p1 is not None and p2 is not None: if p1.entry == p2.entry: results.add(p1.entry) p1 = next_or_none(p1) p2 = next_or_none(p2) elif p1.entry < p2.entry: p1 = next_or_skip(p1, p2.entry) else: p2 = next_or_skip(p2, p1.entry) return results