What syntax does PostgreSQL require when all identifiers in a query are dynamic?

Example:

SELECT $1 FROM $2.$3 WHERE $1 = $4

Result:

ERROR:  syntax error at or near "$2"

Example:

SELECT "$1" FROM "$2"."$3" WHERE "$1" = "$4"

Result:

ERROR:  relation "$2.$3" does not exist

If you wonder why all the identifiers are parameterized, it’s because I’m designing a database manager tool which dynamically allows me to edit any “cell” in any table.

algorithms – Dynamic programming for graph splitting

I have a graph which has edges between every vertices $i$ and $j$ such
that $i < j$. I need to divide the graph into two parts such that the
combined weight of the edges is minimum. For example there is a graph
of 4 vertices where each edge weight is the following

  • $w(1, 3) = 2$
  • $w(1, 4) = 1$
  • $w(3, 4) = 3$
  • $w(1, 2) = 100$
  • $w(2, 3) = 100$
  • $w(2, 4) = 100$

The solution would be ${2}, {1, 3, 4}$.

The algorithm I came up was a brute force i.e. considering each
combination and return the minimum of it i.e. to take one vertex in one
group or include in other which leads to complexity $O(2^n)$.

I want to get the above problem to run in $O(n^2)$ solution. I did draw
a decision tree for the above problem and can see nodes being overlapped
i.e. ${1,2}, {3,4}$ and ${3,4}, {1,2}$ so I figured this is a
dynamic programming problem but I am not able to define the states or
maybe it is not a DP problem.

storage – Unisphere dynamic pools. Does this mean I can lose more than 1 disk with a raid 5 configuration and 20 drives or how are spare space extents treated?

So emc has implemented dynamic pools which do away with hot spares and instead uses some drive extents as spare space on each drive.

When a drive fails, the contents are copied into this spare space (is it usable living on this spare space?).

When setting up a traditional system, you would normally have a couple hot spares so if a drive fails, it rebuilds onto these spares thus saving you a failure. (Raid 5 with a couple hot spares allows you to lose 3 drives at seperate times i believe).

With this dynamic pool, how does this work? If I have a 20 drive system and use raid 5 on a dynamic pool…what number of drives can I lose (at different times of course) and still be operational?

database – Event sourcing with dynamic queries

  • I have a large database of users
  • I have a way for end users to segment these users by creating filters, i.e. last seen 3 days ago. last browser safari etc.

I need to detect when users enter these segments. This needs to be performant as I may have many of these filters, which are dynamically defined and millions of users, so running a query whenever user(s) change will cause significant db load in a naive implementation.

I’m looking for architectural ideas for how to solve this problem, or event the name of the problem I’m trying to solve here. Is this event sourcing or better described as something else. Is such a problem handled with specific tools, or are there patterns I could use to solve this alone.

beginner – dynamic queue implementation in C++

Hello everyone I’ve implemented a dynamic queue along side with a randkm access iterator, can I get a review ? And thanks in advance.

iterator:

#pragma once
#include <iterator>

