Showing posts with label CPP Scope. Show all posts
Showing posts with label CPP Scope. Show all posts

1/16/2015

range() function in C++ : Inspired from Python

# What is range() function, and how it can help you ?
    # A sequence of numbers from starting point to end point ( End point may be undefined, so you have a Infinite range), and optional step parameter.
    # You can generate a sequence integers ,
        example:-
            range(1, 10)  # Exclusive ranges == [start, end, step)
            [1, 2, 3, 4, 5, 6, 7, 8, 9]

            range(1, 10)  # Inclusive ranges == [start, end, step]
            [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    # You are not limited to generate ranges of integers, you can also generate ranges from  char / float ( * )  data type.
        example:-
            range('a', 'z', 1)    # Exclusive ranges == [start, end, step)
            a b c d e f g h i j k l m n o p q r s t u v w x y

            range('A', 'z', 1 )    # Inclusive ranges = [start, end, step]
            A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z


    # More Examples :- ( Exclusive ranges )
        std::vector<char> v = rangeE('a', 'z', 1  );
            a b c d e f g h i j k l m n o p q r s t u v w x y

        std::vector<char> v = rangeE('A', 'z', 3);
            A D G J M P S V Y \ _ b e h k n q t w

        std::vector<int> v = rangeE(0, 100, 4);
            0 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 96

        std::vector<float> v = rangeE<float, float>(10.3, 345.3, 0.4);
            10.3 10.7 11.1 ,... 343.497 343.897 344.297 344.697 345.097

        std::vector<int> v = rangeE(10, 100);
            10 11 12 13 ,... 97 98 99

        std::vector<float> v = rangeE<float, float> (0, 100, 0.5);
            0 0.5 1 1.5 2 ,... 97 97.5 98 98.5 99 99.5

        std::vector<char> v = rangeE('A', 'z', 1  );
            A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y

    # More Examples :- ( Inclusive ranges )
        std::vector<char> v = rangeI('a', 'z', 1  );
            a b c d e f g h i j k l m n o p q r s t u v w x y z

        std::vector<char> v = rangeI('A', 'z', 3);
            A D G J M P S V Y \ _ b e h k n q t w z

        std::vector<int> v = rangeI(0, 100, 4);
            0 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 96 100

        std::vector<float> v = rangeI<float, float>(10.3, 345.3, 0.4);
            10.3 10.7 11.1 ,... 345.097 345.497 345.897 346.297


        std::vector<int> v = rangeI(10, 100);
            10 11 12 13 ,.. 97 98 99 100

        std::vector<float> v = rangeI<float, float> (0, 100, 0.5);
            0 0.5 1 1.5 2 ,... 98 98.5 99 99.5 100 100.5

## range() function using C++
### Exclusive ranges

#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>

using namespace std;

template<class T=int, class U=int>
std::vector<T> rangeE(T start, T stop, U step=1){
    std::vector<T> v;

    while(1) {
        if (start >= stop) {
            break;
        }
        v.push_back(start);
        start += step;
    }
    return v;
}

int main(int argc, char const *argv[])
{
    // std::vector<char> v = rangeE('a', 'z', 1  );
    // std::vector<char> v = rangeE('A', 'z', 3);
    // std::vector<int> v = rangeE(0, 100, 4);
    // std::vector<float> v = rangeE<float, float>(10.3, 345.3, 0.4);
    // std::vector<int> v = rangeE(10, 100);
    std::vector<float> v = rangeE<float, float> (0, 100, 0.5);
    for(auto i: v){
        cout << i << ' ';
    }
    cout << '\n';
    return 0;
}

### Inclusive ranges

#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>

using namespace std;

template<class T=int, class U=int>
std::vector<T> rangeI(T start, T stop, U step=1){
    std::vector<T> v;

    while(1) {
        if (start >= stop+1) {
            break;
        }
        v.push_back(start);
        start += step;
    }
    return v;
}

int main(int argc, char const *argv[])
{
    // std::vector<char> v = rangeI('a', 'z', 1  );
    // std::vector<char> v = rangeI('A', 'z', 3);
    // std::vector<int> v = rangeI(0, 100, 4);
    // std::vector<float> v = rangeI<float, float>(10.3, 345.3, 0.4);
    // std::vector<int> v = rangeI(10, 100);
    std::vector<float> v = rangeI<float, float> (0, 100, 0.5);
    for(auto i: v){
        cout << i << ' ';
    }
    cout << '\n';
    return 0;
}



# Tests :-

## Does our range() function can be directly used in other programs?
## What about its performance?
## Does it comparable to native range() function in C++?

### Implementing and using native range() function in C++11
iteration = 10000000; Language = C++11
# Write output to Console
ssp@ssp-pc ~/Programming/CPP $ xterm ./ExclusiveRangeDef
10.5701301098
10.5873568058
10.4189360142
10.7807810307
10.10414505

# Don't print output to console/Terminal/Command Prompt, Just save that into a Text FILE.
iteration = 10000000; Language = C++11
ssp@ssp-pc ~/Programming/CPP $ xterm -e "./ExclusiveRangeDef > dumploop.txt"
0.556797981262
1.02536702156
1.04401111603
0.961767911911
0.982132911682
0.971391916275
0.934329032898
1.09931206703
0.961050033569
0.937591075897


iteration = 10000000*10; Language = C++11
# Don't print output to console/Terminal/Command Prompt, Just save that into a Text FILE.
ssp@ssp-pc ~/Programming/CPP/RangeInCPP $ xterm -e "./ExclusiveRangeDef > dumploop.txt"
6.75697207451
6.55503702164
8.64361310005
5.96445202827
7.79801106453
6.90669894218
6.0758459568
6.74977111816
7.17069101334
7.01367592812



### Implementing and using range() Function with another function in C++11

iteration = 10000000; Language = C++11
ssp@ssp-pc ~/Programming/CPP $ xterm ./ExclusiveRange
10.5833230019
10.6844108105
10.6519501209
10.6691529751
10.6650300026

# Don't print output to console/Terminal/Command Prompt, Just save them into a Text FILE.
iteration = 10000000; Language = C++11
ssp@ssp-pc ~/Programming/CPP $ xterm -e "./ExclusiveRange > dumploop.txt"
0.753065824509
1.19918298721
1.18582081795
1.1427628994
1.60647511482
1.0098259449
1.18290019035
1.12054014206
1.10526990891
1.14365291595

iteration = 10000000; Language = C++11; Implementation = using C++ Template
ssp@ssp-pc ~/Programming/CPP/RangeInCPP $ xterm -e "./ExclusiveRangeTemp > dumploop.txt"
1.16944003105
0.979794979095
1.31697702408
0.782207012177
1.13583612442
1.56683707237
1.01916909218
1.18384790421
0.986218214035
1.383425951

# Don't print output to console/Terminal/Command Prompt, Just save that into a Text FILE.
iteration = 10000000*10; Language = C++11
ssp@ssp-pc ~/Programming/CPP/RangeInCPP $ xterm -e "./ExclusiveRange > dumploop.txt"
8.63241100311
8.56675696373
6.39435505867
8.33173489571
8.47799301147
8.60728192329
8.34282588959
7.66121196747
7.68087077141
8.34091496468


# Testing our built function within nested loop
# iteration = 1000x1000; Language = C++11
# Code :-

    for(auto i: rangeE(0, 100, 2)) {
        for(auto j: rangeE(0, 100, 2)) {
            cout << i << ' ' << j << endl;
        }
    }

ssp@ssp-pc ~/Programming/CPP/RangeInCPP $ xterm -e "./ExclusiveRangeTempNest0 > dumploop.txt"
1.52887296677
1.60038113594
1.56652903557
1.78646588326
1.50389409065
1.59423518181
1.56246495247
1.57362604141
1.58079385757
1.56350493431

# iteration = 1000x1000; Language = C++11
# Code :-

    for(int i=0; i< 1000; i+=2) {
        for(int j=0; j< 1000; j+=2) {
            cout << i << ' ' << j << endl;
        }
    }
    cout << endl;

ssp@ssp-pc ~/Programming/CPP/RangeInCPP $ xterm -e "./ExclusiveRangeDefNest0 > dumploop.txt"
1.5027050972
1.65471196175
1.49789905548
1.7509188652
1.52278089523
1.58404016495
1.52930808067
1.54360198975
1.54617094994
1.54985117912


# Built-in Python  range() function tests :-

iteration = 10000000; Language = Python2.7
ssp@ssp-pc ~/Programming/CPP $ xterm ./ExclusiveListPY.py
11.4798099995
11.4038779736
11.5232269764
11.460515976
11.6050000191

# Don't print output to console/Terminal/Command Prompt, Just save them into a Text FILE.
iteration = 10000000; Language = Python2.7
ssp@ssp-pc ~/Programming/CPP $ xterm -e "./ExclusiveListPY.py > dumploop.txt"
2.33868694305
2.60927009583
2.50296115875
2.53171110153
2.93597483635
2.12743091583
2.47753596306
2.4461350441
2.69060111046
2.41603589058

# NOTE :- testing of programs is done with Python Script, that will tell you execution time taken by a program.
# PC conf :-
Intel CORE 2 DUO ( 2GB RAM )
LinuxMint 17 - x64


1/11/2015

Custom Build System for C++11 : Sublime-Text-3






# Why sublime-text?
    # Because, it is lightweight, portable, and runs on Win/Linux/MacOS.
    # and it is really awesome.
    # it is more flexible and extensible.
    # You can turn your Sublime-text editor into a FULL featured IDE.
    # I really use it for C/C++/HTML/JS/CSS/Python/Java, and it is great.
    # Sublime-Text Editor is more real, because it allows you to customize almost everything.
    # For Compiled languages, you can use pre-built BUILD SYSTEM or create your own.
    # Snippet, that is a great idea and that would be great if you learn, how to write snippet and use them.

# Why we are here?
    # What is Build-system in Sublime-Text Editor?
        # Build-system allows you to write and build programs directly ( Just hit, CTRL+b ) and if you want to run build binary ( Just hit, SHIFT+CTRL+b )
        # Build System ( RUN, BUILD ) basically allows you to run Two COMMANS using their shortcut key, that really does not matter which command you want to invoke with CTRL+b and CTRL+SHIFT+b.



    # What about pre-built BUILD SYSTEM for C++
        # Hmm, that is great, but we want some more functionality ( I want to pass some command line parameters ) , this is why we want to write our own Custom BUILD SYSTEM for C++.

    # Sublime-Text Build System Script for C/C++
    ## When, You want to use Flags like, -std=c++11
    #### C++11.sublime-build ( FOR LINUX )

{
    "shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\"",
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c, source.c++, source.cpp",

    "variants":
    [
        {
            "name": "Run",
            "shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\" -std=c++11 && \"${file_path}/${file_base_name}\""
        }
    ]
}
    ####

    ## When, you want to run your program in Terminal ( xterm ) instead of sublime-Text console, Becaue you can pass input using Sublime-Text Console.
    ## NOTE, that you may not be able to see terminal because your program won't wait for your response.
    ## So, you need to stop your C/C++ program from being exiting automatically.
    #### C++11.sublime-build

{
    "shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\"",
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c, source.c++, source.cpp",

    "variants":
    [
        {
            "name": "Run",
            "shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\" -std=c++11 && \"${file_path}/${file_base_name}\""
        }
    ]
}
    ####

    # When, you want to run your program in a separate terminal process, that won't exit automatically even if program exists itself or you can tell xTerm, not to close itself.
    # FINAL Version : filename: C++11.sublime-build
    # PATH : $USER_HOME/.config/sublime-text-3/Packages/User/
    # FOR LINUX Operating System
    #### C++11.sublime-build

{
    "shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\"",
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c, source.c++, source.cpp",

    "variants":
    [
        {
            "name": "Run",
            "shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\" -std=c++11 && xterm -fg green -bg black -hold -e \"${file_path}/${file_base_name}\""
        }
    ]
}
    ####

    # FINAL Version : filename: C++11.sublime-build
    # PATH : %sublime-text-3%\Packages\User\
    # For Windows:-
        # NOTE, Ensure that GNU GCC Compiler is in your System PATH environment variable.
        # If you want to check whether g++/gcc BINARY is in your system PATH.
             # use  which   Command for LINUX (  which -a g++ )
             # use  where   Command for Windows  (  where gcc )

        # So if you are a Windows user and you might want to use some external Terminal Emulator Programs for Windows, just like, ( mintty, console2, cmder, conemu ) .
        # I recommend you to use mintty terminal emulator for windows that works just as  xterm   in Linux.

        # Command Example:-

mintty --hold always -e g++ sample.cpp -o sample.exe

        # One, more TIP for windows user( Optional for Linux Users)
            # Sometimes you might want to supply some input to your program.
            # There are actually two ways, first, You are going to type each and every required value, but for sublime, let me tell you one thing, Sublime-Text-3 console is not Interactive, means it will not ask you for any input, this just runs and show you the output.
            # BUT you have second option, When you want to automate your compiled program, OK, we are going to write every required query for proper functioning of our program in a separate text file and tell Sublime-Text-3 to take input data from this file Instead of STDIN FILE.

{
    "shell_cmd": "g++ -std=c++11 \"${file}\" -o \"${file_path}/${file_base_name}.exe\"",
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c, source.c++, source.cpp",

    "variants":
    [
        {
            "name": "Run",
            "shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\" -std=c++11 && mintty --hold always -e \"${file_path}/${file_base_name}.exe\""
        }
    ]
}





# Take a look :-


# Download Sublime Text Editor :-
    http://www.sublimetext.com/
    http://www.sublimetext.com/3


# References :-
    http://sublime-text-unofficial-documentation.readthedocs.org/en/latest/reference/build_systems.html

1/09/2015

C++ _ Scope of any Identifier : Talks to you



# Scope ?
    # In C++ before using any variables, functions, classes and types or compound types needs to be declare first somewhere in your C++ program.
    # Where you declare or define them, decides their visibility.
    # Visibility means whether you can use that everywhere or somewhere in your program.

# GLOBAL variables ?
    # A list of variables that can be used anywhere in your program without even declaring and defining them are GLOBAL variables.

# LOCAL variables ?
    # Those who exists in some defined code blocks.
    # outside that code block they does not exists.
    # sometimes local variables are used to protect GLOBAL variables.

 # In C++ you can declare a identifier once within a single scope.
 # In C++ visibility of Identifiers is influenced by curly brackets that is used to group more than one C++ statements together.
# This means you can create a new scope/world for your new/old variables.

 # Namespace ?
    # Namespace is a C++ Identifier that used to group some code functionality within a single name of identifier.
    # You must be familiar with  std  that is a namespace in C++ that holds all
standard C++ functions/routines, Global variables, Classes.

    # while printing something to Console/Terminal you use :-
        std::cout << "Hello world!" << endl;

    # This is how you can access any function or routine from std namespace even without importing anything from there.


# If you know Java/C#, than you may be familiar with this concept.


# Importing selected Identifiers from any Namespace ?


# Importing everything from selected Namespace into current scope ?

# scope matters while importing anything from any Namespace.



# Static , Global, and Local variables ?

  
# References :-
    http://www.cplusplus.com/doc/tutorial/namespaces/