Jump to content



0

Set managment in MLC, new instructions


2 replies to this topic

#1 moulinaie OFFLINE  

moulinaie

    Chopper Commander

  • 185 posts
  • Location:France, Burgundy

Posted Mon Dec 19, 2011 2:37 AM

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.

Attached Files



#2 retroclouds OFFLINE  

retroclouds

    Stargunner

  • 1,096 posts
  • Location:Germany

Posted Mon Dec 19, 2011 5:33 AM

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

#3 moulinaie OFFLINE  

moulinaie

    Chopper Commander

  • 185 posts
  • Location:France, Burgundy

Posted Mon Dec 19, 2011 11:43 AM

View Postretroclouds, on Mon Dec 19, 2011 5:33 AM, said:

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.pagesp...ge.fr/mlc_f.htm (français)
http://gtello.pagesp...ge.fr/mlc_e.htm (english)

For the PreCompiler with on line documentation:
http://gtello.pagesp...precompiler.htm (Fra & Eng)

Guillaume.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users