Hot answers tagged

19

Is there a better way to do this? Well, you have dynamic dispatch and yet another scattered island of memory for every single token. That is massively inefficient. A simple way to more efficiency is writing your own mini-VM for executing expressions. You will still only have to parse once, but now all the memory is in a single compact chunk which will be ...


5

Major remark of the code as whole: for some reason you write the unreadable version of array access *(arr+i) all over the place. Don't do that! This makes your code look needlessly obscure and hard to read. Instead use the much more readable arr[i]. Your compare is just a naive implementation of strchr. It would be much more efficient to use strchr. Don't ...


4

I will not try to investigate all different kinds of alternatives, but just try to illuminate what your implementation does. Your code is definitely fine for a quick implementation of something. I have seen code similar to this in production. What you are doing is essentially to create a tree representation of the expression but in such a way that it only ...


3

compare is a dubious name. The purpose of the function is to tell whether parse string contains a character. Consider bool contains(char * str, char ch); The special case does not check for a possible overflow. temp_matrix_length could reach temp_matrix_size by the time it is executed. I do not endorse a pointer notation here. string[i] is easier to read ...


3

Technically isdigit(token[0]): Needs #include <cctype>. Should be std::isdigit(static_cast<unsigned char>(token[0])). I guess passing a std::map<std::string_view, double> const& gives us flexibility with arguments.


3

You can use numpy.loadtxt() to read the data and numpy.reshape() to get the shape you want. The default is to split on whitespace and dtype of float. usecols are the columns we want. conveters is a dict mapping column nos. to functions to convert the column data; here they chop of the unwanted text. The .shape() converts the resulting numpy array from two ...


3

Disclaimer: I know this is a very liberal interpretation of a code review since it suggests an entirely different approach. I still thought it might provide a useful perspective when thinking about such problems in the future and reducing coding effort. I would suggest the following approach using regex to extract all the numbers that match the format "...


2

Keep the stack as simple as possible You have given your stack implementation knowledge of the types that are stored in the stack; either float or char. This complicates the code, since now you have to have two node structs, and two sets of push and pop functions, one for each type. You should try to apply the separation of concerns design principle here, ...


2

I see 2 errors and have some more advice. The Shebang is wrong. #!/bash/bin should be #!/bin/bash Do not put comment after the \ escape for a new line | cut -d"/" -f3,5 \ # stackoverflow.com/3581081 # should be | cut -d"/" -f3,5 \ I would move all functions (in your case only parse()) to the top of the file. Use lowercase variables (...


2

You can do better. But this is a deep rabbit hole. How about we start with a purely compile-time sublanguage. template<auto v> struct int_constant { constexpr double operator()(double)const{return v;} }; struct literal { double v = 0.; constexpr double operator()(double)const{return v;} }; struct variable { constexpr double operator()(double ...


1

In addition to the other answers: strpbrk or strcspn are useful for finding the next delimiter in a string. strspn can be used to find the next character that isn't a delimiter in a string. So given a char const* str as input, we could do something like: while (*str) { char const* end = strpbrk(str, delimiters); if (!end) end = strchr(str, '\0'); /*...


1

InputFile doesn't appear to be a file — a better name might be Statistics. usize is conventionally used for indexes and sizes instead of u32. Don't take an argument by &String. Since the algorithm works for not only files but other streams of characters as well, consider taking a BufRead argument. Reading the whole file into memory isn't efficient — an ...


Only top voted, non community-wiki answers of a minimum length are eligible