{$I OVC.INC}

{$B-} {Complete Boolean Evaluation}
{$I+} {Input/Output-Checking}
{$P+} {Open Parameters}
{$T-} {Typed @ Operator}
{$W-} {Windows Stack Frame}
{$X+} {Extended Syntax}

{$IFNDEF Win32}
{$G+} {286 Instructions}
{$N+} {Numeric Coprocessor}

{$C MOVEABLE,DEMANDLOAD,DISCARDABLE}
{$ENDIF}

{*********************************************************}
{*                  OVCSPARY.PAS 2.00                    *}
{*     Copyright (c) TurboPower Software Co 1995-96      *}
{*                 All rights reserved.                  *}
{*********************************************************}

unit OvcSpAry;
  {-Orpheus - sparse array implementation}

interface

uses
  {$IFDEF Win32}
  Windows,
  {$ELSE}
  WinProcs,
  {$ENDIF}
  SysUtils, OvcExcpt, OvcConst;

const
  MaxSparseArrayItems = 320000;  {maximum items in a sparse array}

type
  TSparseArrayFunc = function (Index : longint; Item : pointer;
                               ExtraData : pointer) : boolean;
    {-Sparse array's iterator type. Should return true to continue iterating,
      false otherwise.}

  {The sparse array class}
  TOvcSparseArray = class
    protected {private}
      {.Z+}
      FCount : longint;       {Fake count of the items}
      FArray : pointer;       {Sparse array}

      ChunkCount : word;      {Number of chunks}
      ChunkArraySize : word;  {Size of FArray}

      procedure RecalcCount;
      {.Z-}

    protected
      {.Z+}
      function GetActiveCount : longint;
      function GetItem(Index : longint) : pointer;
      procedure PutItem(Index : longint; Item : pointer);
      {.Z-}

    public
      constructor Create;
      destructor Destroy; override;

      function  Add(Item : pointer) : longint;
        {-Add Item to end of array}
      procedure Clear;
        {-Clear array}
      procedure Delete(Index : longint);
        {-Delete item at Index, all items below move up one}
      procedure Exchange(Index1, Index2 : longint);
        {-Swap the items at Index1 and Index2}
      function  First : pointer;
        {-Return First item}
      function  ForAll(Action : TSparseArrayFunc;
                       Backwards : boolean;
                       ExtraData : pointer) : longint;
        {-Iterate through all active items, maybe backwards}
      function  IndexOf(Item : pointer) : longint;
        {-Get the index of Item}
      procedure Insert(Index : longint; Item : pointer);
        {-Insert Item at Index, it and all items below move down one}
      function  Last : pointer;
        {-Return Last item}
      procedure Squeeze;
        {-Pack the sparse array}

      property Count : longint
        {-Logical count of the number of items (=IndexOf(Last)+1)}
         read FCount;
      property ActiveCount : longint
        {-Count of non-nil items}
         read GetActiveCount;
      property Items[Index : longint] : pointer
        {-Items array}
         read GetItem write PutItem;
         default;
  end;

implementation
