Showing posts with label little sugar. Show all posts
Showing posts with label little sugar. Show all posts

Monday, March 01, 2010

LIttle Sugar: Bash :: Finding the N largest or smallest files under a folder

Finding the N largest files under a folder

find . -type f -print|xargs ls -l|sort -r -n -k 5,5 | tail -N

Finding the N smallest files under a folder

find . -type f -print|xargs ls -l|sort -r -n -k 5,5 | head -N

here replace N by the number of largest files you want in the output

Wednesday, June 17, 2009

Little Sugar: Writing an oscillator function

Say you need to transform values v1, v2 in such a way that they lie between lowerlimit L and upper limit U

then u can use an oscillator function

new value = U - (L + (U * (1 / (1 + value))))

this new value will be between L and U, if u omit L then new value
would be between 0 - U

Thursday, June 11, 2009

Little Sugar: Useful commands while programming on Unix Systems

Here are a list of commands that re use file while programming on unix systems

pid = process id

ps aux | grep process_name
- to search for a given process name , generally use this to fetch pid of process

lsof -p pid
- to get all the files or file descriptors being used by the process having pid

lsof | grep file_name
- to get the list processes using a given file

sockstat | grep process_name
- to see the socket connections between a host and desinations

strace -p pid
- to see what the process is doing in general

nc hostname port
- to see data flowing from the hostname at the given portu

Saturday, September 27, 2008

Little Sugar:Decreasing capacity in C++

When you use vector,string class in C++, the problem that happens is that the capacity just increases and does not decrease, causing the data structure to hog lot of memory.
For example, say in the vector 10 elements were added , then to the same vector 1000000 elements were added. Then 900000 were deleted. This will not cause the capacity to decrease. This might result in the vector still holding on to a lot of memory.

One way to fix this is the sap trick.

vector < int > elements; // the original vector

vector < int > (elements.begin(), elements.end() ).swap(elements)

Thursday, May 22, 2008

C++ : Little Sugar

In C++ when ever you see a function that accepts a const reference than its a valid candidate for temporaries . Temporaries are objects that are created and destroyed whenever there is mismatch in function arguments and a constructor exists that takes the passed argument and converts it to a destination entity via the constructor .

C++ : Little Sugar

mutable keyword is useful in C++ , when you are changing a member variable in side a constant function . A simple way to remove constantness of the "this" pointer is to use code like this

const_cast(this)

Tuesday, May 20, 2008

C++ : Little Sugar

While creating objects in C++ , in order to initialize objects use the member initialization list. When initialization of a class starts , initialization of its members happens . If the member initialization list is not used the default constructor of all the class members is called even before entering the constructor . Once inside the constructor the copy constructor or operator = is called to initialize the member objects .

On the other hand if member initialization list is used only the copy constructor is called once and hence you prevent the over head of an extra call.

Monday, March 24, 2008

Little Sugar : C++

In C++ have you imagined what would happen if the destructor of your class throws an exception . For starters , the object would not get destroyed properly . Actually in reality the control would go back to the callee from the current point directly.

Alright , now think what would happen if the destructor got called as a part of some exception handling code and this destructor threw an exception .

Since the active callee is calling the destructor due to some raised exception .
it would result in the terminate() function getting called and though would terminate the program immediately

Sunday, March 02, 2008

Little Sugar : C++

The only to initialize const pointers in c++ within a class , is via member initialization list.

class MyClass{

Data * const data_ptr;

MyClass(Data * const value) : data_ptr(value)
{}
};

Friday, January 25, 2008

Little Sugar (Trees : relations)

A complete binary tree has odd number of nodes .

Number of leaves + number of internal nodes ( both even) +
1 = number of nodes in a tree

The number of internal nodes + 1 = number of leaves

number of internal nodes = number of leaves - 1

n = number of nodes

n = number of leaves + number of internal nodes

n +1 = 2 * number of leaves

number of leaves = (n+1)/2

The number of total nodes upto height h :

= pow(2,h+1) - 1

The number of nodes for any binary tree at height (h) :

= ceil(n / pow(2,h+1))

Little Sugar (Trees : relations)

A complete binary tree has odd number of nodes .

Number of leaves + number of internal nodes ( both even) +
1 = number of nodes in a tree

The number of internal nodes + 1 = number of leaves

number of internal nodes = number of leaves - 1

n = number of nodes

n = number of leaves + number of internal nodes

n +1 = 2 * number of leaves

number of leaves = (n+1)/2

Wednesday, January 09, 2008

Little Sugar: C++ references

C++ references are a variant of const pointers that always point to something .

