2010-06-28

Java 4-ever - JavaZone trailer


(Removed due to copyright claims. Another source follows.)

2010-06-25

French and Italian soccer / Francia és olasz foci

--- English version follows

A francia és az olasz csapat tagjai tegnap közösen meglátogattak egy fokvárosi árvaházat. Szívszorító volt látni a szomorú kis arcukat és a reménytelenséget – nyilatkozta a 6 éves Dzsamal.

--- English version

The members of the French and Italian soccer teams joined together and visited a shelter for orphaned children in Cape Town. It was heart-smothering seeing their little sad faces and the hopelessness – explained Jamal the 6 years old boy.

2010-06-23

Vuvuzela for eva'


Vuvuzela for ever

→ @index.hu

2010-06-18

My penis is too short / A péniszem túl rövid

--- English version follows

Azt mondta a rendszer, hogy meg kell adnom egy jelszót. Azt mondtam "faszom". Mire a rendszer közölte, hogy túl rövid.

Megkérdeztem egy kollégámat, hogy ilyenkor mit kell csinálni. Azt mondta, hogy elfelejtettem beírni a méretét, úgy mint "faszom45". Működött.

--- English version

The system said that give it a password. I typed "mydick". The system said it is too short.

I asked a colleague what to do. He said that I have to type in the size, like "mydick45". It worked.

2010-06-17

n over k

long long NOverK(int n, int k)
{
    if(k > n >> 1)
        k = n - k;
    long long ret = 1;
    for(int i = 1; i<=k; ++i,--n)
        (ret *= n) /= i;
    return ret;
}

2010-06-09

Gibbets (game / játék)

Save the hanging people!
Mentsd meg a felakasztott embereket!



http://www.notdoppler.com/gibbets.php

UPDATE: It has a second episode.
FRISSÍTÉS: Van második része is.

http://www.notdoppler.com/gibbets2.php

2010-06-07

C++: Class vs Struct (clarification)

Classes and structures are the same thing in C++ except that the struct's default visibility is public, the class' is private. To highlight the equivalence: struct can have member functions, constructors, overloaded operators, virtual member functions, private members. Classes can have public data members. It is really just about the default visibility of members.

Let's see equivalent examples, let's talk about the philosophy later...

struct Vector {
    double x,y;
};

class Vector {
public:
    double x,y;
};

and

class Vector {
public:
    Vector(double x_, double _y);
    Vector& SetXY(double x_,double y_);
    double GetX() const;
    double GetY() const;
    //...
private:
    double x,y;
};

struct Vector {
    Vector(double x_, double _y);
    Vector& SetXY(double x_,double y_);
    double GetX() const;
    double GetY() const;
    //...
private:
    double x,y;
};

In my opinion class's default visibility is not very useful because we used to start the class definition with the public members to ease the interpretation of it for the users of our class.

The big difference is in the intent of the declaration. If we want to create a composite behavior-less data unit that is passed around, then we should use the struct keyword so the reader will know what's going on. If you want to create an object which fulfills an abstract concept and has a behavior, then you should define a class.

IMPORTANT: If you use class you should really take care about the basic requirements like hiding data members (making them private or protected in order to maintain principle of local responsibility), providing type-invariant safe public methods, disabling copy semantics if not needed explicitly, and so on ...

2010-06-06

C++: The static keyword (clarification)

The static keyword can have three different meaning depending on the context.

Static function or variable

The variable or function name will not be propagated for the linker, that means that this variable or function will be available only in it's compilation unit. This feature is rarely used, only if we want to hide something explicitly from other compilation units.
static int MySecretFunction(){ /* ... */ }
static int MY_SECRET_VARIABLE = 7; // You should give a starting value

Static variable in a function

int Summarize(int next)
{
    static int sum = 0;
    sum += next;
    return sum
}
The static variable in a function is initialized at the first time when the function is called. WARNING: Using this feature is not thread-safe in C++. In C++0x you will have the opportunity to write
thread_local static int sum = 0;
which creates a global variable in the function's scope for each thread.

Static class members

Class member functions (methods) can be static or non-static. Non-static members are bound to the objects of the class, static members are not bound to the objects but are somehow related to the class. Study example:
// matrix.h:
class Matrix {
public:
    Matrix(int n_, int m_); // Constructor
    Matrix& MultiplyByNumber(double lambda); // Non-static member function
    static Matrix CreateIdentityMatrix(int n); // Static member function
    // ...
private:
    int n,m; // Non static emember variables
    double *pData; // Non static member variable
    static int serial; // Static member variable
};
// matrix.cpp:
int Matrix::serial = 0;
// ...
Note: You have to provide explicit storage place for the static member variables, that means you have to choose a compilation unit (.cpp file), where you write int Matrix::serial = 0;.
Non-static (or object-) methods can access both static and non-static member functions and variables. Static methods can access only static members. (And of course non-static members of the objects that are around.)

