Bag  0.9
Bag.cpp
Go to the documentation of this file.
1 // Created by Frank M. Carrano and Tim Henry.
2 // Copyright (c) 2013 __Pearson Education__. All rights reserved.
3 
6 #include "Bag.h"
7 #include <cstddef>
8 
9 template<class ItemType>
10 Bag<ItemType>::Bag() : itemCount(0), maxItems(DEFAULT_BAG_SIZE)
11 {
12 } // end default constructor
13 
14 template<class ItemType>
16 {
17  return itemCount;
18 } // end getCurrentSize
19 
20 template<class ItemType>
22 {
23  return itemCount == 0;
24 } // end isEmpty
25 
26 template<class ItemType>
27 bool Bag<ItemType>::add(const ItemType& newEntry)
28 {
29  bool hasRoomToAdd = (itemCount < maxItems);
30  if (hasRoomToAdd)
31  {
32  items[itemCount] = newEntry;
33  itemCount++;
34  } // end if
35 
36  return hasRoomToAdd;
37 } // end add
38 
39 template<class ItemType>
40 bool Bag<ItemType>::remove(const ItemType& anEntry)
41 {
42  int locatedIndex = getIndexOf(anEntry);
43  bool canRemoveItem = !isEmpty() && (locatedIndex > -1);
44  if (canRemoveItem)
45  {
46  itemCount--;
47  items[locatedIndex] = items[itemCount];
48  } // end if
49 
50  return canRemoveItem;
51 } // end remove
52 
53 template<class ItemType>
55 {
56  itemCount = 0;
57 } // end clear
58 
59 template<class ItemType>
60 int Bag<ItemType>::getFrequencyOf(const ItemType& anEntry) const
61 {
62  int frequency = 0;
63  int searchIndex = 0;
64  while (searchIndex < itemCount)
65  {
66  if (items[searchIndex] == anEntry)
67  {
68  frequency++;
69  } // end if
70 
71  searchIndex++;
72  } // end while
73 
74  return frequency;
75 } // end getFrequencyOf
76 
77 template<class ItemType>
78 bool Bag<ItemType>::contains(const ItemType& anEntry) const
79 {
80  return getIndexOf(anEntry) > -1;
81 } // end contains
82 
83 /* ALTERNATE 1
84 template<class ItemType>
85 bool Bag<ItemType>::contains(const ItemType& anEntry) const
86 {
87  return getFrequencyOf(anEntry) > 0;
88 } // end contains
89 */
90 /* ALTERNATE 2
91 template<class ItemType>
92 bool Bag<ItemType>::contains(const ItemType& anEntry) const
93 {
94  bool found = false;
95  for (int i = 0; !found && (i < itemCount); i++)
96  {
97  if (anEntry == items[i])
98  {
99  found = true;
100  } // end if
101  } // end for
102 
103  return found;
104 } // end contains
105 */
106 
107 template<class ItemType>
108 vector<ItemType> Bag<ItemType>::toVector() const
109 {
110  vector<ItemType> bagContents;
111  for (int i = 0; i < itemCount; i++)
112  bagContents.push_back(items[i]);
113  return bagContents;
114 } // end toVector
115 
116 // private
117 template<class ItemType>
118 int Bag<ItemType>::getIndexOf(const ItemType& target) const
119 {
120  bool found = false;
121  int result = -1;
122  int searchIndex = 0;
123  // if the bag is empty, itemCount is zero, so loop is skipped
124  while (!found && (searchIndex < itemCount))
125  {
126  if (items[searchIndex] == target)
127  {
128  found = true;
129  result = searchIndex;
130  }
131  else
132  {
133  searchIndex++;
134  } // end if
135  } // end while
136 
137  return result;
138 } // end getIndexOf