ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Delphi Dataset Page Navigator

Delphi / Object Pascal

Goal -- WPM

Ready
Exercise Algorithm Area
1function CalculatePageInfo(const TotalRecords, PageSize, CurrentPage: Integer): TPageInfo;
2var
3TotalPages: Integer;
4begin
5// Initialize result with default values
6Result.StartRecordIndex := -1; // Indicate invalid state
7Result.TotalPages := 0;
8
9// Handle invalid inputs: negative records or page size
10if (TotalRecords < 0) or (PageSize <= 0) then
11begin
12Exit; // Return default invalid state
13end;
14
15// Calculate total number of pages
16if TotalRecords = 0 then
17begin
18TotalPages := 0;
19end
20else
21begin
22TotalPages := (TotalRecords + PageSize - 1) div PageSize;
23end;
24
25Result.TotalPages := TotalPages;
26
27// Validate current page number
28if (CurrentPage < 1) or (CurrentPage > TotalPages) then
29begin
30// If current page is invalid, return an indicator of no records or last page
31if TotalRecords > 0 then
32Result.StartRecordIndex := (TotalPages - 1) * PageSize
33else
34Result.StartRecordIndex := -1; // No records, so invalid index
35Exit;
36end;
37
38// Calculate the starting record index for the current page
39Result.StartRecordIndex := (CurrentPage - 1) * PageSize;
40end;
41
42// Helper function to get page information
43function GetPageNavigationDetails(const TotalRecords, PageSize, CurrentPage: Integer): TPageInfo;
44begin
45Result := CalculatePageInfo(TotalRecords, PageSize, CurrentPage);
46end;
47
48// Main entry point for demonstration (not part of the core algorithm)
49procedure TForm1.Button1Click(Sender: TObject);
50var
51PageInfo: TPageInfo;
52begin
53PageInfo := GetPageNavigationDetails(100, 10, 3);
54// Use PageInfo.StartRecordIndex and PageInfo.TotalPages
55
56PageInfo := GetPageNavigationDetails(0, 10, 1);
57// Use PageInfo.StartRecordIndex and PageInfo.TotalPages
58
59PageInfo := GetPageNavigationDetails(100, 10, 11);
60// Use PageInfo.StartRecordIndex and PageInfo.TotalPages
61end;
Algorithm description viewbox

Delphi Dataset Page Navigator

Algorithm description:

This Delphi code implements a utility to manage dataset pagination. It calculates the total number of pages required to display a given number of records, based on a specified page size. It also determines the starting index of the first record for a requested page. This is crucial for efficiently loading and displaying large datasets in user interfaces, preventing memory overload and improving responsiveness.

Algorithm explanation:

The `CalculatePageInfo` function takes the total number of records, the desired page size, and the current page number as input. It first handles invalid inputs such as negative record counts or non-positive page sizes, returning a default invalid state. The total number of pages is calculated using integer division with an adjustment to correctly handle cases where the total records are not a perfect multiple of the page size. The core logic `(TotalRecords + PageSize - 1) div PageSize` ensures that even a single remaining record constitutes a full page. It then validates the `CurrentPage` against the calculated `TotalPages`. If the `CurrentPage` is out of bounds, it returns an appropriate `StartRecordIndex` (either the last record's start or -1 if no records exist). Otherwise, it computes the `StartRecordIndex` for the valid `CurrentPage` using `(CurrentPage - 1) * PageSize`. The time complexity is O(1) as it involves a fixed number of arithmetic operations. The space complexity is also O(1) as it uses a fixed amount of memory for variables.

Pseudocode:

FUNCTION CalculatePageInfo(TotalRecords, PageSize, CurrentPage):
  IF TotalRecords < 0 OR PageSize <= 0 THEN
    RETURN {StartRecordIndex: -1, TotalPages: 0}
  END IF

  IF TotalRecords == 0 THEN
    TotalPages = 0
  ELSE
    TotalPages = CEILING(TotalRecords / PageSize)
  END IF

  IF CurrentPage < 1 OR CurrentPage > TotalPages THEN
    IF TotalRecords > 0 THEN
      StartRecordIndex = (TotalPages - 1) * PageSize
    ELSE
      StartRecordIndex = -1
    END IF
  ELSE
    StartRecordIndex = (CurrentPage - 1) * PageSize
  END IF

  RETURN {StartRecordIndex, TotalPages}
END FUNCTION