Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 675 676 [677] 678 679 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 890445 times)

DragonDePlatino

  • Bay Watcher
  • [HABIT:COLLECT_WEALTH]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10140 on: October 21, 2016, 11:48:45 am »

After some googling and testing:
Code: (resizer.bat) [Select]
FOR /R %%image IN (*.png) DO ImageResizer /load "%%image" /resize auto "XBR 2x(hbounds=const, vbounds=const, thresholds=1)" /save "%%~dpnimage_2x.png"

This looks like a great solution! Unfortunately, it gives me the error "%image was unexpected at this time" when I try running it. I don't think my operating system recognizes what "%image" is. Thanks for the help, but I think I'll write a python script instead. ^^; It's good practice anyways.

milo christiansen

  • Bay Watcher
  • Something generic here
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10141 on: October 21, 2016, 12:09:17 pm »

Consider this a Request-For-Comments:

I am vaguely planning on writing an OS and all supporting code as a learning exercise. Basically I plant to take a fairly simple system (probably a raspberry pi) and write an OS, boot loader, several (probably 2) programming languages, bare-metal runtimes for the languages, etc, etc.

I plant to start with IL:A (Implementation Language: A). This will be a simple threaded FORTH-like language designed to be run on bare metal with the express purpose of being easy to implement. IL:A will be written in whatever assembly the target supports, and will be designed to run on the target while being controlled remotely from my development machine.

IL:A will be used to write IL:B. IL:B will be focused on raw performance, with the objective of being used to write the OS kernel.

Once I have IL:B up and running I will use it to write the OS itself, starting with whatever piece I need most at that time. The OS will probably be some kind of micro kernel with as much as possible being run as separate (non-kernel) "service" processes communicating via passing messages through the kernel (possibly with secure semi-shared memory buffers of some kind). Basically the kernel will be a glorified memory manager, message passer, and process scheduler.

Needless to say POSIX compliance will not be considered at all :)

I may or may not get to this any time soon, currently I am in a very early concept stage.
« Last Edit: October 21, 2016, 03:54:33 pm by milo christiansen »
Logged
Rubble 8 - The most powerful modding suite in existence!
After all, coke is for furnaces, not for snorting.
You're not true dwarven royalty unless you own the complete 'Signature Collection' baby-bone bedroom set from NOKEAS

Bumber

  • Bay Watcher
  • REMOVE KOBOLD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10142 on: October 21, 2016, 03:38:13 pm »

After some googling and testing:
Code: (resizer.bat) [Select]
FOR /R %%image IN (*.png) DO ImageResizer /load "%%image" /resize auto "XBR 2x(hbounds=const, vbounds=const, thresholds=1)" /save "%%~dpnimage_2x.png"

This looks like a great solution! Unfortunately, it gives me the error "%image was unexpected at this time" when I try running it. I don't think my operating system recognizes what "%image" is. Thanks for the help, but I think I'll write a python script instead. ^^; It's good practice anyways.
Apparently loop variables can only be a single letter. Try:
Code: (resizer.bat) [Select]
FOR /R %%I IN (*.png) DO ImageResizer /load "%%I" /resize auto "XBR 2x(hbounds=const, vbounds=const, thresholds=1)" /save "%%~dpnI_2x.png"
« Last Edit: October 21, 2016, 03:40:01 pm by Bumber »
Logged
Reading his name would trigger it. Thinking of him would trigger it. No other circumstances would trigger it- it was strictly related to the concept of Bill Clinton entering the conscious mind.

THE xTROLL FUR SOCKx RUSE WAS A........... DISTACTION        the carp HAVE the wagon

A wizard has turned you into a wagon. This was inevitable (Y/y)?

DragonDePlatino

  • Bay Watcher
  • [HABIT:COLLECT_WEALTH]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10143 on: October 21, 2016, 10:17:25 pm »

Thanks! That fixed it up! However, the output is kind of...messy. The batch is spitting out a filename_2x.png next to each filename.png. That's what I asked for, but it's a bit messy in retrospect. It would nice if the program dumped the resized files in a twin directory so I don't have to manually sort through dozens of images.

I think I can take it from here, though. Tutorialspoint has a nice batch tutorial so I'll check that out and use your batch as a starting point. :)

RoguelikeRazuka

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10144 on: October 22, 2016, 09:30:21 am »

Hey I've come here to ask for help from the gurus of python.

The thing is, I have a list (I will call it order) each item of which is the index of an item in another list, let it be T. In other words, the first list basically specifies the order in which we consider the elements of the second list. So, if we wanted to sort the T list, we wouldn't have to operate on T itself, sorting order instead, because there are merely integers, while T may contain whatever sort of things. For example, let T be a list of lists of integers, and we want to sort it by the sums of those lists.

Of course, I can write something like this:

Code: [Select]

row_sums = [0 for i in range(m)]

# there are m lists in T
# each of the lists contains n elements

for i in range(m):
    for j in range(n):
        row_sums[i] += T[i][j]

order = [i for i in range(m)]

