?
Solved

Some Basic Array Questions

Posted on 2004-11-16
7
Medium Priority
?
171 Views
Last Modified: 2010-04-05
I have an array with 1000 items.  Every few seconds a new item is stuffed into the array.  I only want to keep the last 1000 items.

Is there a way to simply push a new item into the array, allowing the oldest one to fall off?  Meaning I would have to shift all of them each time a new item is added.  So if we are at 1000 items, then item number 1 gets deleted... number 2 becomes number 1, 3 becomes 2, etc.. all the way to number 1000 which moves to 999 to make room for the new number 1000.



Having asked this, is this the best way to do this?
0
Comment
Question by:Palamedes
  • 2
4 Comments
 
LVL 31

Accepted Solution

by:
moorhouselondon earned 100 total points
ID: 12599811
The best way is to treat the array as a circular buffer.  Have a function which stores the pointer to the current position of the buffer, when it gets to 1000, it wraps around to 1 again.

public
var
  bufptr:integer;


function circinc(i:integer):integer;
begin
if i=1000 then
  i:=1
else
  i:=i+1;
circinc:=i;
end;


somearray[bufptr]:=some new data;
bufptr:=circinc(bufptr);
somearray[bufptr]:=some more new data;

If you want to list out all the items in the array, you work back from the location pointed to by bufptr back to 1, then from 1000 back to bufptr+1.
0
 
LVL 17

Assisted Solution

by:TheRealLoki
TheRealLoki earned 100 total points
ID: 12600228
If you only want to use the first item, then this sounds like a Queue (first in, first out)

uses contnrs;

var
  myqueue: TQueue;
 then you can push an item onto the end, and pop an item from the front.


alternatively. You could use a TList
var mylist: TList;

mylist := TList.create;

mylist.add( anobject )

while mylist.count > 1000 do
begin
    //free your object)
    //then
    mylist.delete(0)
end;

this keeps only 1000 items in the list.
0
 

Expert Comment

by:Drunkard_Englishman
ID: 12620013
could you not use the move procedure to move all the records at once, and then change the last one?

a quick little example:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

const

  ArrLength = 10;

var
  Form1: TForm1;
  Arr: array[0..ArrLength - 1] of Byte;
  ArrPntr: Integer = 0;
  x: Integer = 0;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  if x <> 255 then Inc(x) else x := 0;
  if ArrPntr >= ArrLength then begin
    ArrPntr := ArrLength - 1;
    Move(Arr[1], Arr[0], (ArrLength - 1) * SizeOf(Byte));
  end;
  Arr[ArrPntr] := x;
  inc(ArrPntr);
  Caption := IntToStr(Arr[0]) + ', ' + IntToStr(Arr[1]) + ', ' + IntToStr(Arr[2]) + ', ' + IntToStr(Arr[3]) + ', ' + IntToStr(Arr[4]) + ', ' + IntToStr(Arr[5]) + ', ' + IntToStr(Arr[6]) + ', ' + IntToStr(Arr[7]) + ', ' + IntToStr(Arr[8]) + ', ' + IntToStr(Arr[9]);
end;

end.

this would only work with fixed sized data, so long strings would mess things up...
but it would beat moving through the list and moving things manually
0
 
LVL 31

Expert Comment

by:moorhouselondon
ID: 12622776
As is often the case with these things, there is a trade-off between speed of execution and ease of coding.  The concept of not moving anything, but simply pointing to the head of the array is going to be faster.   The only things that need to be thought about are

(1) what happens when I get to the end of the array? (wrap around to the first item) and
(2) what happens when I look at an array that is not fully populated with 'events' (you initialise the array to start off with)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question