Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP Pascal/HP-UX Programmer's Guide > Chapter 5 Allocation and Alignment

Sets

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Glossary

 » Index

The HP Pascal packing algorithm allocates sets in units called set chunks. Set chunk size depends on the number of bits required to represent the set and whether the set is unpacked, packed, or crunched.

The number of bits required to represent the set is determined by the formula:

bits_required_for_set = ord(largest_value_in_set) -
ord(smallest_value_in_set) + 1

Table 5-13 “How Set Chunk Size Is Determined (HP Pascal Packing Algorithm) ” shows how the HP Pascal packing algorithm determines set chunk size.

Table 5-13 How Set Chunk Size Is Determined (HP Pascal Packing Algorithm)

Number of Bits Required To Represent Set

Set Chunk Size

Set is not PACKED

Set is PACKED

Set is CRUNCHED

1 to 8

32 bits

8 bits

1 bit

9 to 16

32 bits

16 bits

1 bit

17 or more

32 bits

32 bits

1 bit

 

The number of set chunks allocated to a set depends on its type. For the types Boolean, char, enumeration, and integer, the formula for the number of set chunks is:

number_of_set_chunks = ceil(bits_required_for_set/set_chunk_size)

(where ceil(x) means the integer closest to x that is greater than or equal to x).

Table 5-14 “Bit and Set Chunk Requirements for Boolean, Char, and Integer Types (HP Pascal Packing Algorithm) ” gives the values for bits_required_for_set and number_of_set_chunks for Boolean, char, and integer base types. For enumerated sets, bits_required_for_set is the number of elements in the set, and you must use the formula to determine number_of_set_chunks.

Table 5-14 Bit and Set Chunk Requirements for Boolean, Char, and Integer Types (HP Pascal Packing Algorithm)

Base Type

bits_required_for_set

number_of_set_chunks

Boolean

2

1

Char

256

8

Integer

256 (by default) *

8

 

Title not available (Sets )

Same for bit16, bit32, bit52, shortint, and longint.

*

Integers outside the range 0..255 cannot belong to the set.

Example 1

VAR
days = SET OF (sun,mon,tues,wed,thurs,fri,sat);
months = PACKED SET OF (ja,f,mr,ap,ma,jn,jl,au,s,o,n,d);

set_33 = SET OF (e1,e2,e3,e4,e5,e6,e7,e8,e9,e10,e11,
e12,e13,e14,e15,e16,e17,e18,e19,e20,e21,e22,
e23,e24,e25,e26,e27,e28,e29,e30,e31,e32,e33);

p_set_33 = PACKED SET OF (e1,e2,e3,e4,e5,e6,e7,e8,e9,e10,e11,
e12,e13,e14,e15,e16,e17,e18,e19,e20,e21,e22,
e23,e24,e25,e26,e27,e28,e29,e30,e31,e32,e33);

The set days has seven elements and requires seven bits. Its set chunk size is four bytes (32 bits), so days is allocated one set chunk.

Each element is represented by one bit, like this:

Figure 5-21 Sets Example 1

Sets Example 1

The set months has 12 elements and requires 12 bits. Its set chunk size is two bytes, so months is allocated one set chunk (ceil(12/(2*8))=1). Each element is represented by one bit.

Each of the sets set_33 and p_set_33 has 33 elements and requires 33 bits. The set chunk size is four bytes, so set_33 is allocated two set chunks (ceil(33/(4*8))=2). Each element is represented by one bit.

If the type is a subrange, the formula for the number of set chunks is:

number_of_set_chunks = (upper_bound_set_chunk_number -
lower_bound_set_chunk_number) + 1

The upper bound of the subrange determines upper_bound_set_chunk_number, and the lower bound determines lower_bound_set_chunk_number. The formula is:

set_chunk_number = floor(bound/set_chunk_size)

(where floor(x) means the integer closest to x that is less than or equal to x).

Example 2

VAR
s : SET OF -7..18;

The set s is unpacked, so it has a 32-bit set chunk (see Table 5-13 “How Set Chunk Size Is Determined (HP Pascal Packing Algorithm) ”). The upper bound of the subrange is 18, so upper_bound_set_chunk_number is zero (floor(18/32=0)). The lower bound of the subrange is -7, so lower_bound_set_chunk_number is -1 (floor(-7/32)=-1). The set s is allocated two set chunks (0-(-1))+1=2).

Each set element is represented by one bit, like this:

Figure 5-22 Sets Example 2

Sets Example 2

To minimize storage space, avoid base types that are small subranges that overlap set chunk boundaries.

Example 3

VAR
s1 : SET OF 31..32;
s2 : PACKED SET OF 15..16;

The set s1 takes two 32-bit set chunks, using 64 bits to represent a set that requires only two bits. The arithmetic is: (floor(32/32) - floor(31/32)) + 1 = (1-0) + 1 = 2.

The PACKED set s2 takes two 8-bit set chunks, using 16 bits to represent a set that requires only two bits. The arithmetic is: (floor(16/8) - floor(15/8)) + 1 = (2-1)+1 = 2.

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© Hewlett-Packard Development Company, L.P.