# insertion sort
for i in range(1, m):
    el = order[i]
    j = i - 1
    while row_sums[order[j]] < row_sums[el] and j >= 0:
        order[j + 1] = order[j]
        j -= 1
    order[j + 1] = el



It's necessary to change the order in T not sorting T itself. But are there better (and shorter) ways to achieve this than the one given above?
« Last Edit: October 22, 2016, 09:47:35 am by RoguelikeRazuka »
Logged

TheDarkStar

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10145 on: October 22, 2016, 10:45:06 am »

I'm writing a basic matrix multiplication class, but there's something going badly wrong. Namely, I get crashes/segfaults everywhere. Can someone check through my code? I can't figure out what the problem is, but it may be an issue with "=" copying/overwriting unwanted things.

Code: [Select]
class Matrix
{
public:

    double *Data;
    int rows;
    int columns;
    /*Matrix(int rows, int columns)
    {
        Data = new double* [rows];
        for(int i=0;i<columns;i++)
        Data[i]=new double[columns];
    }*/

    Matrix(int initRows, int initColumns):rows(initRows),columns(initColumns)
    {
        Data = new double[rows*columns];
    }

    Matrix()
    {
        rows = 4;
        columns = 4;
        Data = new double[rows*columns];
    }

    double* operator[](int RowChoice)//C++ is silly with operator overloading, so this looks odd.
    {                                //You use standard 2d array syntax (matrix[#][#]) to access it, but really
        return &Data[RowChoice*columns];//only the first one is overloaded and returns a pointer to the
    }                                //(chosen row * column count)th spot in the 2d array.

    Matrix operator*(Matrix Other) // matrix multiplication
    {
        Matrix temp1 = *this;
        if (this->columns != Other.rows)
            throw "Matrix Multiplication Dimension Mismatch";
        Matrix temp(this->rows, Other.columns);
            for(int i=1; i<=Other.columns; i++)
                for(int j=1; j<=this->rows; j++)
                    for(int k=1; k<=Other.rows;k++)
                        {
                            temp[j][i]+=(temp1[j][k])*(Other[k][i]);
                            //std::cout << i << " " << j << " " << k << std::endl;
                            //Draw(temp);
                        }
        return temp;
    }

    Matrix Transpose(Matrix input)
    {
        Matrix temp(input.columns, input.rows);
        for(int i = 1; i <= input.rows; i++)
            for(int j = 1; j <= input.columns; j++)
                temp[i][j] = input[j][i];
        return temp;
    }

    double operator,(Matrix Other) // vector dot product
    {
        Matrix temp = *this;
        if(this->rows != 1 || Other.rows != 1)
            throw "Cannot take the dot product of non-vector matrices!";
        if(this->columns != Other.columns)
            throw "Cannot take the dot product of unequally-sized vectors!";
        Matrix Result = (temp * Transpose(Other));
        return Result[1][1];
    }

    void Draw(Matrix input)
    {
        std::cout << std::endl;
        for(int i=1;i<=input.columns;i++)
            {
                for(int j=1;j<=input.rows;j++)
                    std::cout << input[i][j] << " " << i << " " << j << "  ";
                std::cout << std::endl;
            }
    }

    ~Matrix()
    {
        delete[] Data;
    }
};

And the testing code I'm using:

Code: [Select]
Matrix testing(4,5);
        for(int i=1;i<=5;i++)
            for(int j=1;j<=4;j++)
                testing[i][j]=i+j;
        Matrix testing2(4,4);
        for(int i=1;i<=4;i++)
            for(int j=1;j<=4;j++)
                testing2[i][j]=i+j;
        std::cout << testing[3][2];
        std::cout << testing[1][1];
        testing.Draw(testing);
        testing2 = testing2 * testing;
        testing2.Draw(testing2);
Logged
Don't die; it's bad for your health!

it happened it happened it happen im so hyped to actually get attacked now

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10146 on: October 22, 2016, 10:57:53 am »

I want to sort list X using the order of list Y
A quick Google search suggests this:
Code: [Select]
XsortedbyY = [x for (x,y) in sorted(zip(X,Y), key=lambda pair: pair[1])]


I'm writing a basic matrix multiplication class, but I forgot that C++'s arrays are 0-based
C++'s arrays are 0-based.
Logged

RoguelikeRazuka

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10147 on: October 22, 2016, 11:42:47 am »

I want to sort list X using the order of list Y
A quick Google search suggests this:
Code: [Select]
XsortedbyY = [x for (x,y) in sorted(zip(X,Y), key=lambda pair: pair[1])]

Hm it seems that this piece of code presumes that 'order' has already been sorted as needed, but this is the very thing I want to get done, so in my case it's ineffectual, and it yields sorted T which is not at all required.

(thank you anyway)
« Last Edit: October 22, 2016, 11:46:14 am by RoguelikeRazuka »
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10148 on: October 22, 2016, 12:38:38 pm »

Your analysis about that algorithm is wrong, but so is my interpretation of your question. How about this:
Code: [Select]
f = lambda x: sum(T[x])
ordersortedbyf = sorted(order, key=f)

