Adding a method for computing Cartesian Product to Groovy’s Collection(s)

groovy-cartesian-productIn these days I’m using the Groovy programming language very often, I found this language very intuitive and expressive. I try to use, when it is appropriate and convenient , Functional programming style and methods.

One of the key elements of functional programming paradigm (opposite to the imperative paradigm) is “thinking in  space rather than thinking in time”, this translates in a extensive usage of collections and constructs for creating a collection based on existing collections. The most common collection used is the list, the syntactic construct for creating a list based on existing lists is named List comprehension.

I think that the list, or more generic collection, comprehension in Groovy is very powerful (Groovy Collection API), and in my everyday usage I found that it has everything that I need to express the algorithm that I implement in terms of Collection comprehension. By the way, more that once I needed to obtain the Cartesian product of two collections, so I thought it is nice to have a method in Collection for computing the Cartesian product.

Cartesian product

The Cartesian product is a mathematical operation which returns a set (or product set) from multiple sets. That is, for sets A and B, the Cartesian product A × B is the set of all ordered pairs (a,b) where a ∈ A and b ∈ B:

f(A, B) = \bigcup_{a\in A}\bigcup_{b\in B} (a, b).

Read more of this post

CRUX PPC 2.5 Release Candidate 4

It’s time for a new (and we hope the last one) release candidate for CRUX PPC 2.5. This release candidate implements all the changes introduced in CRUX PPC Release Candidate 3.

From this release candidate we provide a full support for IBM pSeries machines (since we bought a second hand IBM Intellistation 2-way POWER4+ workstation).

Please test it:

The 32bit version:

The 64bit version:

Please give us feedback on the CRUX PPC forum:

Using ZFS for home partition on Fedora

ZFS is a great filesystem: it’s based on a copy-on-write transactional model, it supports dynamic striping of data across all devices, it supports a transparent compression and many more features, but the more interesting feature to me is the support for snapshot and clones.

I feel interesting snapshots and clones primarily for backups: using a snapshot could be very trivial making a backup of the filesystem, moreover it’s possible making incremental backup with snapshots.

Although ZFS is developed primary for Solaris and OpenSolaris, it’s possible to use it on GNU/Linux through FUSE (Filesytem in Userspace), so it’s possible to use ZFS on GNU/Linux for all purposes (although could be very trouble using it for the root filesystem).

Fedora (10 or 11 ones) is pretty ready for using ZFS: it’s available a package (zfs-fuse) for ZFS-FUSE on the official repository, so the first step for using ZFS is installing this package with yum:

yum install zfs-fuse

Then it’s possible to create the pool of devices containing the filesystems and the filesystems; first of all I’ve created a pool on a partition of one of the harddisks of mine; before creating the book I’ve labelled the partition “ZFS” using “Palimpsest Disk Utility”, so I’ve created the pool with:

zpool create zfs /dev/disk/by-label/ZFS

And then the filesystems, setting the use of compression and disabling the storing of file access time:

zfs create zfs/home
zfs set compression=on zfs/home
zfs set atime=off zfs/home
zfs create zfs/home/cjg

Finally I’ve setted the mount point for the home filesystem:

zfs set mountpoint=/home zfs/home

Les jeux sont fait! From the next reboot I’ll use ZFS for my /home partition.

Now I had to create a new pool for storing the backups, I’ve created it on another partition of another disk (the partition is labelled “ZFSBACKUP”):

zpool create zfsbackup /dev/disk/by-label/ZFSBACKUP
zfs create zfs/home
zfs set compression=on zfs/home
zfs set atime=off zfs/home

I want that all backups will be stored on the zfsbackup pool and I want, also, to use incremental backups, so I’ve setted up a simple script for making incremental backups from the zfs pool to the zfsbackup pool:


DATE=`date +%F-%H-%M`
zfs snapshot zfs/home/cjg@$DATE
zfs send -i latest zfs/home/cjg@$DATE > dump
zfs recv zfsbackup/home/cjg < dump
rm dump
zfs destroy zfs/home/cjg@latest
zfs rename zfs/home/cjg@$DATE zfs/home/cjg@latest

# End Of File

Simply it creates a snapshot of the zfs/home/cjg filesystem and calling it with the current date and time, then with “zfs send” it creates an incremental replication from the latest snapshot, then the replication it’s importend on zfsbackup/home/cjg, the latest snapshot on zfs/home/cjg is removed and the current snapshot is renamed as the latest.
In this way all the snapshot’s history is stored only on the zfsbackup pool.

Before using this script for the first time I had to create the “latest” snapshot on zfs/home/cjg and send it to zfsbackup/home/cjg with:

zfs snapshot zfs/home/cjg@latest
zfs send zfs/home/cjg@latest > dump
zfs recv zfsbackup/home/cjg < dump

To access a particular snapshot it’s possible to use:

zfs clone zfsbackup/home/cjg@2009-05-02-12-33 zfsbackup/home/2009-05-02-12-33

So it’s possible to access to the snapshot file through “/zfsbackup/home/2009-05-02-12-33″; of course it’s possible to use “zfs rollback” to restore the filesystem from a snapshot.

