#! /usr/bin/python2.3 from pprint import pprint import marshal class MatrixLookup: def __init__(self, matrix=None): if matrix is None: matrix = [] self._matrix = matrix self._keyIndexLookup = {} self._valueIndexLookup = {} # reverse dicts of the above. self._indexKeyLookup = {} self._indexValueLookup = {} self._rowIndex = 0 self._colIndex = 0 def _nextRowIndex(self): idx = self._rowIndex self._rowIndex += 1 return idx def _nextColIndex(self): idx = self._colIndex self._colIndex += 1 return idx def _matrixInsert(self, rowIndex, key, colIndex, value): rowLen = len(self._matrix) try: colLen = len(self._matrix[0]) except IndexError: colLen = 0 newRowIndex = -1 if rowLen-1 < rowIndex: self._matrix.append( [0] * (colLen+1) ) newRowIndex = rowIndex if colLen-1 < colIndex: idx = 0 for row in self._matrix: if idx != newRowIndex: row.append(0) idx += 1 self._matrix[rowIndex][colIndex] += 1 pprint(self._matrix) def _keyIndex(self, key): try: return self._keyIndexLookup[key] except IndexError: return None def _valueIndex(self, value): try: return self._valueIndexLookup[value] except IndexError: return None def occurance(self, key, value): rowIndex = self._keyIndex(key) colIndex = self._valueIndex(value) if rowIndex is None or colIndex is None: return None return self._matrix[rowIndex][colIndex] def valuesByKey(self, key): rowIndex = self._keyIndex(key) row = self._matrix[rowIndex] idx = 0 lstValues = [] for occur in row: if occur > 0: lstValues.append(self._indexValueLookup[idx]) idx += 1 return lstValues def keysByValue(self, value): colIndex = self._valueIndex(value) idx = 0 lstKeys = [] for row in self._matrix: if row[colIndex] > 0: lstKeys.append(self._indexKeyLookup[idx]) idx += 1 return lstKeys def keys(self): return self._keyIndexLookup.keys() def values(self): return self._valueIndexLookup.keys() def insert(self, key, value): rowIndex = None if not self._keyIndexLookup.has_key(key): rowIndex = self._nextRowIndex() self._keyIndexLookup[key] = rowIndex self._indexKeyLookup[rowIndex] = key else: rowIndex = self._keyIndexLookup[key] self._indexKeyLookup[rowIndex] = key colIndex = None if not self._valueIndexLookup.has_key(value): colIndex = self._nextColIndex() self._valueIndexLookup[value] = colIndex self._indexValueLookup[colIndex] = value else: colIndex = self._valueIndexLookup[value] self._indexValueLookup[colIndex] = value self._matrixInsert(rowIndex, key, colIndex, value) if __name__ == '__main__': ml = MatrixLookup() if 0: ml.insert("w0", "p0") ml.insert("w1", "p2") ml.insert("w2", "p3") ml.insert("w3", "p4") ml.insert("w3", "p4") ml.insert("w3", "p4") ml.insert("w0", "p2") print ml.valuesByKey('w3') print ml.keysByValue('p2') print ml.keys() print ml.values() print ml.occurance('w3', 'p4') if 1: dicf = open('../sites/gnustep/tmpnAqrUd.info') d = marshal.load(dicf) dicf.close() pprint(d) # for key in d: # value = d[key] # print key # print value # if type(value) == type([]): # for item in value: # ml.insert(key, item) # else: # ml.insert(key, value) # fd = open('table', 'w') # marshal.dump(ml._matrix, fd) # fd.close() # dicf.close()