1 /* 2 * cResourceLib.cc 3 * Avida 4 * 5 * Called "resource_lib.cc" prior to 12/5/05. 6 * Copyright 1999-2011 Michigan State University. All rights reserved. 7 * Copyright 1993-2003 California Institute of Technology. 8 * 9 * 10 * This file is part of Avida. 11 * 12 * Avida is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License 13 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 14 * 15 * Avida is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 17 * 18 * You should have received a copy of the GNU Lesser General Public License along with Avida. 19 * If not, see <http://www.gnu.org/licenses/>. 20 * 21 */ 22 23 #include "cResourceLib.h" 24 25 #include "cResource.h" 26 #include "cResourceHistory.h" 27 28 using namespace std; 29 30 ~cResourceLib()31cResourceLib::~cResourceLib() 32 { 33 for (int i = 0; i < m_resource_array.GetSize(); i++) delete m_resource_array[i]; 34 delete m_initial_levels; 35 } 36 AddResource(const cString & res_name)37cResource* cResourceLib::AddResource(const cString& res_name) 38 { 39 if (m_initial_levels) return NULL; // Initial levels calculated, cannot add more resources 40 41 const int new_id = m_resource_array.GetSize(); 42 cResource* new_resource = new cResource(res_name, new_id); 43 m_resource_array.Resize(new_id + 1); 44 m_resource_array[new_id] = new_resource; 45 46 return new_resource; 47 } 48 GetResource(const cString & res_name) const49cResource* cResourceLib::GetResource(const cString& res_name) const 50 { 51 for (int i = 0; i < m_resource_array.GetSize(); i++) { 52 if (m_resource_array[i]->GetName() == res_name) return m_resource_array[i]; 53 } 54 cerr << "Error: Unknown resource '" << res_name << "'." << endl; 55 return NULL; 56 } 57 58 GetInitialResourceLevels() const59const cResourceHistory& cResourceLib::GetInitialResourceLevels() const 60 { 61 if (!m_initial_levels) { 62 tArray<double> levels(m_resource_array.GetSize()); 63 for (int i = 0; i < m_resource_array.GetSize(); i++) levels[i] = m_resource_array[i]->GetInitial(); 64 m_initial_levels = new cResourceHistory; 65 m_initial_levels->AddEntry(0, levels); 66 } 67 68 return *m_initial_levels; 69 } 70 71 DoesResourceExist(const cString & res_name)72bool cResourceLib::DoesResourceExist(const cString& res_name) 73 { 74 for (int i = 0; i < m_resource_array.GetSize(); i++) if (m_resource_array[i]->GetName() == res_name) return true; 75 return false; 76 } 77 78 /* This assigns an index to a resource within its own type (deme vs. non-deme) 79 * If the resource already has a positive id nothing will be assigned. 80 * (Enforced by cResource::SetIndex()) 81 * 82 * Population resource counts include all non-deme resources, regardless of geometry. 83 * Deme resource counts include all deme resources, regardless of geometry. 84 */ SetResourceIndex(cResource * res)85void cResourceLib::SetResourceIndex(cResource* res) 86 { 87 bool is_deme = res->GetDemeResource(); 88 89 if (is_deme) { 90 res->SetIndex(m_num_deme_resources); 91 m_num_deme_resources++; 92 } 93 else { 94 res->SetIndex(m_resource_array.GetSize() - 1 - m_num_deme_resources); 95 } 96 } 97 98