IGNORED

# Set managment in MLC, new instructions

## Recommended Posts

Hi,

I have added instructions to manage sets in MLC and the PreCompiler.

A set is an interval of integers, for example

`DIMSET 5 160 A`

defines A as a set that can contain 156 numbers from 5 to 160.

Each element is stored in a single bit, so that is the main advantage: you can manage large sets without taking much memory.

```ELEMENT+ A 12	; add element 12 in set A
ELEMENT- A 100	; remove element 100 from set A
ELEMENT? A 12	; test if 12 is in set A
IF=
; here actions if element is in set
ELSE
; here if it is not in the set
ENDIF
```

As you can see, adding, removing and testing elements is easy, you don't have to manage yourself the bit level.

```SETFILL A	; fills A, all elements present
SETCLEAR A	; clear A all elements removed
SETCARD A B	; returns in B the cardinal of A (number of elements present)
SETCOMP A	; turns A into its complement
SETFINDNEXT A B ; search in A the next element present starting from B, and returns it in B
IF=
; here actions if an element was found
ELSE
; here if no more element was found
ENDIF
```

If you have several sets you can do unions, intersections, differences

```SET+ A B	; A = A union B
SET* A B	; A = A inter B
SET- A B	; A = A - B
```

As an example, I have written the program to search for prime numbers from 2 to 32767. (this set is only 4KB long to manage 32766 numbers).

It takes only 22 seconds to find the 3512 prime numbers.

```100 CALL CLEAR::DIM A\$(48)
\$MLC F 110 10 3000
320 PRINT C;" prime numbers"::PRINT "from 2 to 32767."
330 END
\$ERATHO
HIGHMEM 0
DIMSET 2 32767 A		; a is a set with elements from 2 to 32767
SETFILL A			   ; all elements in A
LET B 2				 ; first element to find
DO
SETFINDNEXT A B	 ; start looking for an element in A starting from B
WHILE=				  ; if found, then B=element number, and B is PRIME
LET C B			 ; take the prime number
DO
ADD C B		 ; computes 2*B, 3*B, 4*B, etc...
WHILE>			  ; when it's over 32767, it is considered as negative.
ELEMENT- A C	; and remove this list from A
LOOP
INC B			   ; see if there is another prime number after B
LOOP
SETCARD A C			 ; computes cardinal of set A
PUTPARAM 1 C			; and return the cardinal
\$\$
\$END
```

Guillaume.

##### Share on other sites

The idea of using bit sets is very cool. Like it!

##### Share on other sites

The idea of using bit sets is very cool. Like it!

Thanks! I think this is important on a machine with precious free bytes !!

After a problem on my WEB page, everything has been now updated:

For MLC with its documentation:

For the PreCompiler with on line documentation:

Guillaume.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.