/dpt/ - Daily Programming Thread

No time for love edition.

Previous thread: What are you working on Sup Forums?

Other urls found in this thread:

codeproject.com/articles/15460/c-image-to-byte-array-and-byte-array-to-image-conv
en.wikipedia.org/wiki/Rational_agent
en.wikipedia.org/wiki/Epistemic_modal_logic
en.wikipedia.org/wiki/Dynamic_epistemic_logic
en.wikipedia.org/wiki/Game_theory
humblebundle.com/books/java-books
youtube.com/watch?v=AqDsso3S5fg
twitter.com/SFWRedditGifs

>anime
thanks

programming is great

I'm working on a school project, I'm actually really enjoying my classes.

But I have a question that my professor won't fuckin respond to and it's due on Tuesday so I don't have a lot of time.

I'm working on making a BST structure in Java that features lazy deletion of tree nodes. Doing so causes findMin and findMax to have to be implemented recursively. Those methods also now have to check the opposite child as well.

The assignment sheet says that findMin and findMax have to stay O(log N) time, which I'm finding pretty impossible because you can't avoid having to search the opposite side of the tree if the left or root node is deleted.

Am I retarded, or am I actually right that findMin and findMax will have a worst case of O(N) no matter what? (note that average case is still log N)

What namespaces do I need to get this to work? codeproject.com/articles/15460/c-image-to-byte-array-and-byte-array-to-image-conv
I have:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Drawing;

using namespace std;

why are findMin and findMax deleting anything?

should just be simple
>go down left subtree
>go down right subtree
O(h) where h is height of tree
if the height is log(n) then O(log(n))
(in a balanced tree it is)

I'm on problem 7 in project euler. I'm learning C as I do this. I'm gonna try this one using pointer math and scoping my constants as enums rather than #defines

/*******************************************************************************
P R O B L E M 7 F U N C T I O N D E F I N I T I O N S

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that
the 6th prime is 13.

What is the 10 001st prime number?
*******************************************************************************/

long long problem7(void)
{
enum constants{
NUMPRIMES = 20, // # of primes to calculate
ENDINGS = 4 // # of candidate values
};

/* First three are initialized since most primes dont end with 2 or 5 */
long long primeAr[NUMPRIMES] = {0};
primeAr[0] = 2;
primeAr[1] = 3;
primeAr[2] = 5;
long long* prime = &primeAr[3];
long long testNum = 0;
int endingsAr[ENDINGS] = {1, 3, 7, 9}; // Prime #s endings
int* end = &endingsAr[0];
long long ten = 0;
bool div = false;

printArr( (prime - 3), NUMPRIMES);

return 0;
}

void printArr(long long array[], long long arrSize)
{
for(int i = 0; i < arrSize; ++i){
printf("\n Index = %-3d, Value = %-3lld", i, array[i]);
}
}

Please help noobie :^(
#!/bin/sh

COLORS=(Red Orange Yellow Green)
NUMBERS=(24 532 2 834)

for (( i = 0 ; i < ${#COLORS[@]} ; i++)); do
echo "${COLORS[$i]} ${NUMBERS[$i]}"
done

## Output
# Red 24
# Orange 532
# Yellow 2
# Green 834


Is it possible to get the same output if the variables were strings and without parameter expansion, sh built-ins, or other POSIX utilities?

I first tried nested for loops, but I knew this wouldn't work:
#!/bin/sh

COLORS="Red Orange Yellow Green"
NUMBERS="24 532 2 834"

for color in $COLORS; do
for number in $NUMBERS; do
echo "$color $number"
done
done

## Output
# Red 24
# Red 532
# Red 2
# Red 834
# Orange 24
# Orange 532
# ...
# Green 2
# Green 834

I haven't found a solution in the man pages or on the Internet. Must I just stick with array indexing?

I like how people actually wait for the old thread to nearly die before posting a new one nowadays.
Did all of the stupid fucking autists graduate or something?

Learning OpenGL. I don't understand what clip space is or how 3D objects use it to appear 3D, tutorial isn't really helping with explaining this. Plz help I'm making progress on it I just don't understand this particular part.

I'm enjoying learning C++

findMin/Max are not deleting anything.

In a tree where nodes are deleted for real (as in, node gets removed and shit gets percolated and balance), findMin only ever has to go left. findMax just has to go right.

But in a tree with lazily deleted nodes you might have something that looks like pic related. Grey nodes are inactive nodes, i.e. nodes that have been lazily deleted.

the minimum node of this tree is 3, not 5. But to get there, you would have to check for nodes on the right as well.

FuckX.h:
Fuck Apple
Fuck microsoft
Fuck windows
Fuck WinAPI
Fuck OpenGL
Fuck Ajit Pai

Since stackoverflow has perl has their most hated language, I'm learning perl to troll the shit out of them.

just add a bit to each node?

What do you mean?

Is there an equivalent to the dragon book machine learning/optimization? I'm not looking for anything advanced or cutting edge, but rather an introduction to the field.

I've gotten votes for Deep Learning (Goodfellow) and neuralnetworksanddeeplearning.com .. any others I should consider?

>lazy deletion of tree nodes
sure
>findMin and findMax recursive
as always

>have to check the opposite child
Why would it have to do that?
Has your professor explicitly defined that lazy node deletion means that the node is unavailable for search?

You also have to consider that ``lazy'' anything also implies that you eventually have to actually do the work (garbage collection) by including a phase which O(N) gathers all nodes that are not ``deleted'' and makes a new tree out of them.

It sounds like ``a lot of work'', but in reality garbage collection only happens once in a while and the cost can be amortized.

You can prove that findMin and findMax are O(log N) amortized.

/dpt/ i have an idea and since i know this idea makes too much sense to be original i would like to know what it is called and how bad i fucked it up. does anyone know?

here is the gist of it

suppose you are trying to model what someone is thinking about doing but you do not want to go full generative general purpose artificial intelligence with it you just want a simple well behaved non intelligent system to model their thoughts

so what you do is

you have a world model

conditions can be queried or asserted about that world model

some of the conditions are atomic

others are algebraically constructed: either predicates (parametrized over arbitrary objects or arbitrary other conditions and used to express properties of objects or relations between them) or unique or existential or universal quantifiers (parametrized over either one or all objects satisfying some given other condition)

then you add four special predicates to that algebra: predicting and believing and wanting and allowing

predicting is a predicate of a condition which states not that the condition is the case but that it will be the case in the next iteration of the world

believing is a predicate relating a person to a condition which may or may not actually be true but if the person has to do any reasoning he will reason as though it were

wanting is also a predicate relating a person to a condition such that the reasoning the person does will be geared toward deducing the necessary behavior in which they must engage in this iteration of the world to satisfy that condition in the next (obviously at minimum this requires certain beliefs about predictions)

allowing relates a person to a condition and just means the person is capable of instating that condition as a trivial behavior of that person

add a bit to say if it's marked for deletion?

so prolog

i do not know much about prolog but does it have baked-in support for declaring things about what people think / want such that the resulting solution process emulates said people's rationally optimal behavior? because that is the main point of what i am thinking of here

See my badly drawn paint visualization of the tree. You can traverse deleted nodes, but you cannot return it as the minimum or maximum because it technically isn't supposed to be existent to the outside world anymore.

And that's true, like a hash table, the tree would ideally have to be rebuilt at a certain load factor, but for some reason, when I asked my professor about that he said that isn't a concern for this project. Oh well.

That is what lazy deletion is. Greyed out nodes = marked as deleted. Lazy deletion just means "we didn't actually get rid of the info yet, but we're gonna tell the outside world that we did and function like we did it."

lazy deletion in what sense?
lazily deleting the data or lazily deleting the node?

When Sup Forums wants to make an equation
Are You use an IDE or you use a calculator or excel like programms?

what you are asking for is so extremely poorly defined that you need to start with something like prolog to learn why what you want is incredibly hard

Can a make_unique variable be safely passed to another thread?

whats the point of singletons when you can just make everything static?

cool thanks

it does not seem that hard though

hard for my computer perhaps

but not hard for me as the person defining the rules for my computer to follow

unless i am using a language that is not declarative because then it would be unbelievably hard just to even write a program that can deduce the state of a model from nontrivial assertions about it much less find a path to an optimal state given a current state and a goal and a few universal rules

How the FUCK do I draw text in modern OpenGL? On Windows btw.

Is there a less autistic way than using a bitmap font?

Working on a Brainfuck interpreter for my senior project. Obviously the interpreter itself is pretty easy, so main work is on a shell and two debuggers, made with curses and GTK.
Currently on the second debugger, turns out GTK is pretty fun to use, you can do some cool shit.
Pic is what I have of it so far.

Introduction to Statistical Learning.

Neural networks can be highly effective, and they're definitely used in several cutting edge applications right now, but they're not the only machine learning technique available, and they tend to converge slower than other methods. You should try to develop an understanding of other models so you can better see their analogs with modern techniques.

If you are not willing to do the amortized analysis of findmin/max with garbage collection, then my suggestion to you is to simply jiggle the tree every time a node is lazily deleted.
In this tree when 2 is ``deleted'', you should make 3 the parent of 2, and then swap the positions of 2 and 1.
In this way, after continuous deletion, you will end up with a tree composed of dead node icicles which never need to get traversed. Your dead stuff settles to the bottom.

When a node gets deleted, it's rather problematic because

whats the point of either when you can just use top-level static?

the only reason mutable globals are bad is because foreign code can access and change them out of context, even / especially on accident. top-level static circumvents this problem and makes it so there is literally nothing wrong with mutable globals

better yet you don't even need a class, you can just have a struct of pointers to static functions that operate on the static mutable globals

>how is that different from a regular delete
it isn't lel

So, I'm building a proxy as a uni project, and it requires that multiple clients can access it at the same time. Do I need to assign a new port for each one of them or can I roll with a single port for all of them?
It's in C, using sockets

Shitlangs (non-exhaustive list):
>C++
>Java
>C#
>Python
>Ruby
>JavaScript
>PHP
>Perl
>Groovy
>Scala
>Kotlin
>Go
>BASIC
>COBOL

Fuckoff C89 suckless faggot

One port.

Have one socket listening to the port.
When it gets a message, move the connection to a thread.
One thread per established connection, one thread accepting new connections.

i dont like it because i have to do
MyClass.getInstance().foo();

when i could just import static and do
foo();

Thanks senpai

(cont'd)
>Haskell
>Idris
>ML
>OCaml by extent
>Lisp
>Scheme
>Lua
>Literally any version of assembly or machine code
>(Especially JVM bytecode)
>C
>Objective-C
>Swift
>Integer BASIC
>Visual BASIC
>Visual C++
>D
>B
>BCPL
>Prolog
>Ada
>Erlang

Yes, you can move it between threads.

>not the only machine learning technique available
>develop an understanding of other models

That sounds exactly like what I'm looking for - a broad(ish) overview of the field so I can use it as a reference and starting point for further research, but also enough technical depth that I could implement some stuff on my own.

as an alternative to , forking is also a solution

You can use something like FreeType to generate textures programmatically. Under the hood it's still a bitmap font but you don't need to find one or put one together yourself.

>having to move shit around
Then lazy deletion isn't even needed, the point of lazy deletion is that you're significantly simplifying the delete operation at the cost of increasing complexity of the find, contains, etc. operations.

I took a look at freetype, but it seems really annoying to set up, and I hate adding extra dependencies to a project. Especially if the dependency is hard to build/include.

you seem to be having a fundamental misunderstanding
let's take a page out of functional programming
A function with input X should output Y. i.e. f(n) = f(n). At all times.
Having static mutable globals means that other functions can modify those globals and invalidate our convenient arrangement where our functions always provide a deterministic and expected output.

That thing about ``foreign code accessing and changing things out of context'' applies to internal code as well.

I mean, there should be a way to do it with a Windows API but it will be inevitable be a nightmare to use.

But you wouldn't even need MyClass.getInstance().

It would just be MySingleton.foo().

Better yet, instead of making the functions static and exporting a struct full of their addresses, just export the functions themselves in a namespace.

// MySingleton.h
#pragma once
namespace MySingleton {
int foo();
}

// MySingleton.cpp
#include "MySingleton.h"
static int fooValue = 0;
int MySingleton::foo() {
return fooValue++;
}

// main.cpp
#include "MySingleton.h"
using namespace MySingleton;
int main() {
return foo();
}


Or, even better, use a language where you don't need singletons because you don't use OOP, such as Haskell.

You have to move shit around in a lazy binary tree anyways, the difference is that you're allowed to control when you do it.

Pure functional programming with immutable state is outside the scope of this conversation. You should have been clued in as to this fact when you first realized we were talking about OOP.

Functional programming doesn't require immutable state--immutable state is only a convenient way to provide the nuances of functional programming.

And anyways you seem to have totally missed the point about having functions that always do what you expect them to do, internal or external. Congrats man.

The kind of model you're referring to -- a mathematical object that uses boolean calculus to non intelligently simulate intelligent reasoning -- is called a rational agent. It's a model commonly employed in economics and game theory.

>having functions that always do what you expect them to do
If you define always doing what you expect them to do as always returning the same outputs for the same inputs, then my point stands: that's outside the scope of the discussion. And, once again, the fact that we're talking about OOP should have clued you in on that from the get-go.

Aside from the rotations that normally happen in a self balancing tree, nothing else is getting moved around, really...

en.wikipedia.org/wiki/Rational_agent
en.wikipedia.org/wiki/Epistemic_modal_logic
en.wikipedia.org/wiki/Dynamic_epistemic_logic
en.wikipedia.org/wiki/Game_theory
If you genuinely came up with this idea yourself without having heard of any of that stuff, you've unwittingly hit a gold mine. Not only is the idea not original, SO many people have thought of it before you that it's become its own major field of study and thought.

Back to trying to do Haskell and I still can't decide if I'm going about this the wrong way. Right now I'm just trying to compare each file against each other file in a directory and do an operation on them if they're identical to each other. Later I want to add functionality to detect similar images rather than identical files. Is there a good book on functional programming that will help you unlearn imperative programming?

getFiles :: FilePath -> IO ()
getFiles [] = return ()
getFiles dir = do
contents IO (Bool)
eqPairIO f (a, b) = do
fa

Eh, idk how to fork

I don't know how to multithread either, but I asked in here a while ago and got some good help, it seems much easier than forking I think

Why linux kernel not sending any SIGSEGV signal whenever a free()-d memory is accessed?
I found this line of code in my old utility program, and the program able to run smoothly for more than 4 years without any trouble
foo = getnextfoo(); //activate the while loop
while(foo->active)
{
foo = getnextfoo(); //getting next node
free(foo);
}

It's obviously a heap access after free()-d, and the reason it still run is only because free() didn't change the data in heap, but only release it.
But why the linux kernel didn't detect it as memory violation, since the memory already released and not owned by the program?

/dpt/, I've barely learned any new programming in the last seven years. What are some good, online Computer Architectures courses? I want to learn how computers work so I can write actual programs instead of basic shit in sepples and python that's nothing more than wrappers around libraries.

Is Scala worth learning?

no

What if you had a tree of comparisons?

I'm assuming you're referring to more kernel stuff rather than how processors work? Just read stuff like Linux Device Drivers, Linkers and Loaders, Linux Kernel Development. Learn some assembly to figure out what the computer is actually doing behind the curtain.

If you're actually referring to computer architecture, also learn assembly. Then you can read Computer Architecture: A Quantitative Approach and read Agner Fog's manuals on x86 micro-architecture and assembly optimization. Digital Computer Electronics details how to build an "As Simple as Possible" computer using basic electronic components if you're interested in that level. I'd personally recommend you have a project in mind if you want to delve into something. It's pretty much impossible to really learn something without an intent to use it.

It's possible that the memory was still owned by the program. free() doesn't necessarily immediately deallocate memory, it just marks it as eligible for deallocation at the kernel's discretion. The kernel, the runtime, or even a particularly smart compiler, may optimize your program by delaying actual deallocation until multiple freed objects are in sequence and can all be deallocated with just one syscall.

Anyway, don't rely on the OS to verify the correctness of your memory management, because that's not its job. Use valgrind

Whats the best programming language for building my own product as a one man band?

I suppose you could represent the file comparisons as a tree. You could probably write a generic function that traverses the tree and performs some IO action. The main thing I'm having problems with is how to handle the sort of nested "if then else" constructs.

Redoing my server's backup system. So if the worst happens I still got my core data. Layers you know.
1.3TB External - Server Sys Image/Client Backups/X Drive Content
2.Zyzel NSA540 12TB R-5 - Full Server Data Backup + Server Sys Image/Client backups
3.Zyzel NSA320S 4TB R-0 - Film/Music Archive Backup (Last Resort)

I mean like a binary decision tree
you have a true and a false path

Python.

OOP, and interpreted imperative programming in general, is inefficient, inelegant, and for brainlets, but if you're doing everything yourself, you NEED a language that will treat you like a brainlet and do everything for you, because you have got a LOT to do, and at least in the prototyping phase, you won't have the attention span to think carefully about every little thing and work out elegant and provably type-correct solutions that are expressive and concise but not immediately obvious from the problem statement while still keeping the big picture in mind the whole time. Quantity over quality is necessary if you're working alone on a large scale project, otherwise you'll never finish it.

data BD a =
Done
| Split (BD a) (a -> IO Bool) (BD a)
-- a true branch, the function, and a false branch


and for comparisons
Eq a => BD (a,a)

You can add the printing to the comparison function

Is Eclipse a good IDE for Python?

are these books good?
humblebundle.com/books/java-books
if so, what order would a noobie read them in? or at least start off at?

Same guy here, multithread has been implemented.
I need to implement a cache as well, tho.
Some questions about files (in C):
-Can I get the number of files inside a folder? If so, how?
-Can I delete files? If so, how?
-Can I check which file is the oldest inside a folder? If so, how?
everything else is already planned, but the way I was thinking was just saving them all on a folder and deleting the oldest one after the folder has some certain amount of files already saved

Yeah is awesome

Google, its easy.
>Delete files
Google, its really easy
>Oldest file
GOOGLE DAMNIT YOU LAZY FUCK
>cache
use memcached or some similar thing. Or implement your own in memory adt.

YOu can use select() to use the same port, its called a daemon. It allows you to multithread using polling or other types of approaches.

That's pretty interesting, I'll look into it. Also my current implementation eats a ton of RAM so I'll need to do some more thinking on that.

#!/bin/bash

declare -A colors=(
[Red]=24
[Orange]=532
[Yellow]=2
[Green]=834
)

for key in "${!colors[@]}"; do
echo "${key}: ${colors[$key]}"
done

If you really need strings, then just make your array:

colors=(${str})

Which will split from IFS.

malloc/free are just layers on top of os-specific syscalls. Normally they just mmap an anonymous region of memory to use as an arena, growing it as more memory is needed. From the OS's PoV, the pointer likely is still valid since your free just opened up the memory to be reallocated in the same program instead of actually unmapping the memory region.

As another alternative to only use a single thread and set things up so you can use an event loop using poll (or epoll if you're using linux). Place all the file descriptors you care about on the poll, set them up in non-blocking mode, and just block on the epoll fd.

This is how things like nginx work and really is an improvement over the one-thread-per-connection approach in terms of speed and the number of clients you can simultaneously handle.

c++ is the greatest language of all time

Side note: you should almost certainly be doing something like this in this situation.
int getnextfoo(struct foo *p);

struct foo x;
while (getnextfoo(&x)) {
// ...
}

Its no longer summer.

How do I manage a stack that must be accessed by multiple threads?
I tried googling this up, and got an idea with mutexes, but I think I'd need to use [spoiler]global variables[/spoiler].
Is there any better way to do so?
Basically, every thread will push something in the stack when it finishes running, and given the nature of push changes what the stack pointer will be pointing to, when the next one goes to push it will simply "fork" the stack.
Is using a queue the only other alternative? seems very weird to create a queue of things to be pushed in a stack, and I feel I might also get problems if I try to push two things into the queue at the same time.

Use an atomic counter to track the top of the stack.

tfw just watch videos about crazy and retarded things you can do in cpp instead of actually programming in cpp
youtube.com/watch?v=AqDsso3S5fg

I'd get em' just to have them for reference. But if I can give you some advice, books can only do so much. For java just read up and build examples of: if/else, loops, regular expressions, flow, generics, inheritance, data structures (nodes, binary trees, stacks, etc), pass by reference vs pass by value, primitive data types, interfaces, abstract classes, access modifiers, and I'm sure there's plenty I'm missing but these are some core concepts.

But most of all, hands on experience is super valuable. Think of a project and build it. (and have fun doing it damn it!). Google, stack exchange, and a pen and a piece of paper are your best friend.

Good luck friend.

It's okay I do the same thing all the time. I'm also a 'gamedev' and get lost in endless gdc videos and make no progress on my own projects.

i want to create something but i'm not satisfied with any of the ideas i have, i want to do something big and meaningful

This seems like jayden smith's tweets

jaden* smith is alright

I just finished my final C++ project... email server app

>Make a social media app that inflates peoples self worth and vanity.
>Collect $$$
>Give some to user who gave you this wise advice

thats the opposite of what i want to do

>crazy and retarded things
So just writing normal C++?

God, I think C++11 already retarded.
This just bring the term retarded to another level

programming and trying to manage an entire project with vim is so fucking slow no wonder people use visual studio