Jump to content
IGNORED

Set managment in MLC, new instructions


moulinaie

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
310 CALL LINK("ERATHO",C)
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.

post-31118-0-99701700-1324283575_thumb.jpg

  • Like 1
Link to comment
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:

http://gtello.pagesperso-orange.fr/mlc_f.htm (français)

http://gtello.pagesperso-orange.fr/mlc_e.htm (english)

 

For the PreCompiler with on line documentation:

http://gtello.pagesperso-orange.fr/precompiler.htm (Fra & Eng)

 

Guillaume.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...