namespace con {
template <class T> class rnd_iterator {
  T *m_Ptr;

public:
  /*
   * type aliases
   */
  using value_type = T;
  using iterator_type = rnd_iterator<value_type>;
  using iterator_category = std::random_access_iterator_tag;
  using pointer = value_type *;
  using const_pointer = const pointer;
  using difference_type = std::ptrdiff_t;
  using reference = value_type &;
  using const_reference = const value_type &;
  /*
   * constructors
   */
  rnd_iterator(const rnd_iterator<T> &other) noexcept : m_Ptr(other.m_Ptr) {}
  rnd_iterator(T *p) noexcept : m_Ptr(p) {}
  /*
   * access operators
   */
  reference operator*() { return *m_Ptr; }
  reference operator()(std::size_t idx) { return m_Ptr(idx); }
  pointer operator->() { return m_Ptr; }
  /*
   * increment/decrement and assign operators
   */
  rnd_iterator &operator=(pointer oth) {
    m_Ptr = oth;
    return *this;
  }
  rnd_iterator &operator+=(pointer oth) {
    m_Ptr += oth;
    return *this;
  }
  rnd_iterator &operator-=(pointer oth) {
    m_Ptr -= oth;
    return *this;
  }
  iterator_type &operator=(const iterator_type &rhs) {
    m_Ptr = rhs.m_Ptr;
    return *this;
  }
  friend iterator_type &operator+=(const iterator_type &lhs,
                                   const iterator_type &rhs) {
    lhs.m_Ptr += rhs.m_Ptr;
    return lhs;
  }
  friend iterator_type &operator-=(const iterator_type &lhs,
                                   const iterator_type &rhs) {
    lhs.m_Ptr -= rhs.m_Ptr;
    return lhs;
  }
  rnd_iterator operator++() {
    ++m_Ptr;
    return *this;
  }
  rnd_iterator operator++(int) {
    auto temp = *this;
    m_Ptr++;
    return temp;
  }
  rnd_iterator &operator--() {
    --m_Ptr;
    return *this;
  }
  rnd_iterator operator--(int) {
    auto temp = *this;
    m_Ptr--;
    return temp;
  }
  /*
   * comparison operators
   */
  friend bool operator!=(const iterator_type &lhs, const iterator_type &rhs) {
    return lhs.m_Ptr != rhs.m_Ptr;
  }
  friend bool operator!=(const iterator_type &lhs, pointer rhs) {
    return lhs.m_Ptr != rhs;
  }
  friend bool operator==(const iterator_type &lhs, const iterator_type &rhs) {
    return lhs.m_Ptr == rhs.m_Ptr;
  }
  friend bool operator==(const iterator_type &lhs, pointer rhs) {
    return lhs.m_Ptr == rhs;
  }
  friend bool operator<(const iterator_type &lhs, const iterator_type &rhs) {
    return lhs.m_Ptr < rhs.m_Ptr;
  }
  friend bool operator<(const iterator_type &lhs, pointer rhs) {
    return lhs.m_Ptr < rhs;
  }
  friend bool operator<=(const iterator_type &lhs, const iterator_type &rhs) {
    return lhs.m_Ptr <= rhs.m_Ptr;
  }
  friend bool operator<=(const iterator_type &lhs, pointer rhs) {
    return lhs.m_Ptr <= rhs;
  }
  friend bool operator>(const iterator_type &lhs, const iterator_type &rhs) {
    return lhs.m_Ptr > rhs.m_Ptr;
  }
  friend bool operator>(const iterator_type &lhs, pointer rhs) {
    return lhs.m_Ptr > rhs;
  }
  friend bool operator>=(const iterator_type &lhs, const iterator_type &rhs) {
    return lhs.m_Ptr >= rhs.m_Ptr;
  }
  friend bool operator>=(const iterator_type &lhs, pointer rhs) {
    return lhs.m_Ptr >= rhs;
  }
  friend difference_type operator+(const iterator_type &lhs,
                                   const iterator_type &rhs) {
    return lhs.m_Ptr + rhs.m_Ptr;
  }
  friend difference_type operator+(const iterator_type &lhs, pointer rhs) {
    return lhs.m_Ptr + rhs;
  }
  friend iterator_type operator+(const iterator_type &lhs,
                                 difference_type rhs) {
    return lhs.m_Ptr + rhs;
  }
  friend difference_type operator-(const iterator_type &lhs,
                                   const iterator_type &rhs) {
    return lhs.m_Ptr - rhs.m_Ptr;
  }
  friend iterator_type operator-(const iterator_type &lhs,
                                 difference_type rhs) {
    return lhs.m_Ptr - rhs;
  }
  friend difference_type operator-(const iterator_type &lhs, pointer rhs) {
    return lhs.m_Ptr - rhs;
  }
};
}

queue:

#pragma once
#include "iterator.hpp"
#include <algorithm>
#include <cassert>
#include <initializer_list>
#include <iostream>
#include <iterator>
#include <limits>
#include <memory>
#include <type_traits>
#include <utility>

