Containers Map¶

some refs https://www.sandordargo.com/blog/2023/04/12/vector-of-unique-pointers

In [1]:
import ROOT
In [2]:
import fortranmagic
%load_ext fortranmagic
import os
import sys

import numpy as np

if sys.platform.startswith("win"):
        # Depends of system, python builds, and compilers compatibility.
        # See below.
    f_config = "--fcompiler=gnu95 --compiler=mingw32"
else:
        # For Unix, compilers are usually more compatible.
    f_config = ""

    # Disable only deprecated NumPy API warning without disable any APIs.
f_config += " --extra '-DNPY_NO_DEPRECATED_API=0'"

%fortran_config {f_config}
New default arguments for %fortran:
	 --extra '-DNPY_NO_DEPRECATED_API=0'

c++¶

use it in real compiler such as gcc, clang or msvc: https://visualstudio.microsoft.com/de/vs/community/

In [3]:
%%cpp -d
// P7Containers.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <memory>
#include <map>
#include <string>

using namespace std;
class Geometry {
public:
    Geometry() {}
    virtual ~Geometry() = default;
    virtual double getArea() = 0;
    virtual double getCircumference() = 0;
};

class Rectangle1 :public Geometry {
public:
    Rectangle1(double width, double height) :width(width), height(height) {//when exist c calles the standard constructor of the parent class
    }
    //virtual ~Rectangle() {}//destructor if not used leave it out
    virtual double getArea() override {
        return width * height;
    }
    virtual double getCircumference() override {
        return 2 * (width + height);
    }
protected: //can be only accessed by this class and children

private: //can only be accessed by this class
    double width;
    double height;
};

class Square :public Rectangle1 {
public:
    Square(double l) :Rectangle1(l, l) {
    }
};

class Disc :public Geometry {
public:
    Disc(double r) :r(r) {
    }
    virtual double getArea() {
        return 3.1415926 * r * r;
    }
    virtual double getCircumference() {
        return 2 * 3.1415926 * r;
    }
protected:
    double r;
};
In [4]:
%%cpp
//void main()
{
    Rectangle1 r(2.1, 3.1);
    Square s(3.1);
    Disc d(2.1);

    cout << r.getArea() << endl;
    cout << r.getCircumference() << endl;
    cout << endl;

    cout << s.getArea() << endl;
    cout << s.getCircumference() << endl;
    cout << endl;

    cout << d.getArea() << endl;
    cout << d.getCircumference() << endl;

    std::map<std::string,std::shared_ptr<Geometry>> m;

    m.insert({ "Disc",std::make_shared<Disc>(2.1) });
    m.insert({ "Rectangle",std::make_shared<Rectangle1>(2.1, 3.1) });
    m.insert({ "Square",std::make_shared<Square>(3.1) });
    std::cout << m["Square"]->getArea() << std::endl;
    std::cout << m.at("Square")->getArea() << std::endl;
    m["test"] = std::make_shared<Square>(32.1);
    for (const auto & el : m)
    {
        std::cout << el.first<<":"<< el.second->getArea()<< std::endl;
    }
    
}
6.51
10.4

9.61
12.4

13.8544
13.1947
9.61
9.61
Disc:13.8544
Rectangle:6.51
Square:9.61
test:1030.41

javascript¶

In [5]:
%%js //the next line is only necessary in jupyter notebooks
element.setAttribute('style', 'white-space: pre;');console.log=function(text){element.textContent+=text+"\n"}

class Geometry{
    constructor() {
        if (this.constructor == Geometry) {
            throw new Error("Abstract classes can't be instantiated.");
        }
    }
    getArea(){
        throw new Error("Method 'getArea()' must be implemented.");
    }
    getCircumference(){
        throw new Error("Method 'getCircumference()' must be implemented.");
    }
}

class Rectangle extends Geometry{
    constructor(width, height){
        super();
        this.width=width
        this.height=height
    }
    getArea(){
        return this.width*this.height;
    }
    getCircumference(){
        return 2*(this.width+this.height);
    }
}

class Square extends Rectangle{
     constructor(l){
        super(l,l);
    }
}

class Disc extends Geometry{
     constructor(r){
        super();
        this.r=r;
    }
    getArea(){
        return Math.PI*this.r*this.r;
    }
    getCircumference(){
        return 2*Math.PI*this.r;
    }
}

let m=new Map()

m.set("Rectangle",new Rectangle(2.1,3.1))
m.set("Square",new Square(3.1))
m.set("Disc",new Disc(2.1))

console.log(m.get("Disc").getArea())

for(let i of m){
    console.log(i[0])
    console.log(i[1].getArea())
}

Python¶

In [6]:
from abc import ABC, abstractmethod
import math

class Geometry(ABC):
    @abstractmethod
    def getArea(self):
        pass
    def getCircumference(self):
        pass

class Rectangle(Geometry):
    def __init__(self,width, height):
        self.width=width
        self.height=height
        super().__init__()
    def getArea(self):
        return self.width*self.height
    def getCircumference(self):
        return 2*(self.width+self.height)

class Square(Rectangle):
    def __init__(self,l):
        super().__init__(l,l);

class Disc(Geometry):
    def __init__(self,r):
        self.r=r
        super().__init__();
    def getArea(self):
        return math.pi*self.r*self.r
    def getCircumference(self):
        return 2*math.pi*self.r;

m={}
m["Rectangle"]=Rectangle(2.1,3.1)
m["Square"]=Square(3.1)
m["Disc"]=Disc(2.1)

print(m["Disc"].getArea())
for key in m:
    print(key,m[key].getArea())

for key, value in m.items():
    print(key,value.getArea())
    
13.854423602330987
Rectangle 6.510000000000001
Square 9.610000000000001
Disc 13.854423602330987
Rectangle 6.510000000000001
Square 9.610000000000001
Disc 13.854423602330987

Fortran¶

Fortran does not have map containers.