When the clone isn’t needed anymore it’s possible to remove it with

zfs destroy zfsbackup/home/2009-05-02-12-33

Loading font face from file using Mono and Cairo

I was trying to build up a little application for previewing and installing FreeType fonts using Gtk#, but I didn’t found any existing API for loading the font face from a file (.ttf) in Mono.Cairo.

In Mono.Cairo there is the class FontFace that represents a FontFace, this class has only a constructor that accept an IntPtr (handle) as unique arguments, this handle should refer to an allocated and initialized cairo’s cairo_font_face_t.

The only chances that I had for obtaining the cairo_font_face_t handler for the file was to relay to the C API of Cairo and FreeType. The result is a new class, FreeTypeFontFace derived from Cairo.FontFace:

using System;
using System.Runtime.InteropServices;
using Cairo;

public class FreeTypeInitException : Exception
    public FreeTypeInitException() : base("Can't initialize freetype environment.")

public class CreateFaceException : Exception
    public CreateFaceException(string filename) : base("Can't create the face for file: " + filename + ".")

public class LoadFaceException : Exception
    public LoadFaceException(string filename) : base("Can't load the face for file: " + filename + ".")

public class FreeTypeFontFace : FontFace
    private static bool initialized = false;
    private static IntPtr ft_lib;
    private IntPtr ft_face;

    private FreeTypeFontFace(IntPtr handler, IntPtr ft_face):base(handler)
        this.ft_face = ft_face;

    public void Dispose()
        cairo_font_face_destroy (Handle);
        FT_Done_Face (ft_face);
        ((IDisposable) this).Dispose ();

    public static FreeTypeFontFace Create(string filename, int faceindex, int loadoptions)

        IntPtr ft_face;
        if(FT_New_Face (ft_lib, filename, faceindex, out ft_face) != 0)
            throw new LoadFaceException(filename);

        IntPtr handler = cairo_ft_font_face_create_for_ft_face (ft_face, loadoptions);
        if(cairo_font_face_status(handler) != 0)
            throw new CreateFaceException(filename);

        return new FreeTypeFontFace(handler, ft_face);

    private static void initialize() {
        if(FT_Init_FreeType (out ft_lib) != 0)
            throw new FreeTypeInitException();
        initialized = true;

    [DllImport ("")]
    private static extern int FT_Init_FreeType (out IntPtr ft_lib);

    [DllImport ("")]
    private static extern int FT_New_Face (IntPtr ft_lib, string filename, int faceindex, out IntPtr ft_face);

    [DllImport ("")]
    private static extern int FT_Done_Face (IntPtr ft_face);

    [DllImport ("")]
    private static extern IntPtr cairo_ft_font_face_create_for_ft_face (IntPtr ft_face, int loadoptions);

    [DllImport ("")]
    private static extern int cairo_font_face_status (IntPtr cr_face);

    [DllImport ("")]
    private static extern int cairo_font_face_destroy (IntPtr cr_face);

The constructor is private: the FreeTypeFontFace instances should be create using the static method Create because we have to provide the correct cairo_font_face_t handler to the base constructor.

The steps for initialize the cairo_font_face_t structure are:

  1. Initialize the freetype environment if it isn’t already been initialized.
  2. Load the FreeType face from the file using FT_New_Face() (from the FreeType library).
  3. Build the cairo_font_face_t structure from the FreeType face using cairo_ft_font_face_create_for_ft_face().

As you can see, the Create method can throw some exceptions.

When the object isn’t needed anymore the programmer should invoke the Dispose method (as happens with other Cairo related objects).

To obtain a .net library, named FreeTypeFontFace.dll, for using the class save the above file as “FreeTypeFontFace.cs” and build it using:

mcs -pkg:mono-cairo -target:library FreeTypeFontFace.cs

Follow a sample code (written in boo) using the FreeTypeFontFace for displaying a FreeType Font preview:


import System
import FreeTypeFontFace from "FreeTypeFontFace.dll"

import Gtk from "gtk-sharp"
import Gdk from "gdk-sharp"
import Cairo from "Mono.Cairo"

class FontPreview(DrawingArea):
	private fontfile as string
	private static sizes = [6, 8, 10, 11, 12, 14, 16, 18, 20, 22, 24]
	private static lorem = "Loading font face from file using Mono and Cairo"

	def constructor(fontfile as string):
		self.ModifyBg(StateType.Normal, Gdk.Color(0xff,0xff,0xff))
		self.fontfile = fontfile

	def OnExposeEvent(args as Gdk.EventExpose):
		g = Gdk.CairoHelper.Create (args.Window)
		font = FreeTypeFontFace.Create(self.fontfile, 0, 0)
		g.ContextFontFace = font
		p as int = 10
		for i as int in sizes:
			p = p + i + 2
			g.MoveTo(10, p)
		te = g.TextExtents(lorem);
		WidthRequest = te.Width + 20;
		HeightRequest = p + te.Height + 10;
		ig as IDisposable = g

w = Gtk.Window("FontPreview")
w.DeleteEvent += {Application.Quit()}