namespace con {
template <class T, class Allocator = std::allocator<T>> class queue {
  Allocator m_Alloc;
  std::size_t m_Size, m_Capacity;
  std::allocator_traits<Allocator> m_AllocTraits;
  T *m_RawData;
  void m_ReallocAnyway(std::size_t t_NewCapacity) {
    std::size_t f_old = m_Capacity;
    T *f_temp = m_Alloc.allocate(sizeof(T) * t_NewCapacity);
    try {
      for (std::size_t i = 0; i < m_Size; i++) {
        new (&f_temp(i)) T(std::move_if_noexcept(m_RawData(i)));
        m_AllocTraits.destroy(m_Alloc, std::addressof(m_RawData(i)));
      }
      m_Alloc.deallocate(m_RawData, f_old);
      m_RawData = f_temp;
    } catch (const std::exception &exc) {
      m_Alloc.deallocate(f_temp, sizeof(T) * t_NewCapacity);
      throw std::move(exc);
    }
  }
  void m_Realloc(std::size_t t_NewCapacity) {
    if (t_NewCapacity > m_Capacity) {
      m_ReallocAnyway(t_NewCapacity);
    } else {
      return;
    }
  }
  void m_ShiftToLeft() {
    for (std::size_t i = 0; i < m_Size; i++) {
      new (&m_RawData(i)) T(std::move_if_noexcept(m_RawData(i + 1)));
    }
  }
  template <class F>
  void m_ShiftFromTo(std::size_t from, std::size_t to, F &&func) {
    for (; from < to; from++) {
      new (&m_RawData(from))
          T(std::move_if_noexcept(m_RawData(func(from, to))));
    }
  }
  template <class It> void m_ShiftRangeFromTo(It from, It to) {
    for (; from != to; from++) {
      new (std::addressof(*from))
          T(std::move_if_noexcept(*(from + (to - from))));
    }
  }
  template <class Iter> void m_DestroyRange(Iter beg, Iter end) {
    for (; beg != end; beg++) {
      m_AllocTraits.destroy(m_Alloc, std::addressof(*beg));
    }
  }
  void m_CheckOrAlloc(std::size_t t_Size) {
    if (t_Size >= m_Capacity) {
      m_Realloc(m_Capacity * 2);
    }
  }

public:
  using value_type = T;
  using allocator_type = Allocator;
  using size_type = decltype(m_Size);
  using difference_type = std::ptrdiff_t;
  using reference = value_type &;
  using const_reference = const value_type &;
  using pointer = typename std::allocator_traits<Allocator>::pointer;
  using const_pointer =
      typename std::allocator_traits<Allocator>::const_pointer;
  using iterator = con::rnd_iterator<value_type>;
  using const_iterator = const iterator;
  using reverse_iterator = std::reverse_iterator<iterator>;
  using const_reverse_iterator = std::reverse_iterator<const_iterator>;

  explicit queue(size_type cap = (sizeof(value_type) * 5),
                 const Allocator &alloc = Allocator{}) noexcept
      : m_Alloc(alloc), m_Size(0), m_Capacity(cap),
        m_RawData(m_Alloc.allocate(m_Capacity)) {}
  explicit queue(const std::initializer_list<T> &init,
                 const Allocator &alloc = Allocator{}) noexcept
      : m_Alloc(alloc), m_Size(init.size()), m_Capacity(sizeof(value_type) * 5),
        m_RawData(m_Alloc.allocate(m_Capacity)) {
    m_Size = init.size();
    m_CheckOrAlloc(m_Size);
    std::uninitialized_copy(init.begin(), init.end(), m_RawData);
  }
  explicit queue(const queue<value_type> &oth) : queue() {
    if (std::is_destructible<value_type>::value)
      clear();
    m_Size = oth.size();
    m_CheckOrAlloc(m_Size);
    std::uninitialized_copy(oth.begin(), oth.end(), m_RawData);
  }
  explicit queue(queue<value_type> &&oth) noexcept : queue() {
    if (std::is_destructible<value_type>::value)
      clear();
    m_Size = oth.size();
    m_CheckOrAlloc(m_Size);
    std::uninitialized_move(oth.begin(), oth.end(), m_RawData);
  }
  template <class It> queue(It begin, It end) noexcept : queue() {
    assert(begin <= end);
    size_type f_size = std::distance(begin, end);
    m_CheckOrAlloc(f_size);
    m_Size = f_size;
    std::uninitialized_copy(begin, end, m_RawData);
  }
  explicit queue(const queue<value_type> &&oth) = delete;
  iterator begin() noexcept { return iterator(m_RawData); }
  iterator end() noexcept { return iterator(m_RawData + size()); }
  reverse_iterator rbegin() noexcept { return reverse_iterator(end()); }
  reverse_iterator rend() noexcept { return reverse_iterator(begin()); }

  const_iterator begin() const noexcept { return const_iterator(m_RawData); }
  const_iterator end() const noexcept {
    return const_iterator(m_RawData + size());
  }
  const_reverse_iterator rbegin() const noexcept {
    return const_reverse_iterator(m_RawData + size());
  }
  const_reverse_iterator rend() const noexcept {
    return const_reverse_iterator(m_RawData);
  }

  const_iterator cbegin() const noexcept { return const_iterator(m_RawData); }
  const_iterator cend() const noexcept {
    return const_iterator(m_RawData + size());
  }
  const_reverse_iterator crbegin() const noexcept { return rbegin(); }
  const_reverse_iterator crend() const noexcept { rend(); }

  bool empty() const noexcept { return size() == 0; }
  size_type size() const noexcept { return m_Size; }
  size_type capacity() const noexcept { return m_Capacity; }
  size_type max_capacity() const noexcept {
    return std::numeric_limits<size_type>::max();
  }
  const_pointer data() const { return m_RawData; }
  void clear() requires(std::is_destructible<value_type>::value) {
    for (size_type i = 0; i < size(); i++) {
      m_AllocTraits.destroy(m_Alloc, std::addressof(m_RawData(i)));
    }
    m_Size = 0;
  }
  void reserve(size_type cp) { m_CheckOrAlloc(cp); }
  void resize(size_type sz) {
    m_Size = sz;
    m_CheckOrAlloc(sz);
  }
  void erase(iterator val) {
    if (val != end()) {
      difference_type x = val - begin();
      pointer p = m_RawData + x;
      m_AllocTraits.destroy(m_Alloc, std::addressof(*val));
      m_ShiftFromTo(std::distance(begin(), iterator(p)), size(),
                    ()(auto l, ((maybe_unused)) auto _) { return l + 1; });
      m_Size--;
    } else {
      return;
    }
  }
  void erase(iterator first, iterator last) {
    assert(first <= last && "queue::erase invalid range");
    m_DestroyRange(first, last);
    m_ShiftRangeFromTo(first, last);
    m_Size -= std::distance(first, last);
  }
  void erase(reverse_iterator first, reverse_iterator last) {
    assert(first <= last && "queue::erase invalid range");
    m_DestroyRange(first, last);
    m_ShiftRangeFromTo(first, last);
    m_Size -= std::distance(first, last);
  }
  void erase(reverse_iterator val) {
    if (val != rend()) {
      m_AllocTraits.destroy(m_Alloc, std::addressof(*val));
      m_ShiftFromTo(std::distance(val, rend()) - 1, size(),
                    ()(auto l, ((maybe_unused)) auto _) { return l + 1; });
      m_Size--;
    } else {
      return;
    }
  }
  void erase(const value_type &obj) { erase(std::find(begin(), end(), obj)); }
  void rerase(const value_type &obj) {
    erase(std::find(rbegin(), rend(), obj));
  }
  iterator find(const value_type &obj) {
    return std::find(begin(), end(), obj);
  }
  reverse_iterator rfind(const value_type &obj) {
    return std::find(rbegin(), rend(), obj);
  }
  const_iterator find(const_reference obj) const {
    return std::find(begin(), end(), obj);
  }
  const_reverse_iterator rfind(const value_type &obj) const {
    return std::find(rbegin(), rend(), obj);
  }

  void enqueue(const value_type &oth) requires(
      std::is_copy_constructible<value_type>::value) {
    m_CheckOrAlloc(size());
    new (&m_RawData(m_Size++)) value_type(oth);
  }
  void enqueue(value_type &&oth) requires(
      std::is_move_constructible<value_type>::value) {
    m_CheckOrAlloc(size());
    new (&m_RawData(m_Size++)) value_type(std::move(oth));
  }
  ((nodiscard)) value_type
  dequeue() requires(std::is_destructible<value_type>::value) {
    --m_Size;
    value_type temp = m_RawData(0);
    m_AllocTraits.destory(m_Alloc, std::addressof(m_RawData(0)));
    m_ShiftToLeft();
    return temp;
  }
  template <class... Args> void emplace(Args &&...args) {
    enqueue(value_type(std::forward<Args>(args)...));
  }
  value_type at(size_type index) const {
    if (index >= size()) {
      throw std::range_error("out of bounds queue"); // yes helpful error
    } else {
      return m_RawData(index);
    }
  }
  reference at(size_type index) {
    if (index >= size()) {
      throw std::range_error("out of bounds queue"); // yes helpful error
    } else {
      return m_RawData(index);
    }
  }
  value_type operator()(size_type index) const { return m_RawData(index); }
  reference operator()(size_type index) { return m_RawData(index); }

  queue<value_type> &operator=(const queue<value_type> &oth) {
    if (&oth != this) {
      clear();
      m_Size = oth.size();
      m_CheckOrAlloc(m_Size);
      std::uninitialized_copy(oth.begin(), oth.end(), m_RawData);
    }
    return *this;
  }
  queue<value_type> &operator=(queue<value_type> &&oth) {
    if (&oth != this) {
      clear();
      m_Size = oth.size();
      m_CheckOrAlloc(m_Size);
      std::uninitialized_move(oth.begin(), oth.end(), m_RawData);
      oth.~queue();
    }
    return *this;
  }
  queue<value_type> &operator=(const queue<value_type> &&oth) = delete;
  ~queue() {
    m_Alloc.deallocate(m_RawData, m_Capacity);
    std::exchange(m_RawData, nullptr);
    std::exchange(m_Size, 0);
  }
  ~queue() requires(std::is_destructible<value_type>::value) {
    clear();
    m_Alloc.deallocate(m_RawData, m_Capacity);
    std::exchange(m_RawData, nullptr);
    std::exchange(m_Size, 0);
  }
};
}
```

reactjs – React 17.0.1 build a dynamic editable table

Here is my array of objects,

(
    {
        "Sno": "1",
        "First Name": "name",
        "Last Name": "las2t name",
        "Email": "test@gmail.com",
        "Amount": "2000"
    },
    {
        "Sno": "2",
        "First Name": "first name",
        "Last Name": "last name",
        "Email": "test2@gmail.com",
        "Amount": "2000"
    }
)

I want to create an editable table and store it back as an array object, I only need few of the data to editable (Email and Amount).

I would need your guide on the how to achieve this, are there any packages ?

algorithm – Assessing BigO/small o for forming a dynamic dictionary based on searched key and values pair

I am trying to create a dictionary with a file containing text based on a matched pattern. Lines containing key_str should become keys and subsequent lines not matching key_str should become values and get associated with keys in the dictionary. so i have below code working: But I need help and getting the Big O analysis. How could i say my logic is worth case, Best Case or good case? Also, which case is small o and which is big O

File: file2dict.result-soa1

ml1
/var
/home

cpuml2
/var
/home


Output

my_dict: {ml1: ('/var','/home'), cpuml2: ('/var','/home')}

Code:

import os
homedir = os.environ.get('HOME')

key_str = "ml"
my_dict = {}
val_list = ()
key = ''
with open(homedir + '/backup/file2dict.result-soa1') as file2dict:
    for line in file2dict:
        words = line.split()
        for aWord in words:
            if key_str in aWord:
                if key:
                    my_dict(key) = val_list
                    val_list = ()
                    key = aWord
                else:
                    key = aWord
            else:
                val_list.append(aWord)
    my_dict(key) = val_list
print(my_dict)

Looking for details on Dynamic Range vs ISO

I have been reading up on maximizing dynamic range of digital sensors in order to get a rich image and soft roll-offs. One thing that I cannot wrap my head around are the following concepts and the combination of them:

  1. Digital sensors tend to push multiple steps of dynamic range below 18% grey when the ISO is decreased. To maximize dynamic range in both shadows and highlights and get softer roll-off it seems right to shoot at higher ISO.

enter image description here

  1. Simultaneously, there seems to be a pretty wide consensus that exposing to the right (ETTR) yields best results in terms of dynamic range for digital sensors. This indirectly implies lowering the ISO to keep highlight from clipping. Or must ETTR be combined with ND filters som these purposes?
  2. Multiple sources suggest that shooting above native sensor ISO could yield further movement of dynamic range steps above 18% grey – could this really be true?
  3. The chart shown in the image suggests that dynamic range in highlights/shadows is relative to 18% grey. Is that most often true?
  4. This site and many others imply that overall dynamic range is lost at higher ISO: https://photonstophotos.net/Charts/PDR.htm.

The only conclusion I can positively draw is that light scenes should be shot at slightly higher ISO to maximize highlights dynamic range while dark scenes should be shot at low ISO to maximize shadow dynamic range. But I can’t figure out how to apply ETTR technique to this line of reasoning.

I am aware of the confusing nature of the question, but I cannot boil it down more due to my lack of further understanding in the topic. Any input is much appreciated.

dynamic programming – Why longest common substring path reconstruction is wrong!

I’ve implemented longest common substring in python. I know the usual way to reconstruct the path. But, I am reading my algorithm many times, I don’t understand why it doesn’t work for some test-cases. I am trying to construct the solution while the longest path is being made through dynamic programming.

def lcs(s1, s2):
    n = len(s1)
    m = len(s2)
    
    trow = (0) * (m+1)
    table = (trow) * (n+1)
    
    srow = ("") * (m+1)
    solution = (srow) * (n+1)
    
    for i in range(1, n+1):
        for j in range(1, m+1):
            if s1(i-1) == s2(j-1):                
                table(i)(j) = table(i-1)(j-1) + 1
                
                # Adding this character to the solution
                solution(i)(j) = solution(i-1)(j-1) + s1(i-1)
            
            elif table(i)(j-1) > table(i-1)(j):
                table(i)(j) = table(i)(j-1)
                
                # Choosing the solution related to the longer path
                solution(i)(j) = solution(i)(j-1) 
                
            else:
                table(i)(j) = table(i-1)(j)
                
                # Choosing the solution related to the longer path
                solution(i)(j) = solution(i-1)(j) 
    
    return solution(n-1)(m-1)
lcs('amin', 'kahmjiknln')
# Returns 'amin'

lcs('amin', 'aaaakahmjiknln')
# Returns 'aaaaamin'

microsoft excel – Dynamic sheets reference + column ranges using only column number

I have multiple sheets simply named “2019”, “2021”, “2021”.
In each of those, I have columns whose first cell contains a currency code: “EUR”, “XRP”, “XBT”, etc.

Now I wanna create a new sheet in which a dynamic formula would take a year and a currency code as inputs and find the last filled value of the columnt starting with this currency code on the sheet named with the inputed year.

I can find the column number using MATCH(D5;’2021′!$1:$1;0), where D5 contains my currency code, but I don’t know how to make the sheet name dynamic (say I write 2021 in cell A1 on my new sheet, how do I put A1 in this formula instead).

I can find the last value of column O using OFFSET(‘2021’!O1;COUNTA(‘2021’!O:O)-1;0) but I wanna use the column number found previously instead.

I’m scratching my head over this, any help would be appreciated ^^