Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS 

Generate all subsets of a set

This code generates all the subsets of {1, 2, ..., n} and prints them.

This also contains a very fast binary counter implementation.

See this for further explanations.

#include <stdio.h>

/* Applies the mask to a set like {1, 2, ..., n} and prints it */
void printv(int mask[], int n) {
	int i;
	printf("{ ");
	for (i = 0; i < n; ++i)
		if (mask[i])
			printf("%d ", i + 1); /*i+1 is part of the subset*/
	printf("\b }\n");
}

/* Generates the next mask*/
int next(int mask[], int n) {
	int i;
	for (i = 0; (i < n) && mask[i]; ++i)
		mask[i] = 0;

	if (i < n) {
		mask[i] = 1;
		return 1;
	}
	return 0;
}

int main(int argc, char *argv[]) {
	int n = 3;

	int mask[16]; /* Guess what this is */
	int i;
	for (i = 0; i < n; ++i)
		mask[i] = 0;

	/* Print the first set */
	printv(mask, n);

	/* Print all the others */
	while (next(mask, n))
		printv(mask, n);

	return 0;
}

Show all icons in avkon.mbm

There are 2 type of mbm files. The following code work with
ROM-type (most mbm on Z:\) only.
>>> from appuifw import *
>>> avkon = u'z:\\system\\data\\avkon.mbm'
>>> def showicon(id_list, maskfunc=lambda x:x):
...     if type(id_list) == int:
...         id_list = [id_list] # one item
...     entries = []
...     if type(maskfunc) == dict:  # allow dict as a func
...         func = lambda id: maskfunc.get(id,id)
...     else:
...         func = maskfunc
...     for id in id_list:
...         item = u'%s, %s' % (id, func(id))
...         icon = Icon(avkon, id, func(id))
...         entries.append((item, u'', icon))
...     app.body = Listbox(entries, lambda: None)
...
>>> showicon(range(503))  # all icons, including mask
>>> showicon(range(0,100,2), lambda x:x+1)  # first 50 icons, with their mask brother

Using transparent gif/png with Image's blitting

pys60's Image class will load a transparent gif/png
just like a non-transparent one.
But its blit() method accept a mask paramenter.
I have talked about this in a previous snippet.

The missing link is to create a mask automatically
from the Image. It's typically the top-left pixel.
You can use Image's getpixel() which is undocumented.
I show typical use of getpixel here.

Combine them all, here's the automask function.
def automask(im):
    width, height = im.size
    mask = Image.new(im.size, '1') # black and white
    tran = im.getpixel((0,0))[0]   # transparent top-left
    for y in range(height):
        line = im.getpixel([(x, y) for x in range(width)])
        for x in range(width):
            if line[x] == tran:
                mask.point((x,y), 0)  # mask on the point
    return mask

An example usage
from graphics import Image

ship = Image.open('E:\\Images\\ship.gif')
mask = automask(ship)

canvas.blit(ship, mask=mask)  # don't forget to create canvas first

A Sprite class can be defined based on this too.

Image/canvas bliting

I can't remember how to call the blit method.
So, here's just a personal reminder.
(BTW, the pys60 doc has a wrong order between target and source)
app.body = c = Canvas()
w,h = 20,20
im = Image.new(size=(w,h))

# here's prototype
# blit(im, source=(0,0,w,h), target=(0,0), mask=None, scale=0)
# here are examples
c.blit(im)  # put entire image on top-left
c.blit(im, target=(70,70)) # put it about mid-screen
c.blit(im, (0,0,w/2,h/2))  # put a quater image on top-left

# double the image size and put it about mid-screen
c.blit(im, target=(60,60,100,100), scale=1)

If scale is not specified and source <> target, the image
will be clipped instead of resized.

mask is a 1-bit image with the same size as image.
mask = Image.new((w,h),'1')

Pixel on the image will be copied if the same pixel
on the mask is 'white'.
So we can use mask to make 'Sprite'.

Masked Input //Javascript Object


Simple and generic mask routine.

[UPDATED CODE AND HELP CAN BE FOUND HERE]



@REQUIRES Event-Listener

// REQUIRES: http://jsfromhell.com/geral/event-listener

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/forms/masked-input [v1.0]

MaskInput = function(f, m){ //v1.0
    function mask(e){
        var patterns = {"1": /[A-Z]/i, "2": /[0-9]/, "4": /[À-ÿ]/i, "8": /./ },
            rules = { "a": 3, "A": 7, "9": 2, "C":5, "c": 1, "*": 8};
        function accept(c, rule){
            for(var i = 1, r = rules[rule] || 0; i <= r; i<<=1)
                if(r & i && patterns[i].test(c))
                    break;
                return i <= r || c == rule;
        }
        var k, mC, r, c = String.fromCharCode(k = e.key), l = f.value.length;
        (!k || k == 8 ? 1 : (r = /^(.)\^(.*)$/.exec(m)) && (r[0] = r[2].indexOf(c) + 1) + 1 ?
            r[1] == "O" ? r[0] : r[1] == "E" ? !r[0] : accept(c, r[1]) || r[0]
            : (l = (f.value += m.substr(l, (r = /[A|9|C|\*]/i.exec(m.substr(l))) ?
            r.index : l)).length) < m.length && accept(c, m.charAt(l))) || e.preventDefault();
    }
    for(var i in !/^(.)\^(.*)$/.test(m) && (f.maxLength = m.length), {keypress: 0, keyup: 1})
        addEvent(f, i, mask);
};

//]]>
</script>



Usage (sorry, i'm lazy to write a help hahaha)

<form action="">
telephone "(99)9999-9999"

<input type="text" />

date "99/99/9999"

<input type="text" />


máscara = letter + letter withou accent + 2 numbers + "-" + anything + letter "Cc99-*C"

<input type="text" />


everything, but a, b or c "E^abc"

<input type="text" />


only a, b or c "O^abc"

<input type="text" />


just letters "C^"

<input type="text" />


just letters and also white-space "C^ "

<input type="text" />


just numbers and also the letters a, b e c "9^abc"

<input type="text" />

</form>

<script>
with( document.forms[0] ){
MaskedInput.apply( elements[0], '(99)9999-9999' );
MaskedInput.apply( elements[1], '99/99/9999' );
MaskedInput.apply( elements[2], 'Cc99-*C' );
MaskedInput.apply( elements[3], 'E^abc' );
MaskedInput.apply( elements[4], 'O^abc' );
MaskedInput.apply( elements[5], 'C^' );
MaskedInput.apply( elements[6], 'C^ ' );
MaskedInput.apply( elements[7], '9^abc' );
}
</script>

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS