/images/art2.png

React overview

Init react

Commend

npx create-react-app .

npx means you use this package but don’t download it.

or with redux

npx create-react-app my-app --template redux

life cycle

Class component lifecycle

Function component method

shouldComponentUpdate()

Run before render() to check this component need to render or not.

Concurrent Programming Course note 4

Monitor

signal condition –> waiting monitor –> signaling

producer and consumer with a buffer whose size is one


monitor PC {
    Object buffer;

    void produce(Object o){
        if(buffer != null){ // while
            empty.wait();
        }
        buffer = o;
        full.signal();
    }

    Object consume() {
        if(buffer == null) // while
            full.wait();
        Object temp = buffer;
        empty.signal();
        return temp;
    }
}

Semaphore

// Assumption: E < S < W (Signal and urgent wait)
monitor Semaphore {
    int permit;

    void acquire() {
        if(permit == 0){
            permitsAvailable.wait();
        } else {
            permit--;
        }
    }

    void release(){
        if(!permitAvailable.isEmpty()){
            permitsAvailable.signal();
        } else{
            permit++;
        }
    }
}

readers and writers

assume start first and stop

All algorithm in C++

Content

Algorithm Overview

* –> new feature from C++11

Algorithm NameUsageMutating?Head FileComplexity
accumulateAccumulate values in rangeNnumericO(n)
adjacent_differenceCompute adjacent difference of range and return to another placeNnumericO(n)
adjacent_findFind first equal adjacent elements in rangeNalgorithmO(n)
all_of*Test condition on all elements in rangeNalgorithmO(n)
any_of*Test if any element in range fulfills conditionNalgorithmO(n)
binary_searchTest if value exists in sorted sequenceNalgorithmOn average O(logn + 2). On non-random-access iterator is O(n)
copyCopy range of elementsN(copy)algorithmO(n)
copy_backwardCopy range of elements backwardN(copy)algorithmO(n)
copy_if*Copy certain elements of rangeN(copy)algorithmO(n)
copy_n*Copies the first n elements from the range beginningN(copy)algorithmO(n)
countCount appearances of value in rangeNalgorithmO(n)
count_ifReturn number of elements in range satisfying conditionNalgorithmO(n)
equalTest whether the elements in two ranges are equalNalgorithmO(n)
equal_rangeGet sub range of equal elementsNalgorithmO(2logn + 1) for random access iterator, otherwise O(n)
fillFill range with valueYalgorithmO(n)
fill_nAssigns val to the first n elements of the sequence pointed by firstYalgorithmO(n)
findFind the first element in rangeNalgorithmO(n)
find_endFind last subsequence in rangeNalgorithmO(m*(1+n-m))
find_first_ofReturns an iterator to the first element in the range [first1,last1) that matches any of the elements in [first2,last2)NalgorithmO(nm)
find_ifFind the first element in range in some conditionNalgorithmO(n)
find_if_not*Find the first element in range in some conditionNalgorithmO(n)
for_eachApply function to rangeNalgorithmO(n)
generateAssigns the value returned by successive calls to gen to the elements in the range [first,last)YalgorithmO(n)
generate_nAssigns the value returned by successive calls to gen to the first n elements of the sequence pointed by firstYalgorithmO(n)
includesTest whether sorted range includes another sorted rangeNalgorithmO(n)
inner_productCompute cumulative inner product of rangeNnumericO(n)
inplace_mergeMerge consecutive sorted rangesYalgorithmO(n) if extra memory is available, otherwise is O(nlogn)
iotaStore increasing sequenceYnumericO(n)
is_heap*Test if range is heapNalgorithmO(n)
is_heap_until*Find first element not in heap order.NalgorithmO(n)
is_partitioned*Test whether range is partitionedYalgorithmO(n)
is_permutation*Compares the elements in the range [first1,last1) with those in the range beginning at first2, and returns true if they just different permutationNalgorithmO(n)
is_sorted*Check whether range is sortedNalgorithmO(n)
is_sorted_until*Find first unsorted element in rangeNalgorithmO(n)
iter_swapSwaps the elements pointed to by a and bYalgorithmO(1)
lexicographical_compareCompare two range lexicographically.NalgorithmO(n)
lower_bondReturn iterator to lower boundNalgorithmO(logn + 1) for random access iterator, otherwise O(n)
make_heapMake heap from rangeYalgorithmO(3n)
maxReturns the largest of a and b. If both are equivalent, a is returned.NalgorithmO(1)
max_elementReturn largest element in rangeNalgorithmO(n)
mergeMerge sorted rangesYalgorithmO(n)
minReturns the smallest of a and b. If both are equivalent, a is returned.NalgorithmO(1)
minmax*Return smallest and largest elements from give 2 value or initializerNalgorithmO(1)
minmax_element*Return smallest and largest elements in rangeNalgorithmO(n)
min_elementReturn smallest element in rangeNalgorithmO(n)
mismatchReturn first position where two ranges differNalgorithmO(n)
move*Move range of elementsYalgorithmO(n)
move_backward*Move range of elements backwardYalgorithmO(n)
next_permutationRearranges the elements in the range [first,last) into the next lexicographically greater permutationYalgorithmO(n)
none_of*Test if no elements fulfill conditionNalgorithmO(n)
nth_elementFind the nth element and put it the exact palce.(quick select)YalgorithmO()
partial_sortPartially sort elements in range while the remaining elements are left without any orderYalgorithmO(mlogn)
partial_sort_copyCopy and partially sort rangeY (if in-place)algorithmO(mlogn)
partial_sumCompute partial sums of range and return to another placeNnumericO(n)
partitionPartition range in two and the iterator returned points to the first element of the second group.YalgorithmO(n)
partition_copy*Partition range into twoNalgorithmO(n)
partition_point*Get partition point and Returns an iterator to the first element in second partNalgorithmO(logn + 2)
pop_heapPop element from heap range. Range shrink and value is at end.YalgorithmO(logn)
prev_permutationRearranges the elements in the range [first,last) into the previous lexicographically-ordered permutation.YalgorithmO(n)
push_heapPush element into heap range. Range extendYalgorithmO(logn)
random_shuffleRandomly rearrange elements in rangeYalgorithmO(n)
removeRemoved element equal to val and returns an iterator to the new end of that range.YalgorithmO(n)
remove_copyRemove and copy to new placeYalgorithmO(n)
remove_copy_ifRemove in a given condition and copyYalgorithmO(n)
remove_ifRemove in a given conditionYalgorithmO(n)
replaceAssigns new_value to all the elements that compare equal to old_valueYalgorithmO(n)
replace_copyReplace and copyYalgorithmO(n)
replace_copy_ifReplace in a given condition and copyYalgorithmO(n)
replace_ifReplace in a given conditionYalgorithmO(n)
reverseReverses the order of the elements in the range [first,last)YalgorithmO(n)
reverse_copyCopies the elements in [first,last) but in reverse orderYalgorithmO(n)
rotateRotates the order of the elements and middle becomes the new first elementYalgorithmO(n)
rotate_copyRotate and copyYalgorithmO(n)
searchSearch range for subsequenceNalgorithmO(n*m)
search_nSearch range for n continue elementsNalgorithmO(n)
set_differenceDifference of two sorted rangesN(copy)algorithmO(2(n+m)-1)
set_intersectionIntersection of two sorted rangesN(copy)algorithmO(2(n+m)-1)
set_symmetric_differenceSymmetric difference of two sorted rangesN(copy)algorithmO(2(n+m)-1)
set_unionUnion of two sorted rangesN(copy)algorithmO(2(n+m)-1)
shuffle*Randomly rearrange elements in range using generatorYalgorithmO(n)
sortSort elements in rangeYalgorithmO(nlogn)
sort_heapSort elements of heapYalgorithmO(nlogn)
stable_partitionPartition range in two - stable orderingYalgorithmO(n) with enough space. Otherwise O(nlogn)
stable_sortSort elements preserving order of equivalentsYalgorithmO(nlogn) with enough space, otherwise O(nlognlogn)
swapExchanges the values of a and bYalgorithmO(1)
swap_rangesExchanges a range of valueYalgorithmO(n)
transformTransform rangeYalgorithmO(n)
uniqueRemove consecutive duplicates in rangeYalgorithmO(n)
unique_copyCopy range removing duplicatesYalgorithmO(n)
upper_bondReturn iterator to upper bound. Since [first, last), the value pointed by the iterator must larger than valNalgorithmO(logn + 1) for random access iterator, otherwise O(n)

Copy

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(int argc, char const *argv[])
{
	vector<int> iv = {1,2,3,4,5,6};

	vector<int> iv2(6);
	copy(iv.begin(), iv.end(), iv2.begin());
	// iv2: 1 2 3 4 5 6

	vector<int> iv3(3);
	copy_if(iv.begin(), iv.end(), iv3.begin(), [](int a){return a % 2 == 0;});
	// iv3: 2 4 6

	vector<int> iv4(5);
	copy_n(iv.begin(), 5, iv4.begin());
	// iv4: 1 2 3 4 5 6

	vector<int> iv5(6);
	copy_backward(iv.begin(), iv.end(), iv5.end());
	// iv5: 1 2 3 4 5 6

	return 0;
}

Back to top

SASS (SCSS) features

SCSS features

Variables

$font-stack: Helvetica, sans-serif;
$primary-color: #333;

body {
    font: 100% $font-stack;
    color: $primary-color;
}

Operator

.container {
    width: 100%;
}

article[role='main'] {
    float: left;
    width: 600px / 960px * 100%;
}

article[role='complementary'] {
    float: right;
    width: 300px / 960px * 100%;
}

String interpolation

$name: foo;
$attr: border;
p.#{$name} {
    #{$attr}-color: blue;
}

This is compile to:

Concurrent Programming Course note 3

Semaphore

Initialize how many permissions you will use.

acquire() will add one permission.

release() will remove one permission.

Permission must ≥ 0.

Semaphore solution for the MEP

  1. #criticalSection + permissions = 1
  2. #criticalSection = #acquires − #releases

Mutual exclusion: #criticalSection ≤ 1 since #permission ≥ 0. Absence of deadlock: It never happens that #permission = 0 and #criticalSection = 0

Java

public class Turnstile extends Thread {
    static volatile int counter = 0; // keyword is recommended for variables that are shared
    static Semaphore mutex = new Semaphore (1);
    public void run() {
        for(int i = 0; i < 50; i++){
            mutex.acquire();
            counter ++;
            mutex.release();
            System.out.println(id+"- In comes: "+i );
        }
    }

    public static void main(String args[]) {
        try{
            Thread m1 = new Turnstile (1); m1.start();
            Thread m2 = new Turnstile (2); m2.start();
        } catch(Exception e){}
    }
}

Strong semaphore

Possibility of starvation is caused by the fact that blocked processes are placed in a set of processes. But this can be remedied by changing the set to be a queue.

Concurrent Programming Course note 2

Race condition

Multiple thread access one same variables of object concurrently and at least one does update.

Bad situation.

Atomic operation

An operation is atomic if it execute until it completion without interruption

Critical section

A part of program that accesses shared memory and which we which to execute automatically.

mutual exclusion problem (MEP)

  1. Mutex: at and point in time, there is at most one thread in the critical section
  2. Absence of livelock: If various of threads try to entry the critical section, at lease one of them will succeed.
  3. Free from starvation: A thread trying to enter its critical section will eventually be able to do so.

Deadlock is not exit for a interleaving in a transition system.

Software Development Course note

Agile Vs Traditional SDLC Models

Agile is based on the adaptive software development methods, whereas the traditional SDLC models like the waterfall model is based on a predictive approach. Predictive teams in the traditional SDLC models usually work with detailed planning and have a complete forecast of the exact tasks and features to be delivered in the next few months or during the product life cycle.

Predictive methods entirely depend on the requirement analysis and planning done in the beginning of cycle. Any changes to be incorporated go through a strict change control management and prioritization.

Web development review

Document constructor

$$f(x) = sin(x)$$

Concurrent Programming Course note 1

What is concurrency

Systems of interacting computer programs which share resource and run concurrently.

parallelism and concurrency

Parallelism: Occurring physically at the same time.

Concurrency: Occurring logically at the same time.

synchronization

Process synchronization: Ensure the instructions are executed in certain order.

Synchronization is irrelevant if processes do not interact with each other.

Concurrency, and hence process synchronized, is useful only when processes interact with each other.

interaction

Share memory is kind of interact.

Algorithm feature in c++ STL

See all algorithm click here

Mutating and Non-mutating algorithms

Mutating algorithms

Mutating algorithms means this algorithm will change the content that iterator pointed to. Like copy, swap, replace, fill, remove, permutation, partition, random shuffling and sort.

If your give these algorithms a const iterator, only error will be returned.

#include <iostream>
#include <vector>
using namespace std;

int main(int argc, char const *argv[])
{
    std::vector<int> iv = {22,30,30,17,33,40,17,23,22,12,20};

    vector<int>::iterator ib = iv.begin();
    vector<int>::iterator ie = iv.end();

    sort(ib,ie); //works

    for (std::vector<int>::iterator i = iv.begin(); i != iv.end(); ++i)
    {
        cout << *i << ' ';
    }
    cout << endl;

    // vector<int>::const_iterator ib = iv.begin();
    // vector<int>::const_iterator ie = iv.end();

    // sort(ib,ie); // error
    return 0;
}

Non-mutating algorithm

Algorithm not change any element that iterator pointed to. Like: find, search, for_each, count, equal_mismatch, max, min.