123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Text;
- namespace UAS_LabelMachine.PublicMethod
- {
- class DataGridViewVirtualModel
- {
- public interface IDataPageRetriever
- {
- DataTable ApplyPageOfData(int lowPageBoundary, int rowsPerPage);
- }
- internal class DataRetriever : IDataPageRetriever
- {
- private DataColumnCollection columns;
- private string commaSeparatedColumnNames;
- public DataTable ApplyPageOfData(int lowPageBoundary, int rowsPerPage)
- {
- return new DataTable();
- }
- #region IDataPageRetriver
- #endregion
- }
- internal class Cache
- {
- private struct DataPage
- {
- public DataTable table;
- private int lowIndex;
- private int highIndex;
- public DataPage(DataTable table, int rowIndex)
- {
- this.table = table;
- this.lowIndex = MapLowerBoundary(rowIndex);
- this.highIndex = MapUpperBoundary(rowIndex);
- }
- public int LowIndex
- {
- get { return this.lowIndex; }
- }
- public int HighIndex
- {
- get { return this.highIndex; }
- }
- public static int MapLowerBoundary(int rowIndex)
- {
- return (rowIndex / RowPerPage) * RowPerPage;
- }
- public static int MapUpperBoundary(int rowIndex)
- {
- return MapLowerBoundary(rowIndex) + RowPerPage - 1;
- }
- }
- IDataPageRetriever dataSupply;
- static int RowPerPage;
- DataPage[] catchPages = new DataPage[2];
- public Cache(IDataPageRetriever dataSupplier, int rowsPerPage)
- {
- this.dataSupply = dataSupplier;
- RowPerPage = rowsPerPage;
- PreLoadDataPages();
- }
- private void PreLoadDataPages()
- {
- catchPages[0] = new DataPage(dataSupply.ApplyPageOfData(0, RowPerPage), 0);
- catchPages[1] = new DataPage(dataSupply.ApplyPageOfData(RowPerPage, RowPerPage), RowPerPage);
- }
- public string RetrieveElement(int rowIndex, int colIndex)
- {
- string element = "";
- if (IfPageCatched_TheSetElement(rowIndex, colIndex, ref element))
- {
- return element;
- }
- else
- {
- element = RetrieveData_CatchIt_ReturnElement(rowIndex, colIndex);
- }
- return element;
- }
- private bool IfPageCatched_TheSetElement(int rowIndex, int colIndex, ref string element)
- {
- if (IsRowCatchedInPage(0, rowIndex))
- {
- element = catchPages[0].table.Rows[rowIndex % RowPerPage][colIndex].ToString();
- return true;
- }
- else if (IsRowCatchedInPage(1, rowIndex))
- {
- element = catchPages[1].table.Rows[rowIndex % RowPerPage][colIndex].ToString();
- return true;
- }
- return false;
- }
- private string RetrieveData_CatchIt_ReturnElement(int rowIndex, int colIndex)
- {
- DataPage newPage = new DataPage(dataSupply.ApplyPageOfData(DataPage.MapLowerBoundary(rowIndex), RowPerPage), rowIndex);
- //which old datapage should be replaced?
- catchPages[GetIndexOfReplacedPage(rowIndex)] = newPage;
- return RetrieveElement(rowIndex, colIndex);
- }
- private bool IsRowCatchedInPage(int pageNum, int rowIndex)
- {
- return catchPages[pageNum].LowIndex <= rowIndex &&
- catchPages[pageNum].HighIndex >= rowIndex;
- }
- private int GetIndexOfReplacedPage(int rowIndex)
- {
- if (catchPages[0].HighIndex < rowIndex && catchPages[1].HighIndex < rowIndex)
- {
- int offsetFromPage0 = rowIndex - catchPages[0].HighIndex;
- int offsetFromPage1 = rowIndex - catchPages[1].HighIndex;
- if (offsetFromPage0 < offsetFromPage1)
- return 1;
- else
- return 0;
- }
- else
- {
- int offsetFromPage0 = catchPages[0].LowIndex - rowIndex;
- int offsetFromPage1 = catchPages[1].LowIndex - rowIndex;
- if (offsetFromPage0 < offsetFromPage1)
- return 1;
- return 0;
- }
- }
- }
- }
- }
|