Skip to main content

Posts

Variadic template function - Part 1

 In C, we know we have something called printf() function. This printf() function can take any number of arguments of any built-in type (not user-defined type). As an example, the following piece of code is valid for the standard built-in printf() function. <Code> const char* msg = "%s can accept %i parameters (or %s)."; printf(msg, std::string("Variadic templates"), 100, "more"); </Code> The problem in this code is, it will not print the string part, but it gets compiled with the MSVC as well as GCC 8.2 compiler. However, if I change the code like below, now it prints everything properly. <Code> const char* msg = "%s can accept %i parameters (or %s)."; printf(msg, std::string("Variadic templates").c_str() , 100, "more"); </Code> The printf is like a variadic template function but it sticks to only built-in types. Now the question comes how can I write variadic template functions in C++. In C++ 11 the ...

Bucket Sort using Insertion Sort

 A simple implementation of Bucket Sort, using insertion sort to sort bucket elements.  #include <iostream> #include <cassert> #include <algorithm> #include <vector> template<class FwdIt, class compare = std::less<>> void insertionSort(FwdIt begin, FwdIt end, compare cmp = compare{}) {     for (auto it = begin; it != end; ++it)     {         auto const insertion = std::upper_bound(begin, it, *it, cmp);         std::rotate(insertion, it, std::next(it));         assert(std::is_sorted(begin, std::next(it), cmp));     } } void bucketSort(float arr[], int N) {     std::vector<std::vector<float>> arrBucket(N); // Created N empty buckets     /* Segregating array elements in different buckets*/     for (int i = 0; i < N; ++i)     {         int bucketIndx = N * arr[i];     ...

A concept to a product (Kimidori [ 黄緑]) - Part 2

In the previous part , we have seen KIMIDORI [ 黄緑] detect if a URL is malicious. In this part, we will see the details that KIMIDORI [ 黄緑] fetches out of the URL provided. As an example, provided a safe URL, https://www.azuresys.com/, and let's see what it brings out: As we can see, the link is safe and the link is active, which means we can just click on the link to open it on IE.  Now it's time to look into the URL report (still under development):  We have URLs IP, Location, and HTTP Status code. The Report part is a sliding window, the Show Report button shows as well as hides the report. Show / Hide Report is a toggle button. Let's see if we get the same details for any bad (phishing / malicious) URL: Took an URL example from a phishing link and tested it. The tool detected it as not a good link (Screen Shot Below) & link does not activate unlike a safe URL: Now let's see the report part for more details including domain registration details: It looks ...

A concept to a product (Kimidori [ 黄緑]) - Part 1

It has been 3.5+ years since I have taken a career break due to aging parents. Soon after my career break Dad passed away and Mom was detected with third-stage of throat cancer. I became a full-time caretaker of my mom. During the peak phase of COVID-19, I was daily visiting the hospital for her chemo and radiotherapy. The hospital wasn't willing to admit the patient due to COVID-19 fear. However, I continued my service in the hope that one day she will recover but after almost two years of battle I lost her. Life is seemingly strange to me. I was under the illusion that I can cure my mom. But divinity had a different plan. Her demise put me on a different level of thought process. I felt strongly nothing is really permanent in this material world. My mom told me that just before her demise, she possibly realized that her time is short and asked me to let her go as she was not able to take any more pain. She told me that 'sometimes losing somewhere, marks the beginning of winni...

The Facade pattern

Facade design pattern is a structural design pattern and it's used widely. The aim of this design pattern is to provide a simple interface to the client for a complex underlying system. So, facade means face of the building. This design pattern hides all complexities of the system and just displays a simple face. Very common example could be URL interface of a browser, which hides all complexities behind and only accepts a URL which user intends to browse. Another common example could be withdrawal / deposit of money from banking system via ATM. To withdraw money we need following steps to achieve successfully: 1. Validate card / account number 2. Validate pin 3. In case of withdrawal check account balance and allow / disallow withdrawal 4. Follow steps 1 & 2 for account deposit (Steps 3 not needed). 5. Finally show the balance. Code Example:  <Code> File: WelcomeBank.h #pragma once #include < fmt/format.h > class WelcomeBank{     public :   ...

The Visitor pattern

What is visitor pattern? 1.      It encapsulates an operation executed on an object hierarchy in an object. That says, it allows to add   methods to classes of different types without much altering to those classes. 2.        Enables to define new operations without changing the object hierarchy. Use-Case: a. Operations shall be performed on object hierarchy b. The operations change frequently but object hierarchy is stable. Basic UML Class Diagram:         Visitor: ·   Defines the visit operation on the object structure     Tax Visitor: ·   Implements the visit operation for each type of object     Visitable: ·   Defines the accept operation which takes visitor as an argument      Visitable Element(s):                    ·   Implements accept operations    ...

Variadic template class to add numbers recursively during compilation

 The idea of having a class to add numbers (variable parameters) during compilation time recursively. Also wanted to restrict types to a single type while sending parameters to class member function. That said, if we mix int, float and double types to add function shall result in compilation error. How do we achieve this. The below is the code which actually helps to achieve this: <code> #include < fmt/format.h > template < typename T> class MyVarSumClass{     private :         T _sum = 0 ;     public :         template < typename ... TRest>         T add(T num, TRest... nums){             static_assert (std::conjunction<std::is_same<TRest, T>...>{}); /* Assert fails                if types are different */             _sum += num;         ...