From 6d5e081dfbf3f7b8a1ff3a9d79ab44258f6be605 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucio=20Andr=C3=A9s=20Illanes=20Albornoz?= Date: Tue, 30 Jan 2018 11:17:35 +0100 Subject: [PATCH] Reduce memory usage by folding nested patch {coordinate,colour} list(s). --- MiRCARTCanvas.py | 20 +++++++++---------- MiRCARTCanvasBackend.py | 38 ++++++++++++++++++------------------- MiRCARTCanvasImportStore.py | 6 +++--- MiRCARTToolCircle.py | 6 +++--- MiRCARTToolFill.py | 8 ++++---- MiRCARTToolLine.py | 8 ++++---- MiRCARTToolRect.py | 2 +- MiRCARTToolSelect.py | 10 +++++----- MiRCARTToolSelectClone.py | 2 +- MiRCARTToolSelectMove.py | 6 +++--- MiRCARTToolText.py | 4 ++-- 11 files changed, 55 insertions(+), 55 deletions(-) diff --git a/MiRCARTCanvas.py b/MiRCARTCanvas.py index b75af72..a66cc43 100644 --- a/MiRCARTCanvas.py +++ b/MiRCARTCanvas.py @@ -41,7 +41,7 @@ class MiRCARTCanvas(wx.Panel): # {{{ _commitPatch(self, patch): XXX def _commitPatch(self, patch): - self.canvasMap[patch[0][1]][patch[0][0]] = patch[1:] + self.canvasMap[patch[1]][patch[0]] = patch[2:] # }}} # {{{ _dispatchDeltaPatches(self, deltaPatches): XXX def _dispatchDeltaPatches(self, deltaPatches): @@ -58,8 +58,8 @@ class MiRCARTCanvas(wx.Panel): self.canvasJournal, eventDc) self._canvasDirtyCursor = True if self.canvasBackend.drawPatch(eventDc, patch): - patchDeltaCell = self.canvasMap[patch[0][1]][patch[0][0]] - patchDelta = [list(patch[0]), *patchDeltaCell.copy()] + patchDeltaCell = self.canvasMap[patch[1]][patch[0]] + patchDelta = [*patch[0:2], *patchDeltaCell] if isCursor: self.canvasJournal.pushCursor(patchDelta) else: @@ -127,10 +127,10 @@ class MiRCARTCanvas(wx.Panel): and numRow < len(newCanvas) \ and numCol < len(newCanvas[numRow]): self._commitPatch([ \ - [numCol, numRow], *newCanvas[numRow][numCol]]) + numCol, numRow, *newCanvas[numRow][numCol]]) self.canvasBackend.drawPatch(eventDc, \ - ([numCol, numRow], \ - *self.canvasMap[numRow][numCol])) + [numCol, numRow, \ + *self.canvasMap[numRow][numCol]]) wx.SafeYield() # }}} # {{{ resize(self, newCanvasSize): XXX @@ -160,20 +160,20 @@ class MiRCARTCanvas(wx.Panel): else: for numRow in range(oldCanvasSize[1]): self.canvasMap[numRow].extend( \ - [[[1, 1], 0, " "]] * deltaCanvasSize[0]) + [[1, 1, 0, " "]] * deltaCanvasSize[0]) for numNewCol in range(oldCanvasSize[0], newCanvasSize[0]): self.canvasBackend.drawPatch( \ - eventDc, [[numNewCol, numRow], \ + eventDc, [numNewCol, numRow, \ *self.canvasMap[numRow][-1]]) if deltaCanvasSize[1] < 0: del self.canvasMap[-1:(deltaCanvasSize[1]-1):-1] else: for numNewRow in range(oldCanvasSize[1], newCanvasSize[1]): self.canvasMap.extend( \ - [[[[1, 1], 0, " "]] * newCanvasSize[0]]) + [[[1, 1, 0, " "]] * newCanvasSize[0]]) for numNewCol in range(newCanvasSize[0]): self.canvasBackend.drawPatch( \ - eventDc, [[numNewCol, numNewRow], \ + eventDc, [numNewCol, numNewRow, \ *self.canvasMap[-1][-1]]) self.canvasSize = newCanvasSize diff --git a/MiRCARTCanvasBackend.py b/MiRCARTCanvasBackend.py index f7b20e2..9cf918f 100644 --- a/MiRCARTCanvasBackend.py +++ b/MiRCARTCanvasBackend.py @@ -33,27 +33,27 @@ class MiRCARTCanvasBackend(): # {{{ _drawBrushPatch(self, eventDc, patch): XXX def _drawBrushPatch(self, eventDc, patch): - absPoint = self._xlatePoint(patch[0]) - brushFg = self._brushes[patch[1][1]] - brushBg = self._brushes[patch[1][0]] - pen = self._pens[patch[1][1]] + absPoint = self._xlatePoint(patch) + brushFg = self._brushes[patch[3]] + brushBg = self._brushes[patch[2]] + pen = self._pens[patch[3]] self._setBrushDc(brushBg, brushFg, eventDc, pen) eventDc.DrawRectangle(*absPoint, *self.cellSize) # }}} # {{{ _drawCharPatch(self, eventDc, patch): XXX def _drawCharPatch(self, eventDc, patch): - absPoint = self._xlatePoint(patch[0]) - brushFg = self._brushes[patch[1][0]] - brushBg = self._brushes[patch[1][1]] - pen = self._pens[patch[1][1]] + absPoint = self._xlatePoint(patch) + brushFg = self._brushes[patch[2]] + brushBg = self._brushes[patch[3]] + pen = self._pens[patch[3]] fontBitmap = wx.Bitmap(*self.cellSize) fontDc = wx.MemoryDC(); fontDc.SelectObject(fontBitmap); - fontDc.SetTextForeground(wx.Colour(MiRCARTColours[patch[1][0]][0:4])) - fontDc.SetTextBackground(wx.Colour(MiRCARTColours[patch[1][1]][0:4])) + fontDc.SetTextForeground(wx.Colour(MiRCARTColours[patch[2]][0:4])) + fontDc.SetTextBackground(wx.Colour(MiRCARTColours[patch[3]][0:4])) fontDc.SetBrush(brushBg); fontDc.SetBackground(brushBg); fontDc.SetPen(pen); fontDc.SetFont(self._font) fontDc.DrawRectangle(0, 0, *self.cellSize) - fontDc.DrawText(patch[3], 0, 0) + fontDc.DrawText(patch[5], 0, 0) eventDc.Blit(*absPoint, *self.cellSize, fontDc, 0, 0) # }}} # {{{ _finiBrushesAndPens(self): XXX @@ -89,18 +89,18 @@ class MiRCARTCanvasBackend(): dc.SetPen(pen) self._lastPen = pen # }}} - # {{{ _xlatePoint(self, relMapPoint): XXX - def _xlatePoint(self, relMapPoint): - return [a*b for a,b in zip(relMapPoint, self.cellSize)] + # {{{ _xlatePoint(self, patch): XXX + def _xlatePoint(self, patch): + return [a*b for a,b in zip(patch[0:2], self.cellSize)] # }}} # {{{ drawPatch(self, eventDc, patch): XXX def drawPatch(self, eventDc, patch): - if patch[0][0] < self.canvasSize[0] \ - and patch[0][0] >= 0 \ - and patch[0][1] < self.canvasSize[1] \ - and patch[0][1] >= 0: - if patch[3] == " ": + if patch[0] < self.canvasSize[0] \ + and patch[0] >= 0 \ + and patch[1] < self.canvasSize[1] \ + and patch[1] >= 0: + if patch[5] == " ": self._drawBrushPatch(eventDc, patch) else: self._drawCharPatch(eventDc, patch) diff --git a/MiRCARTCanvasImportStore.py b/MiRCARTCanvasImportStore.py index e35b4a9..2714e0f 100644 --- a/MiRCARTCanvasImportStore.py +++ b/MiRCARTCanvasImportStore.py @@ -105,7 +105,7 @@ class MiRCARTCanvasImportStore(): inCellState, self._CellState.CS_UNDERLINE) else: inRowCols += 1 - outMap[inCurRow].append((inCurColours, inCellState, inChar)) + outMap[inCurRow].append([*inCurColours, inCellState, inChar]) elif inParseState == self._ParseState.PS_COLOUR_DIGIT0 \ or inParseState == self._ParseState.PS_COLOUR_DIGIT1: if inChar == "," \ @@ -146,8 +146,8 @@ class MiRCARTCanvasImportStore(): # }}} # {{{ importNew(self, newCanvasSize=None): XXX def importNew(self, newCanvasSize=None): - newMap = [[[[1, 1], 0, " "] \ - for x in range(newCanvasSize[0])] \ + newMap = [[[1, 1, 0, " "] \ + for x in range(newCanvasSize[0])] \ for y in range(newCanvasSize[1])] self.parentCanvas.onStoreUpdate(newCanvasSize, newMap) # }}} diff --git a/MiRCARTToolCircle.py b/MiRCARTToolCircle.py index 0fc5fb7..9249b69 100644 --- a/MiRCARTToolCircle.py +++ b/MiRCARTToolCircle.py @@ -45,9 +45,9 @@ class MiRCARTToolCircle(MiRCARTTool): for brushX in range(-radius, radius + 1): if ((brushX**2)+(brushY**2) < (((radius**2)+radius)*0.8)): patch = [ \ - [atPoint[0] + int(originPoint[0]+brushX), \ - atPoint[1] + int(originPoint[1]+brushY)], \ - brushColours, 0, " "] + atPoint[0] + int(originPoint[0]+brushX), \ + atPoint[1] + int(originPoint[1]+brushY), \ + *brushColours, 0, " "] if isLeftDown or isRightDown: dispatchFn(eventDc, False, patch); dispatchFn(eventDc, True, patch); else: diff --git a/MiRCARTToolFill.py b/MiRCARTToolFill.py index c1adc8c..00675e0 100644 --- a/MiRCARTToolFill.py +++ b/MiRCARTToolFill.py @@ -32,17 +32,17 @@ class MiRCARTToolFill(MiRCARTTool): # onMouseEvent(self, event, atPoint, brushColours, brushSize, isDragging, isLeftDown, isRightDown, dispatchFn, eventDc): XXX def onMouseEvent(self, event, atPoint, brushColours, brushSize, isDragging, isLeftDown, isRightDown, dispatchFn, eventDc): pointStack = [list(atPoint)]; pointsDone = []; - testColour = self.parentCanvas.canvasMap[atPoint[1]][atPoint[0]][0][1] + testColour = self.parentCanvas.canvasMap[atPoint[1]][atPoint[0]][0:2] if isLeftDown or isRightDown: if isRightDown: brushColours = [brushColours[1], brushColours[0]] while len(pointStack) > 0: point = pointStack.pop() pointCell = self.parentCanvas.canvasMap[point[1]][point[0]] - if pointCell[0][1] == testColour: + if pointCell[0:2] == testColour: if not point in pointsDone: - dispatchFn(eventDc, False, [point.copy(), \ - [brushColours[0], brushColours[0]], 0, " "]) + dispatchFn(eventDc, False, [*point, \ + brushColours[0], brushColours[0], 0, " "]) if point[0] > 0: pointStack.append([point[0] - 1, point[1]]) if point[0] < (self.parentCanvas.canvasSize[0] - 1): diff --git a/MiRCARTToolLine.py b/MiRCARTToolLine.py index fe14df7..d70143e 100644 --- a/MiRCARTToolLine.py +++ b/MiRCARTToolLine.py @@ -55,10 +55,10 @@ class MiRCARTToolLine(MiRCARTTool): lineD = 2 * pointDelta[1] - pointDelta[0]; lineY = 0; for lineX in range(pointDelta[0] + 1): for brushStep in range(brushSize[0]): - patch = [[ \ + patch = [ \ originPoint[0] + lineX*lineXX + lineY*lineYX + brushStep, \ - originPoint[1] + lineX*lineXY + lineY*lineYY], \ - brushColours, 0, " "] + originPoint[1] + lineX*lineXY + lineY*lineYY, \ + *brushColours, 0, " "] if isCursor: dispatchFn(eventDc, False, patch); dispatchFn(eventDc, True, patch); else: @@ -83,7 +83,7 @@ class MiRCARTToolLine(MiRCARTTool): self.toolColours = brushColours self.toolOriginPoint = list(atPoint) self.toolState = self.TS_ORIGIN - dispatchFn(eventDc, True, [atPoint, brushColours, 0, " "]) + dispatchFn(eventDc, True, [*atPoint, *brushColours, 0, " "]) elif self.toolState == self.TS_ORIGIN: targetPoint = list(atPoint) originPoint = self.toolOriginPoint diff --git a/MiRCARTToolRect.py b/MiRCARTToolRect.py index d4250ba..66c0094 100644 --- a/MiRCARTToolRect.py +++ b/MiRCARTToolRect.py @@ -43,7 +43,7 @@ class MiRCARTToolRect(MiRCARTTool): brushSize[0] *= 2 for brushRow in range(brushSize[1]): for brushCol in range(brushSize[0]): - patch = [[atPoint[0] + brushCol, atPoint[1] + brushRow],brushColours, 0, " "] + patch = [atPoint[0] + brushCol, atPoint[1] + brushRow, *brushColours, 0, " "] if isLeftDown or isRightDown: dispatchFn(eventDc, False, patch); dispatchFn(eventDc, True, patch); else: diff --git a/MiRCARTToolSelect.py b/MiRCARTToolSelect.py index 68f5539..3084776 100644 --- a/MiRCARTToolSelect.py +++ b/MiRCARTToolSelect.py @@ -53,18 +53,18 @@ class MiRCARTToolSelect(MiRCARTTool): else: curColours = [0, 0] dispatchFn(eventDc, True, \ - [[rectX, rectFrame[0][1]], curColours, 0, " "]) + [rectX, rectFrame[0][1], *curColours, 0, " "]) dispatchFn(eventDc, True, \ - [[rectX, rectFrame[1][1]], curColours, 0, " "]) + [rectX, rectFrame[1][1], *curColours, 0, " "]) for rectY in range(rectFrame[0][1], rectFrame[1][1]+1): if curColours == [0, 0]: curColours = [1, 1] else: curColours = [0, 0] dispatchFn(eventDc, True, \ - [[rectFrame[0][0], rectY], curColours, 0, " "]) + [rectFrame[0][0], rectY, *curColours, 0, " "]) dispatchFn(eventDc, True, \ - [[rectFrame[1][0], rectY], curColours, 0, " "]) + [rectFrame[1][0], rectY, *curColours, 0, " "]) # }}} # @@ -82,7 +82,7 @@ class MiRCARTToolSelect(MiRCARTTool): self.toolState = self.TS_ORIGIN else: dispatchFn(eventDc, True, \ - [list(atPoint), brushColours.copy(), 0, " "]) + [*atPoint, *brushColours, 0, " "]) elif self.toolState == self.TS_ORIGIN: self.toolRect[1] = list(atPoint) if isLeftDown or isRightDown: diff --git a/MiRCARTToolSelectClone.py b/MiRCARTToolSelectClone.py index 14c6024..3d2a5d5 100644 --- a/MiRCARTToolSelectClone.py +++ b/MiRCARTToolSelectClone.py @@ -53,7 +53,7 @@ class MiRCARTToolSelectClone(MiRCARTToolSelect): cellOld = self.toolSelectMap[numRow][numCol] rectY = selectRect[0][1] + numRow rectX = selectRect[0][0] + numCol - dispatchFn(eventDc, isCursor, [[rectX+disp[0], rectY+disp[1]], *cellOld]) + dispatchFn(eventDc, isCursor, [rectX+disp[0], rectY+disp[1], *cellOld]) self._drawSelectRect(newToolRect, dispatchFn, eventDc) self.toolRect = newToolRect diff --git a/MiRCARTToolSelectMove.py b/MiRCARTToolSelectMove.py index 7412062..134826c 100644 --- a/MiRCARTToolSelectMove.py +++ b/MiRCARTToolSelectMove.py @@ -50,14 +50,14 @@ class MiRCARTToolSelectMove(MiRCARTToolSelect): isCursor = True for numRow in range(len(self.toolSelectMap)): for numCol in range(len(self.toolSelectMap[numRow])): - dispatchFn(eventDc, isCursor, [[self.srcRect[0] + numCol, \ - self.srcRect[1] + numRow], [1, 1], 0, " "]) + dispatchFn(eventDc, isCursor, [self.srcRect[0] + numCol, \ + self.srcRect[1] + numRow, 1, 1, 0, " "]) for numRow in range(len(self.toolSelectMap)): for numCol in range(len(self.toolSelectMap[numRow])): cellOld = self.toolSelectMap[numRow][numCol] rectY = selectRect[0][1] + numRow rectX = selectRect[0][0] + numCol - dispatchFn(eventDc, isCursor, [[rectX+disp[0], rectY+disp[1]], *cellOld]) + dispatchFn(eventDc, isCursor, [rectX+disp[0], rectY+disp[1], *cellOld]) self._drawSelectRect(newToolRect, dispatchFn, eventDc) self.toolRect = newToolRect diff --git a/MiRCARTToolText.py b/MiRCARTToolText.py index a3eb686..8026eba 100644 --- a/MiRCARTToolText.py +++ b/MiRCARTToolText.py @@ -42,7 +42,7 @@ class MiRCARTToolText(MiRCARTTool): self.textColours = brushColours.copy() if self.textPos == None: self.textPos = list(atPoint) - dispatchFn(eventDc, False, [self.textPos, self.textColours, 0, keyChar]) + dispatchFn(eventDc, False, [*self.textPos, *self.textColours, 0, keyChar]) if self.textPos[0] < (self.parentCanvas.canvasSize[0] - 1): self.textPos[0] += 1 elif self.textPos[1] < (self.parentCanvas.canvasSize[1] - 1): @@ -65,6 +65,6 @@ class MiRCARTToolText(MiRCARTTool): if self.textColours == None: self.textColours = brushColours.copy() self.textPos = list(atPoint) - dispatchFn(eventDc, True, [self.textPos, self.textColours, 0, "_"]) + dispatchFn(eventDc, True, [*self.textPos, *self.textColours, 0, "_"]) # vim:expandtab foldmethod=marker sw=4 ts=4 tw=120