Effective C++ Ed. 3rd: Item 20. Prefer pass-by-reference-to-const to pass-by-value

  • For User-defined types(yes, for even small ones) Instead of
    Way 1:
    returnType FunctionName(ArgType1 arg1, ...);

    use
    Way 2:
    returnType FunctionName(const ArgType1& arg1, ...);

    This would reduce the cost of calling copy constructor and destructor for each argument passed as shown above.
  • If a derived object of class ArgType1 is passed by Way1, the copy the called function will get will contain only information defined in Base Class ie. ArgType1 here due to Object Slicing. It can be prevented using Way2.

Note: Iterators and function objects of STL and build-in types are better off passed by value due to their design.
prev | next
I hope this helps
Cheers and try hosting at Linode.
Monish

Effective C++ Ed. 3rd: Item 19. Treat Class Design As Type Design

What all you must consider to provide in a class you write? Check the following:
  1. Constructors and Destructor as well as memory allocation and deallocation fuctions.
  2. Object Initialization vs. Object Assignment.
  3. Copy constructor to pass objects by value.
  4. Error checking for object values outside legal range allowed, specially in constructors, assignment operators and "setter" functions.
  5. Declare virtual functions and virtual destructor depending upon: if you want to allow others to inherit this class.
  6. Type Conversion functions and constructors.
  7. Provide required functionality by defining functions and Overloaded operators.
  8. Disallow suitable standard funtions. (COPY CTOR etc.)
  9. Use access specifiers depending upon who will be allowed to access particular members.
  10. What kind of guarantee does the class provide in terms of exception safety, resource usage?
  11. Make your class generic using class template if  required.
  12. Is this class is what you need? Can't this be achieved through a function?
prev | next
Hope this helps
Cheers and try hosting at Linode.
Monish

Strong Passwords

A strong password is defined as any password which meets the following criteria:
  • At least fifteen (15) characters in length.
  • Does not contain your user name, real name, organization name, family member's names or names of your pets.
  • Does not contain your birth date.
  • Does not contain a complete dictionary word.
  • Is significantly different from your previous password.
  • Should contain three (3) of the following character types.
    • Lowercase Alphabetical (a, b, c, etc.)
    • Uppercase Alphabetical (A, B, C, etc.)
    • Numerics (0, 1, 2, etc.)
    • Special Characters (@, %, !, etc.) 

Examples

Google: Ranking System

Google maintains much more information about web documents than typical search engines. Every hitlist includes position, font, and capitalization information. Additionally, they factor in hits from anchor text and the PageRank of the document. Combining all of this information into a rank is difficult. They designed the ranking function so that no particular factor can have too much influence. First, consider the simplest case -- a single word query. In order to rank a document with a single word query, Google looks at that document's hit list for that word. Google considers each hit to be one of several different types (title, anchor, URL, plain text large font, plain text small font, ...), each of which has its own type-weight. The type-weights make up a vector indexed by type. Google counts the number of hits of each type in the hit list. Then every count is converted into a count-weight. Count-weights increase linearly with counts at first but quickly taper off so that more than a certain count will not help. We take the dot product of the vector of count-weights with the vector of type-weights to compute an IR score for the document. Finally, the IR score is combined with PageRank to give a final rank to the document.

Secure hosted files

Operating System: Ubuntu Server 10.04
Web Server: apache2
As apache2 accesses file as user: www-data and group: www-data, First change the owner using following command:
chown www-data: path/file
and then to making a particular file only readable only to user:www-data
chmod 500 path/file
For scripts and images or logo use 500 (r _ x   _ _ _   _ _ _)
For images which you only want to save but don't want them to accessible to users/www-data, use
300 (_ w x   _ _ _   _ _ _)

Remember to give necessary access to the user which updates the hosted files. Another alternative to secure files is Apache Directives and Containers in http.conf of apache server.

Hope this helps
Cheers and try hosting at Linode.
Monish


Hope this helps
Cheers and try hosting at Linode.

NP = P?

Computer science is largely concerned with a single question: How long does it take to execute a given algorithm? But computer scientists don’t give the answer in minutes or milliseconds; they give it relative to the number of elements the algorithm has to manipulate.
P denotes set of problems which can be solved in polynomial time i.e. having solution times proportional to O(n raise to k), k is any constant. Whereas, NP is class of problems whose solution can be verified in polynomial time and its solution times for some inputs might go near exponential times or so...
The question “Does NP equal P?” means “If the solution to a problem can be verified in polynomial time, can it be found in polynomial time?”
Click here to read more...

P, NP, and NP-Completeness: The Basics of Computational Complexity

Java Program to illustrate - changing contents within the same JFrame object

This can be helpful when one need to make a GUI based java software. When you need that the contents of the same frame object get replaced by contents of another container/panel when a user clicks on the say, 'NEXT' button. Following program will give you hints about how you could do that..
Program Name: GUItest.java
import javax.swing.*;
public class GUItest extends JFrame
{
    GUItest()
    {
        setSize(300, 200);
        setVisible(true);
//        setLayout(new GridLayout(1,1));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.add((new JPanel()).add(new JButton("Hello")));
        this.getContentPane().removeAll();
        this.add((new JPanel()).add(new JButton("World!!")));
    }
    public static void main(String args[])
    {
        GUItest frame1 = new GUItest();
    }
}

Dive into the source code of UNIX

Want to observe how OS works at lower level but find source code of linux  and its features bewildering. Get started with the ->
Lions' Commentary on Unix 6th Edition, with Source Code, San Jose, CA: Peer-to-Peer Communications, 1996.
For your reference, here is the source code and here is the commentary.



Here is an another resource for OS implementation text, instead more precisely for Minix3 unix-based operating system :
Operating Systems : Design and Implementation by A.S. Tanenbaum

Create your own C Header file

See the following simple example to learn: (tested in gcc)
File 1: myLib.c
int sum(int a, int b)
{
      return a+b;
}
File 2: myLib.h
int sum(int, int);
File 3: testLib.c
#include<stdio.h>
#include"myLib.h"
int main()
{
      printf("%d\n",sum(10,20));
      return 0;
}
Commands to run:
gcc myLib.h myLib.c testLib.c
./a.out

so fast - Judy Arrays

 Some of the reasons Judy outperforms binary trees, b-trees, and skip-lists:
  • Judy rarely compromises speed/space performance for simplicity (Judy will never be called simple except at the API).
  • Judy is designed to avoid cache-line fills wherever possible. (This is the main design criteria for Judy.)
  • A b-tree requires a search of each node (branch), resulting in more cache-line fills.
  • A binary-tree has many more levels (about 8X), resulting in more cache-line fills.
  • A skip-list is roughly equivalent to a degree-4 (4-ary) tree, resulting in more cache-line fills.
  • An "expanse"-based digital tree (of which Judy is a variation) never needs balancing as it grows.
  • A portion (8 bits) of the key is used to subdivide an expanse into sub-trees. Only the remainder of the key need exist in the sub-trees, if at all, resulting in key compression.

Learn more here.