#include <cstdlib> // Provides size_t // included in permute_append.h
#include <sstream> // provides the << operator for string concatenation. // not using
#include <string> //provides string type //included in permute_append.h
#include "permute_append.h"
usingnamespace std;
namespace CISP430_A5
{
/*CONSTRUCTORS, DECONSTRUCTOR*/
permute_append::permute_append() {
firstString = "x"; // x is just a default value
secondString = "x"; // x is just a default value
total = 0;
}
permute_append::permute_append(constchar* first, constchar* second) {
firstString = first;
secondString = second;
total = 1; // at least one permutation
for (int i=firstString.length(); i>0; --i) { // number of permutations is equal to factorial of the number of characters in firstString
total *= i;
}
/*Turn string into linked list of chars*/
for (int i=0; i<firstString.length(); ++i) {
permute_me.add(firstString[i]);
}
}
permute_append::permute_append(const permute_append &source) {
firstString = source.firstString;
secondString = source.secondString;
total = source.total;
permute_me = source.permute_me;
result_list = source.result_list;
}
permute_append::~permute_append() {
total = 0;
firstString = "";
secondString = "";
/*so I guess we don't need to delete the linked_list object manually*/
// delete permute_me;
// delete result_list;
}
linked_list<string> permute_append::permute(linked_list<char> charList) { // permute the characters in the array (n items, n at a time)
/*Returns a linked_list of strings, each string a permutation of the chars in charList.*/
static linked_list<string> perms; static linked_list<char> usedChars;
linked_list<char> character;
for (int i = 0; i < charList.size(); ++i) {
character = list_splice(charList, i, 1); //??? How do we pass charList to list_splice so list_splice modifies charList directly? Answer: just like I did.
usedChars.add( character.get(0) );
if (charList.size() == 0) perms.add( charList_join("", usedChars) ); //??? Is charList_join() working? I believe so.
permute(charList);
list_splice( charList, i, 0, character.get(0) );
list_pop( usedChars );
}
return perms;
}
string permute_append::do_it() {
// appends secondString to each permutation of firstString and stores each result in result_list
linked_list<string> perms( permute(permute_me) ); // ??? How do you point to the returned linked_list properly?
// string result = "";
for (size_t i=0; i<perms.size(); ++i) {
result_list.add(perms.get(i) + secondString);
}
}
string permute_append::do_it(constchar* first, constchar* second) {
// set firstString and secondString then appends secondString to each permutation of firstString and stores each result in result_list
firstString = first; secondString = second;
do_it();
}
string permute_append::get(size_t position) { // get a result
/*Get the item at position position from result_list */
return result_list.get(position);
}
}