10 votes

Fortnightly Programming Q&A Thread

General Programming Q&A thread! Ask any questions about programming, answer the questions of other users, or post suggestions for future threads.

Don't forget to format your code using the triple backticks or tildes:

Here is my schema:

```sql
CREATE TABLE article_to_warehouse (
  article_id   INTEGER
, warehouse_id INTEGER
)
;
```

How do I add a `UNIQUE` constraint?

3 comments

  1. [2]
    gpl
    Link
    What is the "proper" way to handle this situation? I have a C++ class that essentially does some Fourier transforms on some data. As an example, you can think of it as a solver class for the...

    What is the "proper" way to handle this situation?

    I have a C++ class that essentially does some Fourier transforms on some data. As an example, you can think of it as a solver class for the Poisson equation, which solves it by using the Green's function. That's irrelevant to my question but sets the stage. Now, I am using FFTW to do Fourier transforms, and FFTW creates an object called a "plan" which optimizes the FFT. It needs an allocated array in order to create this object, and this object is used to actually compute the FFT. An example from the docs is:

    #include <fftw3.h>
    ...
    {
        fftw_complex *in, *out;
        fftw_plan p;
        ...
        in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
        out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
        p = fftw_plan_dft_1d(N, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
        ...
        fftw_execute(p); /* repeat as needed */
        ...
        fftw_destroy_plan(p);
        fftw_free(in); fftw_free(out);
    }
    

    Here, every time fftw_execute is called, the data in in is Fourier transformed and place in out.

    Now, for my use case, I want to create a class that has these plans has member objects and are all set up to preform the Fourier transform on arrays I pass to it. The reason for this is there are other actions I want to preform on the data while it is transformed and before inverting the transform, for example if I want to convolve a function by multiplying the transform by another function. There are also objects I need that I don't need outside of the solver for this, like a grid of frequencies in Fourier space. So for these reasons I think it makes sense to make a class. Schematically I will have a class that does something like:

    class SolverClass {
        public:
            SolverClass(std::complex<double> *in, /*other input parameters*/){
               initialize(in);
            }
            void fft_forward(std::complex<double>* in); // calls fftw_execute on the forward plan 
           void initialize(){
               /* code like the block above setting up plans */
            }
    
        protected:
               fftw_plan forward_transform;
               std::complex<double>* buf1;
               /* other stuff */   
    

    That's all the backstory. My question is this. In order to set up the internal FFT plans, I need to have buffers for the input and output array. I could copy my input and output arrays to internal member copies, construct the plan using those allocations, and copy the data back out using member functions when I need to access it outside of the class. Or, I could just pass pointers to the input and output array to the class, and initialize the FFT plan using pointers to those external arrays. In the first instance, everything used by the class is contained by the class and there's good encapsulation, but I will need to copy the data whenever I need it outside the class, which at the least will occur for I/O. I also will have basically double the memory allocated at any time that I need since the copies sitting outside the class won't be used while the solver is doing its solving. In the second option, though, I have pointers and member objects that are pointing to memory allocated outside of the class and not owned by it. Now, I don't think this will cause any problems, and I am currently planning on going this route. But I am wondering if there are any standards or good practices I should follow in this case. Is it bad to have pointers within a class pointing to memory not owned by the class? Should I be managing this in a different way? Am I overthinking things?

    EDIT: I also want to acknowledge the past very useful answers I have gotten in these threads which I do not always have the time or energy to respond to. If you've responded to me here before please know it is very much appreciated and I haven't ignored it - sometimes I just don't have the bandwidth to follow up even when I initiate the thread.

    5 votes
    1. joplin
      Link Parent
      If it were me, I would use shared pointers to the memory. That way it doesn't really matter who owns it. Whoever lets go of it last is the one who frees it and it's guaranteed to stick around as...

      If it were me, I would use shared pointers to the memory. That way it doesn't really matter who owns it. Whoever lets go of it last is the one who frees it and it's guaranteed to stick around as long as someone's still got a shared pointer that points at it.

      One other thing to watch out for, is if you are using the buffers on multiple threads, you'll want to make sure you're not reading data in a given buffer while also writing data to the same buffer. If you made copies you wouldn't have to worry about that, but as you say, that's slower and uses more memory, so best to use some sort of synchronization object like a mutex or similar with the shared_ptr I recommended above.

      5 votes
  2. wirelyre
    Link
    Does anyone know of a good introduction / overview of LL parsers and table construction? I've got a library nearby, so I can go get a textbook if it has pictures, but I don't want to wade through...

    Does anyone know of a good introduction / overview of LL parsers and table construction?

    I've got a library nearby, so I can go get a textbook if it has pictures, but I don't want to wade through pages and pages of Greek letters and follow sets to get the intuition. I already did that with LR.

    2 votes