Logged

RoguelikeRazuka

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10149 on: October 22, 2016, 12:53:47 pm »

Your analysis about that algorithm is wrong, but so is my interpretation of your question. How about this:
Code: [Select]
f = lambda x: sum(T[x])
ordersortedbyf = sorted(order, key=f)

Oh no, your first suggestion was correct, actually, it was I who got mistaken. Thank you again.

PS the second solution turned out to be fruitful as well, good to know of another method of doing the same thing.
« Last Edit: October 22, 2016, 01:07:13 pm by RoguelikeRazuka »
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10150 on: October 22, 2016, 01:28:16 pm »

<snip>

C++ pointer-based arrays start at address zero, not address 1. You're making zero-based arrays then reading them as if they're 1-based arrays, therefore you're writing/reading 1 space over the end, while not actually accessing the data in the first row or column of any field.

« Last Edit: October 22, 2016, 01:48:41 pm by Reelya »
Logged

DragonDePlatino

  • Bay Watcher
  • [HABIT:COLLECT_WEALTH]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10151 on: October 22, 2016, 01:56:54 pm »

What about using an std::vector instead of a pointer to a 2D array? I recently made the switch from C++ to C for game development and while I'm very comfortable with 2D arrays by now, I'm starting to see the advantages of vectors.

If you're manually allocating, indexing and tracking the size of an array like TheDarkStar, std::vector handles that for you. From what I understand, it's just a wrapper for a C-style array that gives you a [] operator. If you avoid resizing your vector or inserting elements at the front, it's exactly as performant as a C-style array (operator overloading notwithstanding, but he seems to be doing that already). Lastly, if you try reading outside of its bounds then it gives you an explicit exception instead of a sinister segfault.
« Last Edit: October 22, 2016, 01:58:52 pm by DragonDePlatino »
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10152 on: October 22, 2016, 03:16:02 pm »

It's better to write your own structure that has an interface similar to std:: stuff. It's tons more compact and efficient. It's good practice to make your own String class that is a replacement for std::string. It saves memory too.

also vector<vector<int>> as a type totally sucks. Wrap it inside a custom class stub at least, so you can easily factor it out later. The problem is the massive overheads, with every row having it's own length value stored, rather than having a global rows/cols for the entire table. That's fine if it's like 1 map array in the whole program, but for a Matrix class where every 3D object has e.g. several matrices, it's an absolute clusterfuck of badly conceived "quick" coding.

e.g. say I write a 4x4 transformation matrix like this:

class Matrix4x4
{
    float data[16];

    // we're doing row major
    float* operator[](int index) // return pointer to
    {
        return (&data) + (index << 2); // bit-shift index two to the right, faster than doing a multiply operation.
    }
};

That's it. Should work perfectly fine for doing the basic stuff. And the 16 floats are packed in memory and take up exactly 64 bytes - which is 1 cache line exactly, so reading the matrix takes up exactly one memory read cycle. With a vector<vector<int>> based "solution" the memory is spread out all over the shop, taking up who knows how many cache reads to read one matrix.
« Last Edit: October 22, 2016, 03:36:17 pm by Reelya »
Logged

miauw62

  • Bay Watcher
  • Every time you get ahead / it's just another hit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10153 on: October 22, 2016, 03:54:29 pm »

It's better to write your own structure that has an interface similar to std:: stuff. It's tons more compact and efficient. It's good practice to make your own String class that is a replacement for std::string. It saves memory too.
Eh, string seems like one of the worse types to do this for. Strings have a *ton* of functionality and they're used a *lot*.

I'd much rather have the extremely reliable STL for something like that, even if it's a little slower. It's better than having bugs.
« Last Edit: October 22, 2016, 05:14:54 pm by miauw62 »
Logged

Quote from: NW_Kohaku
they wouldn't be able to tell the difference between the raving confessions of a mass murdering cannibal from a recipe to bake a pie.
Knowing Belgium, everyone will vote for themselves out of mistrust for anyone else, and some kind of weird direct democracy coalition will need to be formed from 11 million or so individuals.

lethosor

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10154 on: October 22, 2016, 03:54:40 pm »

Quote
bit-shift index two to the right, faster than doing a multiply operation.
If this is the case with your compiler, you need a different compiler. GCC generates a left shift for both "x*4" and "x<<2" without any optimization flags.

Also, I don't think it's "good practice" to re-implement STL stuff all the time unless you have a very good reason and you know how to make your implementation fit your needs better than the STL's implementation. I don't think that's the case here.

I do agree about vector<vector<...>> being less efficient than a flat array. A one-dimensional vector that's treated as a 2D array would be less inefficient, though (but still more overhead than a raw array). I'm not quite sure which one DragonDePlatino was suggesting.
Logged
DFHack - Dwarf Manipulator (Lua) - DF Wiki talk

There was a typo in the siegers' campfire code. When the fires went out, so did the game.
Pages: 1 ... 675 676 [677] 678 679 ... 796