Forum > General

Sets Vs Arrays. When to use and why.

(1/5) > >>

OC DelGuy:
I looked on YouTube, the FPC Wiki and Tutorials Point and there's not much on the subject.  Is there a video, website or PDF that compares the two and gives examples on when to use one over the other?

440bx:

--- Quote from: OC DelGuy on March 07, 2025, 10:08:07 pm ---Is there a video, website or PDF that compares the two and gives examples on when to use one over the other?

--- End quote ---
I am not aware of one of those but, here are a few "guidelines" for you.

Use a set when:

1. the order in which the elements occur does not matter (because sets don't keep track of element order.)
2. there are no more than 256 elements in the set (this is a current limitation which may change in the future.)
3. the first element has an ordinal of zero and the last an ordinal of 255

Use an array when:

1. the order of elements matter, e.g, country flag colors.
2. there can be more than 256 elements, e.g, the collection of employees that went to work on a particular day in a multinational company.
3. a collection of elements has indexes that straddles the 255 limit, e.g, a number range such as 4,000,000..4,000,100 (commas present for legibility only) This last restriction could also be removed but I don't believe removing it has even been considered.

Off the top of my head, those are the main reasons.

HTH.

korba812:
Another difference is that properties of the set type can be published.

n7800:
Also, in sets, each element can be specified only once. It is either present or absent. In essence, it is a set of flags that replaces several individual boolean variables. That is, they are interchangeable:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  TSet = (enum1, enum2, enum3);var  FSet: TSet; { or } var  FEnum1: boolean;  FEnum2: boolean;  FEnum3: boolean; 
By the way, sets usually take up less memory than individual boolean variables. Their flags are stored in individual bits, while each variable requires at least one byte (in fact, much more due to memory alignment).

n7800:

--- Quote from: 440bx on March 07, 2025, 10:29:28 pm ---3. the first element has an ordinal of zero and the last an ordinal of 255

--- End quote ---

This can be controlled, for example, like this (see docs):


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  sign = (negative = -1, none = 0, positive = 1); 
But it is almost never necessary.

EDITED: Link added.

Navigation

[0] Message Index

[#] Next page

Go to full version