Now consider

string s1("abc");

string s2("def");

string & refTos1 = s1;

Now refTosi points to s1.

Even after refTos1 = s2. Its still points to s1 . But the value of s1 is now changed from "abc" to "def"

Tuesday, January 08, 2008

Little Sugar : Big Oh

Consider a scenario where you had a list of N numbers . Now say you have an algorithm that finds the does some sort of comparison . If comparison strategy used compares every element with all the succeeding elements . Then on an average the number of comparisons possible in different cases would be something like this :

(n) + (n-1) + (n-2) .... 1

for the 1 st case we have n comparisons
for the 1 st element we have n-1 comparisons
for the 2 nd element we have n-2 comparison .. and so on.

the sum is = ((n)*(n+1))/2 = O(n^2)

Tuesday, November 13, 2007

Little Sugar : Casting away constant ness

In C++ if you create a constant object , say

const String A("Hello World");

String& B = const_cast(A);

can be used to create a non constant reference to the so called constant object and hence
manipulate it .

Monday, November 12, 2007

Little Sugar :const function pointers and references

The syntax in c++ for constant function pointers is like this

Handle& (*const getHandle) = handle;

where handle is a function returning a reference to the handle.

The syntax for reference function pointers is like this:

Handle& (&getHandle) = handle;

Thursday, November 01, 2007

Little Sugar : What does virtual in C++ mean

In c++ if a function is declared virtual for any class, then that class has an associated
virtual table . With each of those virtual functions a special type of virtual table pointer is associated , which has an entry in the virtual table associated with the corresponding class.

These virtual pointers are used at runtime to figure out which virtual function to invoke at runtime.

Making virtual functions inline does not make sense ?? why ...

Inline functions are meant to be made inline , so that means they don't have an explicit address but then if it does not have an address and its virtual then how will it have an entry in the virtual table ??

It will have an entry , cause our friend compiler will generate a function body for us and embed the address somewhere for it .

Monday, August 13, 2007

Little Sugar (Heaps)

  • Heap is a data structure whcih satisfies the MAX-HEAP or MIN-HEAP property
  • All basic operations on a heap run in a time proportional to the height of the heap (lg n ) where n being the total number of nodes in a heap
  • A heap is generally a complete tree
  • IN an array represenation of an N-element heap the leaves nodes are indexed by n/2+1 , n/2 + 2 ... n
  • In an N element heap , the number of nodes of height H are n/pow(2,h+1)

Thursday, June 28, 2007

Little Sugar ( Mixins in Java)

Mixin ?? what is a mixin ...

Thats what i wondered when i cam accross the phrase . A Mixin is a way of adding features to the core functionlaity of a class . It is different from extending the class , cause it generally pertains to adding features not related to the core or default behaviour of a class .

A little example will make things clear . Say you have a small class Person that represents a person wholly . Not if you want to compare 2 instances of Person , you could implement the Comparable interface . By implementing the Comparable interface you add a functionlity that is not directly related to a Person object but is a help ful feature .

So a simple way to implement a Mixin is to implement an interface .. so much for buzz words...

Wednesday, June 27, 2007

Little Sugar (Security Manager , Java)

Today i ll gloss over a few small things worth remembering about security
in java . So lets get started :

In Java , we hava a Java API . The java API provides us with a lot methods to accomplish different type of tasks . Now if for some reason we want to enforce security on these tasks so that only selected operation can be performed and that to by selected users . In that case we need to create a security policy file.

In the security policy file , we specifiy how permissions are applied to different code sources . We specify differnt permissions in terms of Permission objects that are applied to CodeSource objects .

At runtime a policy object is created corresponding to the policy file .

For the policies to take effect a SecurityManager needs to be installed . Once the security manager is installed it uses the AccessController ,which inturn checks the different permissions based on different permission objects .

When the class loader loads a type in the JVM . It places the type in a protection domain , which encapsulates the permissions . At runtime , when methods on the type instance are invoked the security manger is checked to see if the method can be invoked .

Tuesday, June 19, 2007

Litttle Sugar (Java Collections)

The other day I was reading about java collections . I came accross this trivial point about Sets in java that is worth remembering . In java there are 3 different implementations of the Set interface , namely :
  1. HashSet
  2. TreeSet
  3. LinkedHashSet
HashSet:
The fastest among the 3 implementations , but does not maintain ordering.

TreeSet:
The slowest among the 3 implementations , uses red black tree to internally store items and orders the items by value.

LinkedHashSet:
Its performance comes in between the 1st two but maintain the ordering in which items are
inserted . Uses hashtable with linked list to store items.