|
|
-i broke the compressor into functions was it a good decision ? |
-if i were to get rid of the private functions and just put the code inside of the compress function it would allow me to declare the member variables locally inside of the function instead of declaring them in the private area should i consider doing so even tough i don't have a memory limit? |
-will be separated into .h and .cpp files once finished |
|
|
|
|
auto&&
) :void createKey(std::string filePath, const std::map<unsigned char, std::vector<bool>>& m_keyArg);
|
|
|
|
->
because it's reference now, as opposed to an iterator.std::size_t
, so use that type in the for loop, rather than int
If you were to do that, the variables go out of scope as soon as the function ends, it's better to have them private. |
|
|
void Huffman::createKey(const std::string& filePath) const;
|
|
Note what I said in the other topic about the efficiency of vector versus other containers. Do some testing :+D Hopefully you have version control, it's easier if you are going alter types here and there. |
Thanks for the response! |
I have implemented the range FOR instead if using iterators and was just curious why people dislike them ? |
I don't really need the variables once the compress function finishes executing is it still worth leaving them private? |
I don't understand why you passed in the m_key by const reference if it can be seen by the function without being passed in wouldn't it be the same if i declared the function as following ?:void Huffman::createKey(const std::string& filePath) const; |
const
, one can't have const auto&&
, silly me - you fixed that by making the whole function const :+D Well done !void Huffman::createKey(std::string filePath, const std::map<unsigned char, std::vector<bool>>& m_keyArg) {
|
|
for (std::map<unsigned char, uint64_t>::iterator index = m_table.begin(); index != m_table.end(); index++) {
for (auto index = m_table.begin(); index != m_table.end(); index++) {
|
|
static_cast<unsigned char >()
everywhere; orunsigned char
to char
.cppreference wrote: |
---|
1 ↑ As of C++14, char must represent 256 distinct values, bijectively convertible to the values 0..255 of unsigned char, which may require a wider range of values. |
std::list
to std::vector
, and use unique_ptr
instead of new
.std::cout << (double)(clock() - tStart) / CLOCKS_PER_SEC << "(s)" << '\n';
std::cout << static_cast<double>(clock() - tStart) / CLOCKS_PER_SEC << "(s)" << '\n';
Source.cpp(57): warning C4244: 'argument': conversion from 'const std::streamsize' to 'unsigned int', possible loss of data |
auto const char_count = static_cast<unsigned int>(inFile.gcount());
i understand what the warning means but i cant seem to find how to fix it (i don't know which variable is of type const::std::streamsize so that i can static_cast the other i will experiment atm) |
|
|
The other things worth doing is change std::list to std::vector |
and use unique_ptr instead of new. |
|
|
std::reference_wrapper
because one can't have a container of references.Never used unique_ptr will have to learn this one before implementing. just a quick question do i release the memory with delete as if it was operator new? |
with your permission ofcourse :) |
m_
to member variables, but have stopped doing that awhile back. More importantly I was going to change m_table to freq_table or similar. The reason was I looking at the node struct, and erroneously thought the frequency wasn't being changed. I might have realised if the variable name was changed as well as the function setTable
to UpdateFreqTable
say.storeInList
is a little problem now the type is changed - so try to avoid putting types into function names :+)Does this mean we can use char instead of unsigned char ? |
m_fileContent = std::string(char_count, unsigned char{});
std::string
. std::string
is an alias for std::basic_string<char, std::char_traits<char>>
. If you wish the code to remain portable, you must use a char
there: it won't compile everywhere right now.cppreference wrote: |
---|
1 ↑ As of C++14, char must represent 256 distinct values, bijectively convertible to the values 0..255 of unsigned char, which may require a wider range of values. |
signed char
(say) to a unsigned char
? And that we can use char
without having to worry about sign problems? The thing is the default for x86 systems is signed char
. I am guessing (hoping ) this problem was the whole point of making that change in C++14.
|
|
I was just trying to change things to unique_ptr, but there is a problem: shared ownership between the table and list. Apparently shared_ptr is something to be ideally avoided. Am going to mention a different tack: try to do it with references. Will need std::reference_wrapper because one can't have a container of references. |
I was going to rename some of the variables as well. I used to pre-pend m_ to member variables, but have stopped doing that awhile back. More importantly I was going to change m_table to freq_table or similar. The reason was I looking at the node struct, and erroneously thought the frequency wasn't being changed. I might have realised if the variable name was changed as well as the function setTable to UpdateFreqTable say. |
The storeInList is a little problem now the type is changed - so try to avoid putting types into function names :+) |
Did you manage to find the equivalent of -Wconversion and -Wsign-conversion in your compiler? It was those which identified the problem in the first place. |
I will have to release the allocated memory at the second the compress function finishes which doesn't mean that the object will get out of scope until than so i think the options are: a) call the destructor to release the memory explicitly (will have to make one now :) ). b) make a function that uses m_root to release the memory the same way you would make a destructor for a binary tree. |
new
and use the STL containers in a pure fashion, then there shouldn't be a need to worry about manually doing memory management at all. That's why I mentioned using a std::vector<std::reference_wrapper<node>> node_data
buffer |= index << (7 - offSet);
(7 - offSet)
could be negative. Although you prevent that a few lines later, one could put that in an if statement, just to stop the warning:
|
|
Didn't find those in visual studio 2015 although the compiler does tell you if you do type conversion |
std::vector
. Another efficient container is std::deque
and it has functions such pop_front()
Sorry about that, it is a result of me being an amateur coder.
|
|
|
|
|
|
Can you enable C4018 ? Not sure if that is the right one, but it will be in there somewhere :+) |
But there is still a warning about sign conversion because (7 - offSet) could be negative. |
int buffer{};
and than you used static_cast<char>()
every time you use it. So why not just make it char buffer{};
from the beginning ?
|
|
std::deque<std::reference_wrapper<node>> NodeData;
works.std::reference_wrapper<node>
never worked with this one before still need to learn it
|
|