Created
March 16, 2014 17:37
-
-
Save luthfianto/9586944 to your computer and use it in GitHub Desktop.
A bad implementation of intersection via skip pointers from Chapter 2 of "Introduction to Information Retrieval"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from math import sqrt, ceil | |
class SkipList: | |
def __init__(self, list, i=0): | |
self.list=list | |
self.len=len(self.list) | |
self.interval=ceil(sqrt(self.len)) | |
self.i=i | |
def next(self): | |
try: | |
self.i+=1 | |
return self.list[self.i] | |
except: pass | |
def hasSkip(self): | |
return self.i+self.interval < self.len and self.i%self.interval==0 | |
def isNotNil(self): | |
return self.i<self.len | |
def skip(self): | |
return SkipList(self.list, self.i+self.interval) | |
def doSkip(self): | |
self.i+=self.interval | |
def docID(self): | |
return self.list[self.i] | |
def INTERSECTWITHSKIPS(A, B): | |
intersections=[] | |
A_gagal=[] | |
B_gagal=[] | |
A_sukses=[] | |
B_sukses=[] | |
while A.isNotNil() and B.isNotNil() : | |
if A.docID() == B.docID() : | |
intersections.append( A.docID() ) | |
if A.hasSkip(): | |
A_gagal.append( A.docID() ) | |
if B.hasSkip(): | |
B_gagal.append( B.docID() ) | |
A.next() | |
B.next() | |
else: | |
if A.docID() < B.docID() : | |
if A.hasSkip(): | |
if A.skip().docID()<=B.docID() : | |
A_sukses.append( A.docID() ) | |
while A.hasSkip() and A.skip().docID() <= B.docID(): | |
A.doSkip() | |
else: | |
A_gagal.append( A.docID() ) | |
A.next() | |
else: | |
A.next() | |
else: | |
if B.hasSkip(): | |
if B.skip().docID()<=A.docID() : | |
B_sukses.append( B.docID() ) | |
while B.hasSkip() and B.skip().docID() <= B.docID() : | |
B.doSkip() | |
else: | |
B_gagal.append( B.docID() ) | |
B.next() | |
else: | |
B.next() | |
print("A failed skips :" + str(A_gagal) ) | |
print("A successful skips:" + str(A_sukses) ) | |
print("B failed skips :" + str(B_gagal) ) | |
print("B successful skips:" + str(B_sukses) ) | |
return intersections | |
A = SkipList( [2,4,8,16,19,23,28,43] ) | |
B = SkipList( [1,2,3, 5, 8,41,51,60,71] ) | |
print( "\nIntersections :" + str(INTERSECTWITHSKIPS(A,B) ) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment