Guest User

Untitled

a guest
Jul 23rd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. #include <ostream>
  2. #include <string>
  3. #include <list>
  4.  
  5. class string_set
  6. {
  7. public:
  8.  
  9. string_set(int n);
  10. ~string_set();
  11.  
  12. bool insert(std::string s);
  13. bool remove(std::string s);
  14. bool member(std::string s);
  15.  
  16. operator std::string();
  17.  
  18. private:
  19. typedef std::list<std::string>* ptr_to_list_of_strings;
  20.  
  21. //array of pointers to lists of strings
  22. ptr_to_list_of_strings* table;
  23.  
  24. int hash(std::string s);
  25.  
  26. //the current number of lists in the table array
  27. int size;
  28.  
  29. //the number of strings in the list
  30. // int current_size;
  31. };
  32.  
  33. #include <iostream>
  34. #include <istream>
  35. #include <ostream>
  36. #include <sstream>
  37. #include <string>
  38. #include <list>
  39. #include <locale>
  40.  
  41. #include "string_set.hpp"
  42.  
  43. /***************************
  44. * CONSTRUCTOR
  45. * allocates a table array of size n,
  46. * and initializes it to contain n pointers to n empty lists.
  47. ***************************/
  48. string_set::string_set(int n)
  49. {
  50. size = n;
  51.  
  52. // initialize table to new pointer
  53. table = new ptr_to_list_of_strings[n];
  54.  
  55. // loop through and create a list in each array index
  56. for (int i(0); i < n; i++)
  57. {
  58. table[i] = new std::list<std::string>;
  59. }
  60. }
  61.  
  62.  
  63.  
  64. /**************************
  65. * DESTRUCTOR
  66. * deletes the table array
  67. ***************************/
  68. string_set::~string_set()
  69. {
  70. for (int i(0); i < size; i++)
  71. {
  72. delete table[i];
  73. }
  74. delete[] table;
  75. }
  76.  
  77.  
  78.  
  79. /**************************
  80. * INSERT
  81. * add the given string to the string_set if it is not already present.
  82. * The boolean returned should indicate whether the string_set was modified.
  83. ***************************/
  84. bool string_set::insert(std::string s)
  85. {
  86. // if string already exists
  87. if (member(s))
  88. {
  89. return false;
  90. }
  91.  
  92. // otherwise string does not exist
  93. else
  94. {
  95. // push string onto list at hashed table index
  96. table[hash(s)]-> push_front(s);
  97.  
  98. // current_size += 1;
  99. }
  100.  
  101. return true;
  102. }
  103.  
  104.  
  105.  
  106. /**************************
  107. * REMOVE
  108. * remove the given string from the string_set if it is present.
  109. * The boolean returned should indicate whether the string_set was modified.
  110. ***************************/
  111. bool string_set::remove(std::string s)
  112. {
  113. // if string exists
  114. if (member(s))
  115. {
  116. // remove the string from its hashed array index
  117. table[hash(s)]-> remove(s);
  118.  
  119. return true;
  120. }
  121.  
  122. return false;
  123. }
  124.  
  125.  
  126.  
  127. /**************************
  128. * MEMBER
  129. * return a boolean indicating whether the given string is present in the string_set.
  130. ***************************/
  131. bool string_set::member(std::string s)
  132. {
  133. // store hash value in index varibale
  134. int index = hash(s);
  135.  
  136. // initialize iter for looping
  137. std::list<std::string>::iterator iter;
  138.  
  139. ptr_to_list_of_strings elm = table[index];
  140.  
  141. // loop through table array
  142. for (iter = elm-> begin(); iter != elm-> end(); iter++)
  143. {
  144. // if equal string found
  145. if ((*iter) == s)
  146. {
  147. return true;
  148. }
  149. }
  150.  
  151. return false;
  152. }
  153.  
  154.  
  155.  
  156. /**************************
  157. * CONVERSION TO STRING
  158. * produce a string consisting of the strings in your string_set in no particular order
  159. * concatenated together, each followed by a newline. The empty set should produce just a newline.
  160. ***************************/
  161. string_set::operator std::string()
  162. {
  163. // initialize stringstream variable
  164. std::stringstream text;
  165.  
  166. bool has_at_least_one_word = false;
  167.  
  168. // loop through array
  169. for (int i(0); i < size; i++)
  170. {
  171. // else list is not empty, and contains strings
  172. ptr_to_list_of_strings elm = table[i];
  173.  
  174. if (elm-> empty()==false)
  175. {
  176. // initialize iter for looping
  177. std::list<std::string>::iterator iter;
  178.  
  179. // loop through table
  180. for (iter = elm-> begin(); iter != elm-> end(); iter++)
  181. {
  182. // load strings into stringstream
  183. text << *iter << 'n';
  184. }
  185. has_at_least_one_word = true;
  186. }
  187. }
  188.  
  189. // if bool indicates we have empty set, use newline
  190. if (has_at_least_one_word == false)
  191. {
  192. text << 'n';
  193. }
  194.  
  195. return text.str();
  196. }
  197.  
  198.  
  199.  
  200. /**************************
  201. * HASH FUNCTION
  202. ***************************/
  203. int string_set::hash(std::string s)
  204. {
  205. using namespace std;
  206.  
  207. locale loc;
  208.  
  209. const collate<char>& coll = use_facet<collate<char> > (loc);
  210.  
  211. unsigned long hash_num = coll.hash(s.data(), s.data() + s.length());
  212.  
  213. return hash_num % size;
  214. }
  215.  
  216. #include <iostream>
  217. #include <istream>
  218. #include <ostream>
  219. #include <fstream>
  220.  
  221.  
  222. #include "string_set.hpp"
  223.  
  224. int main()
  225. {
  226. // declare a single string_set of table size 1000
  227. string_set S(1000);
  228.  
  229. // then read in strings (i.e. words) from the first file
  230. // put them in S
  231. std::ifstream fin("first.txt");
  232. if (not fin)
  233. std::perror("first.txt");
  234. else
  235. {
  236. std::string word;
  237. while (fin >> word)
  238. S.insert(word);
  239. fin.close();
  240. }
  241.  
  242. // then read in strings (i.e. words) from the second file and
  243.  
  244. // take those out of S.
  245. std::ifstream sin("second.txt");
  246. if (not sin)
  247. std::perror("second.txt");
  248. else
  249. {
  250. std::string word;
  251. while (sin >> word)
  252. S.remove(word);
  253. sin.close();
  254. }
  255.  
  256. // output the words in S to standard output (i.e. std::cout).
  257. std::cout << (std::string)S;
  258. }
Add Comment
Please, Sign In to add comment