home *** CD-ROM | disk | FTP | other *** search
- (********************************************)
- (* *)
- (* Unit MMatrix *)
- (* *)
- (* *)
- (* Skelenten implementation of a matrix *)
- (* unit dynamic allocation of memory. *)
- (* *)
- (********************************************)
-
- unit MM_MEM;
- {$N+,E+}
-
- interface
-
- type
-
- PMCell = ^TMCell;
- TMCell = integer;
-
- Pmatrix = ^Tmatrix;
- Tmatrix = record
- DimRow ,DimCol: byte;
- buf: PMCell;
- end;
-
- const
-
- TMCellSize = SizeOf(TMCell);
-
- procedure MStoreVal(var M: PMatrix; R, C: byte; InVal: TMCell);
- procedure MGetVal(M: PMatrix; R, C: byte; var OutVal: TMCell);
- procedure MIncVal(M: PMatrix; R, C: byte; IncVal: TMCell);
- procedure MExchangeRow(var M: PMatrix; R1, R2: byte);
-
- procedure NewMatrix(var NewM: PMatrix; InitDimRow, InitDimCol: byte);
- procedure DelMatrix(var M: PMatrix);
- procedure MInsert(var M1: PMatrix; M2: PMatrix; R: byte);
- procedure CopyMatrix(M: PMatrix; var Result: PMatrix);
- procedure SubMatrix(M: PMatrix; StartRow, StartCol:
- byte; SubDimRow, SubDimCol: byte;
- var Result: PMatrix);
-
- function MBufSize(M: PMatrix): Integer;
- function GetPtr(M: PMatrix; R, C: byte): PMCell;
-
- implementation
-
- function GetPtr(M: PMatrix; R, C: byte): PMCell;
- begin
- GetPtr := Ptr(Seg(M^.buf^), (((R-1 ) * M^.DimCol) + C-1 )*TMCellSize+Ofs(M^.buf^));
- end;
-
- function MBufSize(M: PMatrix): Integer;
- begin
- MBufSize := M^.DimRow * M^.DimCol * SizeOf(TMCell);
- end;
-
- procedure MStoreVal(var M: PMatrix; R, C: byte; InVal: TMCell);
- var
- P: PMCell;
- begin
- P := GetPtr(M, R, C);
- P^ := InVal;
- end;
-
- procedure MGetVal(M: PMatrix; R, C: byte; var OutVal: TMCell);
- var
- P: PMCell;
- begin
- P := GetPtr(M, R, C);
- OutVal := P^;
- end;
-
- procedure MIncVal(M: PMatrix; R, C: byte; IncVal: TMCell);
- var
- P: PMCell;
- begin
- P := GetPtr(M, R, C);
- P^ := P^ + IncVal;
- end;
-
- procedure MExchangeRow(var M: PMatrix; R1, R2: byte);
- var
- TempP: PMCell;
- ColLen: Word;
- begin
- ColLen := M^.DimCol*TMCellSize;
- GetMem(TempP, ColLen);
- Move(GetPtr(M, R1, 1)^, TempP^, ColLen);
- Move(GetPtr(M, R2, 1)^, GetPtr(M, R1, 1)^, ColLen);
- Move(TempP^, GetPtr(M, R2, 1)^, ColLen);
- FreeMem(TempP, ColLen);
- end;
-
- { insert M2 into M1; starting at row R }
- procedure MInsert(var M1: PMatrix; M2: PMatrix; R: byte);
- var
- I: Integer;
- NewBuf: PMCell;
-
- function NewBufPtr(R, C: Byte): PMCell;
- begin
- NewBufPtr := PMCell(Ptr(Seg(NewBuf^), Ofs(NewBuf^)+((R-1)*M1^.DimCol+(C-1))*TMCellSize));
- end;
-
- begin
- if M1^.DimCol <> M2^.DimCol then
- Exit;
-
- GetMem(NewBuf, MBufSize(M1)+MBufSize(M2));
- I := (R-1)*M1^.DimCol*TMCellSize; { size of top half of M1 }
-
- Move(M1^.Buf^, NewBuf^, I);
- Move(GetPtr(M1,R,1)^, NewBufPtr(R+M2^.DimRow,1)^, MBufSize(M1)-I);
- Move(GetPtr(M2,1,1)^, NewBufPtr(R,1)^, MBufSize(M2));
-
- FreeMem(M1^.buf, MBufSize(M1));
- M1^.buf := NewBuf;
- M1^.DimRow := M1^.DimRow+M2^.DimRow;
- end;
-
- procedure CopyMatrix(M: PMatrix; var Result: PMatrix);
- begin
- DelMatrix(Result);
- NewMatrix(Result, M^.DimRow, M^.DimCol);
- Move(M^.Buf^, Result^.Buf^, MBufSize(M));
- end;
-
- procedure SubMatrix(M: PMatrix; StartRow, StartCol:
- byte; SubDimRow, SubDimCol: byte;
- var Result: PMatrix);
- var
- i: byte;
- begin
- DelMatrix(Result);
- NewMatrix(Result, SubDimRow, SubDimCol);
- for i := 1 to SubDimRow do
- Move(GetPtr(M, StartRow + i - 1, StartCol)^, GetPtr(Result, i, 1)^, SubDimCol*TMCellSize);
- end;
-
- procedure NewMatrix(var NewM: PMatrix; InitDimRow, InitDimCol: byte);
- var
- MSize: Word;
- begin
-
- NewM := New(Pmatrix);
- NewM^.DimRow := InitDimRow;
- NewM^.DimCol := InitDimCol;
- MSize := MBufSize(NewM);
- GetMem(NewM^.buf, MSize);
-
- { Init }
- FillChar(NewM^.buf^, MSize, $0);
- end;
-
- procedure DelMatrix(var M: PMatrix);
- begin
- if M<>nil then
- begin
- if M^.buf <> nil then
- FreeMem(M^.buf, M^.DimRow * M^.DimCol * TMCellSize);
- Dispose(M);
- end;
- end;
-
- end. {unit}