Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Part A - A2 Analysis
- ### numRecords()
- Let NR(n) represent the number of operations needed
- ```cpp
- template <class TYPE>
- int SimpleTable<TYPE>::numRecords() const{
- int rc=0; // 1
- for(int i=0;records_[i]!=nullptr;i++){ // 1 + n + n
- rc++; // n
- }
- return rc; // 1
- }
- ```
- NR(n) = 1 + 1 + n + n + n + 1
- NR(n) = 3n + 3
- NR(n) is O(n)
- ### update() - if item does not exists so you need to add it as a new record, assume no grow() call is made
- Let U(n) represent the number of operations needed
- ```cpp
- template <class TYPE>
- void SimpleTable<TYPE>::update(const std::string& key, const TYPE& value){
- int idx=-1; // 1
- int size=numRecords(); // 1 + NR(n)
- for(int i=0;i<size;i++){ // 1 + n + n
- if(records_[i]->key_ == key){ // n + n
- idx=i;
- }
- }
- if(idx==-1){ // 1
- if(size == capacity_){ // 1
- grow();
- }
- records_[size++]=new Record(key,value); // 1 + 1 + 1 + 2 (Record())
- for(int i=size-1;i>0 && records_[i]->key_ < records_[i-1]->key_;i--){ // 2 + 8(n-1) + (n-1)
- Record* tmp=records_[i]; // 2(n-1)
- records_[i]=records_[i-1]; // 4(n-1)
- records_[i-1]=tmp; // 3(n-1)
- }
- }
- else{
- records_[idx]->data_=value;
- }
- }
- ```
- U(n) = 1 + 1 + NR(n) + 1 + n + n + n + n + 1 + 1 + 1 + 1 + 1 + 2 + 2 + 8(n-1) + (n-1) + 2(n-1) + 4(n-1) + 3(n-1)
- U(n) = 1 + 1 + 3n + 3 + 1 + n + n + n + n + 1 + 1 + 1 + 1 + 1 + 2 + 2 + 8(n-1) + (n-1) + 2(n-1) + 4(n-1) + 3(n-1)
- U(n) = 7n + 18(n-1) + 15
- U(n) = 7n + 18n + 15 - 18
- U(n) = 25n - 3
- U(n) is O(n)
- ### update() - if item does exists and you are just modifying what is there
- Let UE(n) represent the number of operations needed
- ```cpp
- template <class TYPE>
- void SimpleTable<TYPE>::update(const std::string& key, const TYPE& value){
- int idx=-1; // 1
- int size=numRecords(); // 1 + NR(n)
- for(int i=0;i<size;i++){ // 1 + n + n
- if(records_[i]->key_ == key){ // n + n
- idx=i;
- }
- }
- if(idx==-1){ // 1
- if(size == capacity_){
- grow();
- }
- records_[size++]=new Record(key,value);
- for(int i=size-1;i>0 && records_[i]->key_ < records_[i-1]->key_;i--){
- Record* tmp=records_[i];
- records_[i]=records_[i-1];
- records_[i-1]=tmp;
- }
- }
- else{
- records_[idx]->data_=value; // 1 + 1 + 1
- }
- }
- ```
- UE(n) = 1 + 1 + NR(n) + 1 + n + n + n + n + 1 + 1 + 1 + 1
- UE(n) = 1 + 1 + 3n + 3 + 1 + n + n + n + n + 1 + 1 + 1 + 1
- UE(n) = 7n + 7
- UE(n) is O(n)
- ### find() - if item is there
- Let F(n) represent the number of operations needed
- ```cpp
- template <class TYPE>
- bool SimpleTable<TYPE>::find(const std::string& key, TYPE& value){
- int idx=-1; // 1
- for(int i=0;i<numRecords();i++){ // 1 + n + n(NR(n)) + n
- if(records_[i]->key_ == key){ // n + n + n
- idx=i; // 1
- }
- }
- if(idx==-1) // 1
- return false;
- else{
- value=records_[idx]->data_; // 1 + 1 + 1
- return true; // 1
- }
- }
- ```
- F(n) = 1 + 1 + n + n(NR(n)) + n + n + n + n + 1 + 1 + 1 + 1 + 1 + 1
- F(n) = 1 + 1 + n + n(3n + 3) + n + n + n + n + 1 + 1 + 1 + 1 + 1 + 1
- F(n) = 5n + n(3n + 3) + 8
- F(n) = 5n + 3n^2 + 3n + 8
- F(n) = 3n^2 + 8n + 8
- F(n) is O(n^2)
- ### find() - if item is not there
- Let FN(n) represent the number of operations needed
- ```cpp
- template <class TYPE>
- bool SimpleTable<TYPE>::find(const std::string& key, TYPE& value){
- int idx=-1; // 1
- for(int i=0;i<numRecords();i++){ // 1 + n + n(NR(n)) + n
- if(records_[i]->key_ == key){ // n + n + n
- idx=i; // 1
- }
- }
- if(idx==-1) // 1
- return false; // 1
- else{
- value=records_[idx]->data_;
- return true;
- }
- }
- ```
- FN(n) = 1 + 1 + n + n(NR(n)) + n + n + n + n + 1 + 1 + 1
- FN(n) = 1 + 1 + n + n(3n + 3) + n + n + n + n + 1 + 1 + 1
- FN(n) = 5n + n(3n + 3) + 5
- FN(n) = 5n + 3n^2 + 3n + 5
- FN(n) = 3n^2 + 8n + 5
- FN(n) is O(n^2)
- ### remove() - if item is there
- Let R(n) represent the number of operations needed
- ```cpp
- template <class TYPE>
- bool SimpleTable<TYPE>::remove(const std::string& key){
- int idx=-1; // 1
- for(int i=0;i<numRecords();i++){ // 1 + n + n(NR(n)) + n
- if(records_[i]->key_ == key){ // n + n + n
- idx=i; // 1
- }
- }
- if(idx!=-1){ // 1
- int size=numRecords(); // 1 + NR(n)
- delete records_[idx]; // 1 + 1
- for(int i=idx;i<size-1;i++){ // 1 + (n-1) + (n-1)
- records_[i]=records_[i+1]; // 4(n-1)
- }
- records_[size-1]=nullptr; // 1 + 1 + 1
- return true; // 1
- }
- else{
- return false;
- }
- }
- ```
- R(n) = 1 + 1 + n + n(NR(n)) + n + n + n + n + 1 + 1 + 1 + NR(n) + 1 + 1 + 1 + (n-1) + (n-1) + 4(n-1) + 1 + 1 + 1 + 1
- R(n) = 1 + 1 + n + n(3n + 3) + n + n + n + n + 1 + 1 + 1 + 3n + 3 + 1 + 1 + 1 + (n-1) + (n-1) + 4(n-1) + 1 + 1 + 1 + 1
- R(n) = 8n + 6(n-1) + 12 + n(3n + 3)
- R(n) = 8n + 6(n-1) + 12 + 3n^2 + 3n
- R(n) = 8n + 6n + 12 + 3n^2 + 3n - 6
- R(n) = 3n^2 + 17n + 6
- R(n) is O(n^2)
- ### remove() - if item is not there
- Let RN(n) represent the number of operations needed
- ```cpp
- template <class TYPE>
- bool SimpleTable<TYPE>::remove(const std::string& key){
- int idx=-1; // 1
- for(int i=0;i<numRecords();i++){ // 1 + n + n(NR(n)) + n
- if(records_[i]->key_ == key){ // n + n + n
- idx=i; // 1
- }
- }
- if(idx!=-1){ // 1
- int size=numRecords();
- delete records_[idx];
- for(int i=idx;i<size-1;i++){
- records_[i]=records_[i+1];
- }
- records_[size-1]=nullptr;
- return true;
- }
- else{
- return false; // 1
- }
- }
- ```
- RN(n) = 1 + 1 + n + n(NR(n)) + n + n + n + n + 1 + 1 + 1
- RN(n) = 1 + 1 + n + n(3n + 3) + n + n + n + n + 1 + 1 + 1
- RN(n) = n(3n + 3) + 5n + 5
- RN(n) = 3n^2 + 3n + 5n + 5
- RN(n) = 3n^2 + 8n + 5
- RN(n) is O(n^2)
- ### copy constructor
- Let CC(n) represent the number of operations needed
- ```cpp
- template <class TYPE>
- SimpleTable<TYPE>::SimpleTable(const SimpleTable<TYPE>& rhs){
- records_=new Record*[rhs.capacity_]; // 1 + 1 + 1
- capacity_=rhs.capacity_; // 1
- for(int i=0;i<rhs.numRecords();i++){ // 1 + n + n(NR(n)) + n
- update(rhs.records_[i]->key_,rhs.records_[i]->data_); // n(U(n)) + n + n + n + n
- }
- }
- ```
- CC(n) = 1 + 1 + 1 + 1 + 1 + n + n(NR(n)) + n + n(U(n)) + n + n + n + n
- CC(n) = 1 + 1 + 1 + 1 + 1 + n + n(3n + 3) + n + n(25n - 3) + n + n + n + n
- CC(n) = n(3n + 3) + n(25n - 3) + 6n + 5
- CC(n) = 3n^2 + 3n + 25n^2 - 3n + 6n + 5
- CC(n) = 28n^2 + 6n + 5
- CC(n) is O(n^2)
- ### destructor
- Let D(n) represent the number of operations needed
- ```cpp
- template <class TYPE>
- SimpleTable<TYPE>::~SimpleTable(){
- if(records_){ // 1
- int sz=numRecords(); // 1 + NR(n)
- for(int i=0;i<sz;i++){ // 1 + n + n
- remove(records_[0]->key_); // n + n + n(R(n))
- }
- delete [] records_; // 1
- }
- }
- ```
- D(n) = 1 + 1 + NR(n) + 1 + n + n + n + n + n(R(n)) + 1
- D(n) = 1 + 1 + 3n + 3 + 1 + n + n + n + n + n(3n^2 + 17n + 6) + 1
- D(n) = n(3n^2 + 17n + 6) + 7n + 7
- D(n) = 3n^3 + 17n^2 + 13n + 7
- D(n) is O(n^3)
- # Part B - A2 Improvements
- ### Function(s) improved: One Argument Constructor
- The for loop initializing the new records can be avoided by using an initializer list when dynamically allocating the array.
- ```cpp
- template <class TYPE>
- SimpleTable<TYPE>::SimpleTable(int capacity): Table<TYPE>(){
- records_=new Record*[capacity] {nullptr};
- capacity_=capacity;
- // for(int i=0;i<capacity_;i++){
- // records_[i]=nullptr;
- // }
- }
- ```
- ### Function(s) improved: numRecords(), and any function that calls numRecords()
- The number of records can be stored as a property in the class instead of calculating it each time the function is called
- ```cpp
- template <class TYPE>
- int SimpleTable<TYPE>::numRecords() const{
- // int rc=0;
- // for(int i=0;records_[i]!=nullptr;i++){
- // rc++;
- // }
- // return rc;
- return size;
- }
- ```
- ### Function(s) improved: Copy Constructor, Copy Assignment Operator
- The object being passed into function(s) is of type SimpleTable. Because we know SimpleTables are sorted, there's no need to call update. Instead we can do a simple deep copy.
- ```cpp
- template <class TYPE>
- SimpleTable<TYPE>::SimpleTable(const SimpleTable<TYPE>& rhs){
- records_=new Record*[rhs.capacity_];
- capacity_=rhs.capacity_;
- for(int i=0;i<rhs.numRecords();i++){
- // update(rhs.records_[i]->key_,rhs.records_[i]->data_);
- records_[i] = new Record(rhs.records_[i]->key_,rhs.records_[i]->data_);
- }
- }
- ```
- # Part A - A2 Analysis
- ### numRecords()
- Let NR(n) represent the number of operations needed
- ```cpp
- template <class TYPE>
- int SimpleTable<TYPE>::numRecords() const{
- int rc=0; // 1
- for(int i=0;records_[i]!=nullptr;i++){ // 1 + n + n
- rc++; // n
- }
- return rc; // 1
- }
- ```
- NR(n) = 1 + 1 + n + n + n + 1
- NR(n) = 3n + 3
- NR(n) is O(n)
- ### update() - if item does not exists so you need to add it as a new record, assume no grow() call is made
- Let U(n) represent the number of operations needed
- ```cpp
- template <class TYPE>
- void SimpleTable<TYPE>::update(const std::string& key, const TYPE& value){
- int idx=-1; // 1
- int size=numRecords(); // 1 + NR(n)
- for(int i=0;i<size;i++){ // 1 + n + n
- if(records_[i]->key_ == key){ // n + n
- idx=i;
- }
- }
- if(idx==-1){ // 1
- if(size == capacity_){ // 1
- grow();
- }
- records_[size++]=new Record(key,value); // 1 + 1 + 1 + 2 (Record())
- for(int i=size-1;i>0 && records_[i]->key_ < records_[i-1]->key_;i--){ // 2 + 8(n-1) + (n-1)
- Record* tmp=records_[i]; // 2(n-1)
- records_[i]=records_[i-1]; // 4(n-1)
- records_[i-1]=tmp; // 3(n-1)
- }
- }
- else{
- records_[idx]->data_=value;
- }
- }
- ```
- U(n) = 1 + 1 + NR(n) + 1 + n + n + n + n + 1 + 1 + 1 + 1 + 1 + 2 + 2 + 8(n-1) + (n-1) + 2(n-1) + 4(n-1) + 3(n-1)
- U(n) = 1 + 1 + 3n + 3 + 1 + n + n + n + n + 1 + 1 + 1 + 1 + 1 + 2 + 2 + 8(n-1) + (n-1) + 2(n-1) + 4(n-1) + 3(n-1)
- U(n) = 7n + 18(n-1) + 15
- U(n) = 7n + 18n + 15 - 18
- U(n) = 25n - 3
- U(n) is O(n)
- ### update() - if item does exists and you are just modifying what is there
- Let UE(n) represent the number of operations needed
- ```cpp
- template <class TYPE>
- void SimpleTable<TYPE>::update(const std::string& key, const TYPE& value){
- int idx=-1; // 1
- int size=numRecords(); // 1 + NR(n)
- for(int i=0;i<size;i++){ // 1 + n + n
- if(records_[i]->key_ == key){ // n + n
- idx=i;
- }
- }
- if(idx==-1){ // 1
- if(size == capacity_){
- grow();
- }
- records_[size++]=new Record(key,value);
- for(int i=size-1;i>0 && records_[i]->key_ < records_[i-1]->key_;i--){
- Record* tmp=records_[i];
- records_[i]=records_[i-1];
- records_[i-1]=tmp;
- }
- }
- else{
- records_[idx]->data_=value; // 1 + 1 + 1
- }
- }
- ```
- UE(n) = 1 + 1 + NR(n) + 1 + n + n + n + n + 1 + 1 + 1 + 1
- UE(n) = 1 + 1 + 3n + 3 + 1 + n + n + n + n + 1 + 1 + 1 + 1
- UE(n) = 7n + 7
- UE(n) is O(n)
- ### find() - if item is there
- Let F(n) represent the number of operations needed
- ```cpp
- template <class TYPE>
- bool SimpleTable<TYPE>::find(const std::string& key, TYPE& value){
- int idx=-1; // 1
- for(int i=0;i<numRecords();i++){ // 1 + n + n(NR(n)) + n
- if(records_[i]->key_ == key){ // n + n + n
- idx=i; // 1
- }
- }
- if(idx==-1) // 1
- return false;
- else{
- value=records_[idx]->data_; // 1 + 1 + 1
- return true; // 1
- }
- }
- ```
- F(n) = 1 + 1 + n + n(NR(n)) + n + n + n + n + 1 + 1 + 1 + 1 + 1 + 1
- F(n) = 1 + 1 + n + n(3n + 3) + n + n + n + n + 1 + 1 + 1 + 1 + 1 + 1
- F(n) = 5n + n(3n + 3) + 8
- F(n) = 5n + 3n^2 + 3n + 8
- F(n) = 3n^2 + 8n + 8
- F(n) is O(n^2)
- ### find() - if item is not there
- Let FN(n) represent the number of operations needed
- ```cpp
- template <class TYPE>
- bool SimpleTable<TYPE>::find(const std::string& key, TYPE& value){
- int idx=-1; // 1
- for(int i=0;i<numRecords();i++){ // 1 + n + n(NR(n)) + n
- if(records_[i]->key_ == key){ // n + n + n
- idx=i; // 1
- }
- }
- if(idx==-1) // 1
- return false; // 1
- else{
- value=records_[idx]->data_;
- return true;
- }
- }
- ```
- FN(n) = 1 + 1 + n + n(NR(n)) + n + n + n + n + 1 + 1 + 1
- FN(n) = 1 + 1 + n + n(3n + 3) + n + n + n + n + 1 + 1 + 1
- FN(n) = 5n + n(3n + 3) + 5
- FN(n) = 5n + 3n^2 + 3n + 5
- FN(n) = 3n^2 + 8n + 5
- FN(n) is O(n^2)
- ### remove() - if item is there
- Let R(n) represent the number of operations needed
- ```cpp
- template <class TYPE>
- bool SimpleTable<TYPE>::remove(const std::string& key){
- int idx=-1; // 1
- for(int i=0;i<numRecords();i++){ // 1 + n + n(NR(n)) + n
- if(records_[i]->key_ == key){ // n + n + n
- idx=i; // 1
- }
- }
- if(idx!=-1){ // 1
- int size=numRecords(); // 1 + NR(n)
- delete records_[idx]; // 1 + 1
- for(int i=idx;i<size-1;i++){ // 1 + (n-1) + (n-1)
- records_[i]=records_[i+1]; // 4(n-1)
- }
- records_[size-1]=nullptr; // 1 + 1 + 1
- return true; // 1
- }
- else{
- return false;
- }
- }
- ```
- R(n) = 1 + 1 + n + n(NR(n)) + n + n + n + n + 1 + 1 + 1 + NR(n) + 1 + 1 + 1 + (n-1) + (n-1) + 4(n-1) + 1 + 1 + 1 + 1
- R(n) = 1 + 1 + n + n(3n + 3) + n + n + n + n + 1 + 1 + 1 + 3n + 3 + 1 + 1 + 1 + (n-1) + (n-1) + 4(n-1) + 1 + 1 + 1 + 1
- R(n) = 8n + 6(n-1) + 12 + n(3n + 3)
- R(n) = 8n + 6(n-1) + 12 + 3n^2 + 3n
- R(n) = 8n + 6n + 12 + 3n^2 + 3n - 6
- R(n) = 3n^2 + 17n + 6
- R(n) is O(n^2)
- ### remove() - if item is not there
- Let RN(n) represent the number of operations needed
- ```cpp
- template <class TYPE>
- bool SimpleTable<TYPE>::remove(const std::string& key){
- int idx=-1; // 1
- for(int i=0;i<numRecords();i++){ // 1 + n + n(NR(n)) + n
- if(records_[i]->key_ == key){ // n + n + n
- idx=i; // 1
- }
- }
- if(idx!=-1){ // 1
- int size=numRecords();
- delete records_[idx];
- for(int i=idx;i<size-1;i++){
- records_[i]=records_[i+1];
- }
- records_[size-1]=nullptr;
- return true;
- }
- else{
- return false; // 1
- }
- }
- ```
- RN(n) = 1 + 1 + n + n(NR(n)) + n + n + n + n + 1 + 1 + 1
- RN(n) = 1 + 1 + n + n(3n + 3) + n + n + n + n + 1 + 1 + 1
- RN(n) = n(3n + 3) + 5n + 5
- RN(n) = 3n^2 + 3n + 5n + 5
- RN(n) = 3n^2 + 8n + 5
- RN(n) is O(n^2)
- ### copy constructor
- Let CC(n) represent the number of operations needed
- ```cpp
- template <class TYPE>
- SimpleTable<TYPE>::SimpleTable(const SimpleTable<TYPE>& rhs){
- records_=new Record*[rhs.capacity_]; // 1 + 1 + 1
- capacity_=rhs.capacity_; // 1
- for(int i=0;i<rhs.numRecords();i++){ // 1 + n + n(NR(n)) + n
- update(rhs.records_[i]->key_,rhs.records_[i]->data_); // n(U(n)) + n + n + n + n
- }
- }
- ```
- CC(n) = 1 + 1 + 1 + 1 + 1 + n + n(NR(n)) + n + n(U(n)) + n + n + n + n
- CC(n) = 1 + 1 + 1 + 1 + 1 + n + n(3n + 3) + n + n(25n - 3) + n + n + n + n
- CC(n) = n(3n + 3) + n(25n - 3) + 6n + 5
- CC(n) = 3n^2 + 3n + 25n^2 - 3n + 6n + 5
- CC(n) = 28n^2 + 6n + 5
- CC(n) is O(n^2)
- ### destructor
- Let D(n) represent the number of operations needed
- ```cpp
- template <class TYPE>
- SimpleTable<TYPE>::~SimpleTable(){
- if(records_){ // 1
- int sz=numRecords(); // 1 + NR(n)
- for(int i=0;i<sz;i++){ // 1 + n + n
- remove(records_[0]->key_); // n + n + n(R(n))
- }
- delete [] records_; // 1
- }
- }
- ```
- D(n) = 1 + 1 + NR(n) + 1 + n + n + n + n + n(R(n)) + 1
- D(n) = 1 + 1 + 3n + 3 + 1 + n + n + n + n + n(3n^2 + 17n + 6) + 1
- D(n) = n(3n^2 + 17n + 6) + 7n + 7
- D(n) = 3n^3 + 17n^2 + 13n + 7
- D(n) is O(n^3)
- # Part B - A2 Improvements
- ### Function(s) improved: One Argument Constructor
- The for loop initializing the new records can be avoided by using an initializer list when dynamically allocating the array.
- ```cpp
- template <class TYPE>
- SimpleTable<TYPE>::SimpleTable(int capacity): Table<TYPE>(){
- records_=new Record*[capacity] {nullptr};
- capacity_=capacity;
- // for(int i=0;i<capacity_;i++){
- // records_[i]=nullptr;
- // }
- }
- ```
- ### Function(s) improved: numRecords(), and any function that calls numRecords()
- The number of records can be stored as a property in the class instead of calculating it each time the function is called
- ```cpp
- template <class TYPE>
- int SimpleTable<TYPE>::numRecords() const{
- // int rc=0;
- // for(int i=0;records_[i]!=nullptr;i++){
- // rc++;
- // }
- // return rc;
- return size;
- }
- ```
- ### Function(s) improved: Copy Constructor, Copy Assignment Operator
- The object being passed into function(s) is of type SimpleTable. Because we know SimpleTables are sorted, there's no need to call update. Instead we can do a simple deep copy.
- ```cpp
- template <class TYPE>
- SimpleTable<TYPE>::SimpleTable(const SimpleTable<TYPE>& rhs){
- records_=new Record*[rhs.capacity_];
- capacity_=rhs.capacity_;
- for(int i=0;i<rhs.numRecords();i++){
- // update(rhs.records_[i]->key_,rhs.records_[i]->data_);
- records_[i] = new Record(rhs.records_[i]->key_,rhs.records_[i]->data_);
- }
- }
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement