Okay, first off Im a C programmer not a C++ programmer, but heres are few pointers ;-)
You shouldn't have header files including source files, the correct way to compile a multi source file project is to compile each of the source files individually and then link them together to create a single executable. They way to do this varies based on the compiler/IDE used but an example with gcc is:
Code:
$ gcc file1.c file2.c file3.c -o exuctable_file
You shouldn't have functions inside a header file, you should only define functions in headers for example:
Code:
/* File.c */
#include "file.h"
int func(int a) {
/* Do stuff */
}
/* File.h */
int func(int);
If you have a function body in a header file and the header is included by more than one source file you will get errors because the function will be defined twice. If you need to put something in a header that may get defined twice you can enclose it in an ifdef block, for example:
Code:
#ifndef MYTYPE
#define MYTYPE
typedef struct {
int a;
int b;
} myType_t;
#endif /* MYTYPE */
Then if the MYTYPE token is already defined then myType_t won't get redefined.
As for style, C and C++ let you put whitespace just about anywhere you want, some of your code is a little cluttered (IMHO) for example:
Code:
void Slicer::bounce(int steps){
for(int z = 0; z < steps; ++z){
a_dup[0] = ((a[a_size-1] << 1)|(a[a_size-1] >> (bits-1))) ^ (a[0] | a[1]); // Roll last row to align with first row.
for(int i = 1; i < a_size-1; ++i) // Starts with a[1]...a[0] has already been done.
a_dup[i] = a[i-1] ^ (a[i] | a[i+1]); // a slight optimization could be had by incrementing i in the lvalue (a_dup[i++] and leaving the increment blank in the for loop...not done for clarity, and because speed-gain would be nominal.
a_dup[a_size - 1] = a[a_size-2] ^ (a[a_size-1] | (a[0] >> 1)|(a[0] << (bits-1))); // Roll 1st row to align with last row.
swap(a, a_dup);
toggler = !toggler; // if toggler is set to 0 upon initialization, the first bit of data will be offset.
}
for (int i = offset * toggler; i < a_size; i += spacer) //update key. (offset * toggler) toggles between 0 and offset.
key[i/spacer] = a[i]; // (i/spacer) increments by one because 'i' increments as i += spacer.
keypos = 0; // Resets to 0 after each update.
}
Is much harder to read than:
Code:
void Slicer::bounce(int steps) {
for(int z = 0; z < steps; ++z) {
// Roll last row to align with first row.
a_dup[0] = ((a[a_size-1] << 1) |
(a[a_size-1] >> (bits-1))) ^
(a[0] | a[1]);
// Starts with a[1]...a[0] has already been done.
for(int i = 1; i < a_size-1; ++i) {
/*
* a slight optimization could be had by
* incrementing i in the lvalue (a_dup[i++]
* and leaving the increment blank in the for
* loop...not done for clarity, and because
* speed-gain would be nominal.
*/
a_dup[i] = a[i-1] ^ (a[i] | a[i+1]);
}
// Roll 1st row to align with last row.
a_dup[a_size - 1] = a[a_size-2] ^
(a[a_size-1] |
(a[0] >> 1) |
(a[0] << (bits-1)));
swap(a, a_dup);
/*
* if toggler is set to 0 upon initialization,
* the first bit of data will be offset.
*/
toggler = !toggler;
}
//update key. (offset * toggler) toggles between 0 and offset.
for(int i = offset * toggler; i < a_size; i += spacer) {
/*
* (i/spacer) increments by one because 'i'
* increments as i += spacer.
*/
key[i/spacer] = a[i];
}
keypos = 0; // Resets to 0 after each update.
}
Other than those few things, nice work.