Simple global variables and initialization

// gadget.h:
extern int dontUseGlobalVariablesIfPossible; // "Propagation" in the .h file
// gadget.cpp:
int dontUseGlobalVariablesIfPossible = 0; // Storage place
The initialization of variables in static storage class (simple global variables, static variables, static member variables, but not function's static variables) are done in the order that they are presented inside the compilation unit (see exception below). The order of initialization of two variables in different compilation units is undefined.
The important exception is that primitive types (int, char, pointer, float, double, ...) are initialized upon loading if they are initialized with literals.
int x = 0; // Load time, practically before "everything"
int y = f(); // Simply before main() based on the rules above.

2010-06-04

C++: References and pointers (clarification)

pointer
A pointer refers directly to (or "points to") another value stored elsewhere in the computer memory using its address. It can be also interpreted as a pointer to many objects, depending on the semantics of the usage.
reference
Is a pointer with some (useful) restrictions and a reliefs. It's value can be set only once (at declaration) and it can point only to one object. The relief is that you don't have to apply any syntax for dereferencing. Another benefit is that you know that the area pointed by a reference does not have to be freed by you.

Pointer examples

int array[10]; // Array with 10 elements allocated on the stack
*array = 5; // Set the zeroth element to 5
*(array+1) = 6; // Set the first element to 6
array[2] = 7; // Set the second element to 7

int *array2 = new int[10]; // Array allocated on the heap
*array2 = 5; // Set the zeroth element to 5
*(array2+1) = 6; // Set the first element to 6
array2[2] = 7; // Set the second element to 7
delete[] array2; // Freeing allocation

int *p = array+3; // Create a pointer to the 3rd element
*p = 8; // Set it to 8

char s1[] = {'H','E','L','L','O','!',0}; // Char array on stack (terminated by zero)
char s2[] = "HELLO!"; // Char array on stack (terminated by zero)
const char *s3 = "HELLO!"; // Pointer to string literal. "const" because you do not want to change a literal, do you?
  • In the first case you don't really have a pointer that is stored anywhere. Handling of aggregated arrays (on stack or in a class/struct) is presented with pointer-syntax.
  • In the second case you really have a pointer on the stack that points to the allocated area which is on the heap.
To demonstrate the difference between pointers and pseudo pointers:
++array; // Will not compile
++array2; // Will incrase the array2 pointer by one
// (it will point to the 1st element in the array - not the 0th)
Note that you still have to call delete[] with a pointer that points to the 0th element!!! Something like
delete[] (array2-1);

Reference examples

Reference typed parameter

std::string ReverseText(const std::string& text)
{
    size_t len = text.size();
    std::string ret(len,'\0');
    for(int i=0; i<len; ++i)
    {
        ret.at(i) = text.at(len-1-i);
    }
    return ret; // We love "named return value optimization"
}
Note: ret.at(i) returns a reference to the i-th character of the string.
  • The parameter's value is not copied, just a pointer to it.
  • You don't have to use pointer syntax like (*text).size() (or text->size()), you can use text.size() conveniently
  • You don't have to test for NULL pointer. The user of the function can clearly see that he cannot pass a NULL pointer, that he/she must provide this parameter.
Invocation:
std::string s("Heavy Metal music");
std::cout << ReverseText(s) << std::endl;

Aliasing

A real life example would involve more complex concepts than the aliasing itself. It is rarely used, but very useful when needed. So let's see a l'art pour l'art teaching example!
unsigned int LargestCommonDivider(unsigned int a, unsigned int b)
{
    while(a!=b)
    {
        unsigned int &greater(a>b?a:b);
        unsigned int &smaller(a>b?b:a);
        greater -= smaller;
    }
    return a;
}
  • Let's see the usefulness and necessity of "functional programming minded" operator ?: here!
  • The a and b variables are copies of the parameters given at the place of invocation, they can be used as ordinary local variables (which they are in reality).
See also initializer list.

Opening beer with a helicopter / Sörnyitás helikopterrel

That's the way this is done.
Így kell csinálni.

2010-06-03

Gully at Guatemala / Guatemalai víznyelő

Pictures without boundaries
(Some of them are boring, but there are some treasure there)

Képek határok nélkül
(Néhányuk uncsi, de van pár igazi gyöngyszem)



@ index.hu