<NME> AbstractDataTable.cpp
<BEF> /* -------------------------------------------------------------------------- *
 *                  OpenSim:  AbstractDataTable.cpp                           *
 * -------------------------------------------------------------------------- *
 * The OpenSim API is a toolkit for musculoskeletal modeling and simulation.  *
 * See http://opensim.stanford.edu and the NOTICE file for more information.  *
 * OpenSim is developed at Stanford University and supported by the US        *
 * National Institutes of Health (U54 GM072970, R24 HD065690) and by DARPA    *
 * through the Warrior Web program.                                           *
 *                                                                            *
 * Copyright (c) 2005-2017 Stanford University and the Authors                *
 * Authors:                                                                   *
 *                                                                            *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may    *
 * not use this file except in compliance with the License. You may obtain a  *
 * copy of the License at http://www.apache.org/licenses/LICENSE-2.0.         *
 *                                                                            *
 * Unless required by applicable law or agreed to in writing, software        *
 * distributed under the License is distributed on an "AS IS" BASIS,          *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
 * See the License for the specific language governing permissions and        *
 * limitations under the License.                                             *
 * -------------------------------------------------------------------------- */

#include "AbstractDataTable.h"

namespace OpenSim {

size_t AbstractDataTable::getNumRows() const {
    return implementGetNumRows();
}

size_t AbstractDataTable::getNumColumns() const {
    return implementGetNumColumns();
}

void 
AbstractDataTable::removeTableMetaDataKey(const std::string& key) {
    _tableMetaData.removeValueForKey(key);
}

std::vector<std::string> 
AbstractDataTable::getTableMetaDataKeys() const {
    return _tableMetaData.getKeys();
}

const AbstractDataTable::TableMetaData& 
AbstractDataTable::getTableMetaData() const {
    return _tableMetaData;
}

AbstractDataTable::TableMetaData& 
AbstractDataTable::updTableMetaData() {
    return _tableMetaData;
}

const AbstractDataTable::IndependentMetaData& 
AbstractDataTable::getIndependentMetaData() const {
    return _independentMetaData;
}

void 
AbstractDataTable::setIndependentMetaData(const IndependentMetaData& 
                                          independentMetaData) {
    _independentMetaData = independentMetaData;
    validateIndependentMetaData();
}

const AbstractDataTable::DependentsMetaData& 
AbstractDataTable::getDependentsMetaData() const {
    return _dependentsMetaData;
}

void 
AbstractDataTable::setDependentsMetaData(const DependentsMetaData& 
                                         dependentsMetaData) {
    _dependentsMetaData = dependentsMetaData;
    validateDependentsMetaData();
}

void
AbstractDataTable::removeDependentsMetaDataForKey(const std::string& key) {
    _dependentsMetaData.removeValueForKey(key);
}

bool
AbstractDataTable::hasColumnLabels() const {
    return _dependentsMetaData.hasKey("labels");
}

std::vector<std::string> 
AbstractDataTable::getColumnLabels() const {
    OPENSIM_THROW_IF(!hasColumnLabels(),
                     NoColumnLabels);

    const auto& absArray = 
        _dependentsMetaData.getValueArrayForKey("labels");
    std::vector<std::string> labels{};
    for(size_t i = 0; i < absArray.size(); ++i)
        labels.push_back(absArray[i].getValue<std::string>());

    return labels;
}

const std::string& 
AbstractDataTable::getColumnLabel(const size_t columnIndex) const {
    OPENSIM_THROW_IF(!hasColumnLabels(),
                     NoColumnLabels);

    const auto& labels = 
        _dependentsMetaData.getValueArrayForKey("labels");

    OPENSIM_THROW_IF(columnIndex >= labels.size(),
                     ColumnIndexOutOfRange,
                     columnIndex, 0, 
                     static_cast<unsigned>(labels.size() - 1));

    return labels[columnIndex].getValue<std::string>();
}

void 
AbstractDataTable::setColumnLabels(const std::initializer_list<std::string>&
                                   columnLabels) {
    setColumnLabels(columnLabels.begin(), columnLabels.end());
}

void 
AbstractDataTable::setColumnLabel(const size_t columnIndex,
                                  const std::string& columnLabel) {
    using namespace SimTK;
    using namespace std;

    OPENSIM_THROW_IF(!hasColumnLabels(),
                     NoColumnLabels);

    ValueArray<std::string> newLabels{};
    const auto& oldLabels = 
        _dependentsMetaData.getValueArrayForKey("labels");

    OPENSIM_THROW_IF(columnIndex >= oldLabels.size(),
                     ColumnIndexOutOfRange,
                     columnIndex, 0, 
                     static_cast<unsigned>(oldLabels.size() - 1));

    for(unsigned i = 0; i < oldLabels.size(); ++i) {
        if(i == columnIndex) {
            newLabels.upd().push_back(Value<string>(columnLabel));
        } else {
            auto value = Value<string>(oldLabels[i].getValue<string>());
            newLabels.upd().push_back(value);
        }
    }

    _dependentsMetaData.removeValueArrayForKey("labels");
    _dependentsMetaData.setValueArrayForKey("labels", newLabels);

    validateDependentsMetaData();
}

size_t 
    const auto& absArray = 
        _dependentsMetaData.getValueArrayForKey("labels");
    for(size_t i = 0; i < absArray.size(); ++i)
        if(absArray[i].getValue<std::string>() == columnLabel)
            return i;

    OPENSIM_THROW(KeyNotFound, columnLabel);
}

bool 
AbstractDataTable::hasColumn(const std::string& columnLabel) const {
    OPENSIM_THROW_IF(!hasColumnLabels(),
                     NoColumnLabels);

    const auto& absArray = 
        _dependentsMetaData.getValueArrayForKey("labels");
    for(size_t i = 0; i < absArray.size(); ++i)
        if(absArray[i].getValue<std::string>() == columnLabel)
            return true;

    return false;
}

bool 
AbstractDataTable::hasColumn(const size_t columnIndex) const {
    return columnIndex < getNumColumns();
}

void
AbstractDataTable::appendColumnLabel(const std::string& columnLabel) {
    auto& absArray = _dependentsMetaData.updValueArrayForKey("labels");
    auto& labels = static_cast<ValueArray<std::string>&>(absArray);
    labels.upd().push_back(SimTK::Value<std::string>{columnLabel});

    validateDependentsMetaData();
}

} // namespace OpenSim

<MSG> Make setColumnLabel use setColumnLabels to take advantage of labels validation being performed by setColumnLabels
Streamline setColumnLabel().

<DFF> @@ -132,28 +132,21 @@ AbstractDataTable::setColumnLabel(const size_t columnIndex,
     OPENSIM_THROW_IF(!hasColumnLabels(),
                      NoColumnLabels);
 
-    ValueArray<std::string> newLabels{};
-    const auto& oldLabels = 
-        _dependentsMetaData.getValueArrayForKey("labels");
+    auto labels = getColumnLabels();
 
-    OPENSIM_THROW_IF(columnIndex >= oldLabels.size(),
-                     ColumnIndexOutOfRange,
-                     columnIndex, 0, 
-                     static_cast<unsigned>(oldLabels.size() - 1));
-
-    for(unsigned i = 0; i < oldLabels.size(); ++i) {
-        if(i == columnIndex) {
-            newLabels.upd().push_back(Value<string>(columnLabel));
-        } else {
-            auto value = Value<string>(oldLabels[i].getValue<string>());
-            newLabels.upd().push_back(value);
-        }
+    if(columnIndex == labels.size()) {
+        labels.push_back(columnLabel);
+    }
+    else if(columnIndex < labels.size()) {
+        labels.at(columnIndex) = columnLabel;
+    }
+    else {
+        OPENSIM_THROW(ColumnIndexOutOfRange,
+            columnIndex, 0,
+            static_cast<unsigned>(labels.size()) );
     }
 
-    _dependentsMetaData.removeValueArrayForKey("labels");
-    _dependentsMetaData.setValueArrayForKey("labels", newLabels);
-
-    validateDependentsMetaData();
+    setColumnLabels(labels);
 }
 
 size_t 

{'commit': 'diff --git a/OpenSim/Common/AbstractDataTable.cpp b/OpenSim/Common/AbstractDataTable.cpp\nindex ddc38b7a1..4211334f3 100644\n--- a/OpenSim/Common/AbstractDataTable.cpp\n+++ b/OpenSim/Common/AbstractDataTable.cpp\n@@ -132,28 +132,21 @@ AbstractDataTable::setColumnLabel(const size_t columnIndex,\n     OPENSIM_THROW_IF(!hasColumnLabels(),\n                      NoColumnLabels);\n \n-    ValueArray<std::string> newLabels{};\n-    const auto& oldLabels = \n-        _dependentsMetaData.getValueArrayForKey("labels");\n+    auto labels = getColumnLabels();\n \n-    OPENSIM_THROW_IF(columnIndex >= oldLabels.size(),\n-                     ColumnIndexOutOfRange,\n-                     columnIndex, 0, \n-                     static_cast<unsigned>(oldLabels.size() - 1));\n-\n-    for(unsigned i = 0; i < oldLabels.size(); ++i) {\n-        if(i == columnIndex) {\n-            newLabels.upd().push_back(Value<string>(columnLabel));\n-        } else {\n-            auto value = Value<string>(oldLabels[i].getValue<string>());\n-            newLabels.upd().push_back(value);\n-        }\n+    if(columnIndex == labels.size()) {\n+        labels.push_back(columnLabel);\n+    }\n+    else if(columnIndex < labels.size()) {\n+        labels.at(columnIndex) = columnLabel;\n+    }\n+    else {\n+        OPENSIM_THROW(ColumnIndexOutOfRange,\n+            columnIndex, 0,\n+            static_cast<unsigned>(labels.size()) );\n     }\n \n-    _dependentsMetaData.removeValueArrayForKey("labels");\n-    _dependentsMetaData.setValueArrayForKey("labels", newLabels);\n-\n-    validateDependentsMetaData();\n+    setColumnLabels(labels);\n }\n \n size_t \n', 'commit_subject': 'Make setColumnLabel use setColumnLabels to take advantage of labels validation being performed by setColumnLabels Streamline setColumnLabel().', 'repo_name': 'opensim-org/opensim-core', 'committer': {'name': 'Ajay Seth', 'email': 'b506629e4698a937c5166aa6069ad6008e969ba5@stanford.edu', 'time_sec': '1526798698', 'tz_offset': '-420', 'date': {'seconds': '1526798698'}}, 'commit_tree': 'dc18a02067bfadd14573a4ce17fbfc76ddc21263', 'commit_message': 'Make setColumnLabel use setColumnLabels to take advantage of labels validation being performed by setColumnLabels\nStreamline setColumnLabel().\n', 'language_name': 'Java', 'license': 'apache-2.0', 'hunks': '@@ -132,28 +132,21 @@ AbstractDataTable::setColumnLabel(const size_t columnIndex,\n     OPENSIM_THROW_IF(!hasColumnLabels(),\n                      NoColumnLabels);\n \n-    ValueArray<std::string> newLabels{};\n-    const auto& oldLabels = \n-        _dependentsMetaData.getValueArrayForKey("labels");\n+    auto labels = getColumnLabels();\n \n-    OPENSIM_THROW_IF(columnIndex >= oldLabels.size(),\n-                     ColumnIndexOutOfRange,\n-                     columnIndex, 0, \n-                     static_cast<unsigned>(oldLabels.size() - 1));\n-\n-    for(unsigned i = 0; i < oldLabels.size(); ++i) {\n-        if(i == columnIndex) {\n-            newLabels.upd().push_back(Value<string>(columnLabel));\n-        } else {\n-            auto value = Value<string>(oldLabels[i].getValue<string>());\n-            newLabels.upd().push_back(value);\n-        }\n+    if(columnIndex == labels.size()) {\n+        labels.push_back(columnLabel);\n+    }\n+    else if(columnIndex < labels.size()) {\n+        labels.at(columnIndex) = columnLabel;\n+    }\n+    else {\n+        OPENSIM_THROW(ColumnIndexOutOfRange,\n+            columnIndex, 0,\n+            static_cast<unsigned>(labels.size()) );\n     }\n \n-    _dependentsMetaData.removeValueArrayForKey("labels");\n-    _dependentsMetaData.setValueArrayForKey("labels", newLabels);\n-\n-    validateDependentsMetaData();\n+    setColumnLabels(labels);\n }\n \n size_t \n', 'addition_count': 12, 'deletion_count': 19, 'src_file': 'a/OpenSim/Common/AbstractDataTable.cpp', 'tgt_file': 'b/OpenSim/Common/AbstractDataTable.cpp', 'file_extension': '.cpp', 'before_file': '/* -------------------------------------------------------------------------- *\n *                  OpenSim:  AbstractDataTable.cpp                           *\n * -------------------------------------------------------------------------- *\n * The OpenSim API is a toolkit for musculoskeletal modeling and simulation.  *\n * See http://opensim.stanford.edu and the NOTICE file for more information.  *\n * OpenSim is developed at Stanford University and supported by the US        *\n * National Institutes of Health (U54 GM072970, R24 HD065690) and by DARPA    *\n * through the Warrior Web program.                                           *\n *                                                                            *\n * Copyright (c) 2005-2017 Stanford University and the Authors                *\n * Authors:                                                                   *\n *                                                                            *\n * Licensed under the Apache License, Version 2.0 (the "License"); you may    *\n * not use this file except in compliance with the License. You may obtain a  *\n * copy of the License at http://www.apache.org/licenses/LICENSE-2.0.         *\n *                                                                            *\n * Unless required by applicable law or agreed to in writing, software        *\n * distributed under the License is distributed on an "AS IS" BASIS,          *\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *\n * See the License for the specific language governing permissions and        *\n * limitations under the License.                                             *\n * -------------------------------------------------------------------------- */\n\n#include "AbstractDataTable.h"\n\nnamespace OpenSim {\n\nsize_t AbstractDataTable::getNumRows() const {\n    return implementGetNumRows();\n}\n\nsize_t AbstractDataTable::getNumColumns() const {\n    return implementGetNumColumns();\n}\n\nvoid \nAbstractDataTable::removeTableMetaDataKey(const std::string& key) {\n    _tableMetaData.removeValueForKey(key);\n}\n\nstd::vector<std::string> \nAbstractDataTable::getTableMetaDataKeys() const {\n    return _tableMetaData.getKeys();\n}\n\nconst AbstractDataTable::TableMetaData& \nAbstractDataTable::getTableMetaData() const {\n    return _tableMetaData;\n}\n\nAbstractDataTable::TableMetaData& \nAbstractDataTable::updTableMetaData() {\n    return _tableMetaData;\n}\n\nconst AbstractDataTable::IndependentMetaData& \nAbstractDataTable::getIndependentMetaData() const {\n    return _independentMetaData;\n}\n\nvoid \nAbstractDataTable::setIndependentMetaData(const IndependentMetaData& \n                                          independentMetaData) {\n    _independentMetaData = independentMetaData;\n    validateIndependentMetaData();\n}\n\nconst AbstractDataTable::DependentsMetaData& \nAbstractDataTable::getDependentsMetaData() const {\n    return _dependentsMetaData;\n}\n\nvoid \nAbstractDataTable::setDependentsMetaData(const DependentsMetaData& \n                                         dependentsMetaData) {\n    _dependentsMetaData = dependentsMetaData;\n    validateDependentsMetaData();\n}\n\nvoid\nAbstractDataTable::removeDependentsMetaDataForKey(const std::string& key) {\n    _dependentsMetaData.removeValueForKey(key);\n}\n\nbool\nAbstractDataTable::hasColumnLabels() const {\n    return _dependentsMetaData.hasKey("labels");\n}\n\nstd::vector<std::string> \nAbstractDataTable::getColumnLabels() const {\n    OPENSIM_THROW_IF(!hasColumnLabels(),\n                     NoColumnLabels);\n\n    const auto& absArray = \n        _dependentsMetaData.getValueArrayForKey("labels");\n    std::vector<std::string> labels{};\n    for(size_t i = 0; i < absArray.size(); ++i)\n        labels.push_back(absArray[i].getValue<std::string>());\n\n    return labels;\n}\n\nconst std::string& \nAbstractDataTable::getColumnLabel(const size_t columnIndex) const {\n    OPENSIM_THROW_IF(!hasColumnLabels(),\n                     NoColumnLabels);\n\n    const auto& labels = \n        _dependentsMetaData.getValueArrayForKey("labels");\n\n    OPENSIM_THROW_IF(columnIndex >= labels.size(),\n                     ColumnIndexOutOfRange,\n                     columnIndex, 0, \n                     static_cast<unsigned>(labels.size() - 1));\n\n    return labels[columnIndex].getValue<std::string>();\n}\n\nvoid \nAbstractDataTable::setColumnLabels(const std::initializer_list<std::string>&\n                                   columnLabels) {\n    setColumnLabels(columnLabels.begin(), columnLabels.end());\n}\n\nvoid \nAbstractDataTable::setColumnLabel(const size_t columnIndex,\n                                  const std::string& columnLabel) {\n    using namespace SimTK;\n    using namespace std;\n\n    OPENSIM_THROW_IF(!hasColumnLabels(),\n                     NoColumnLabels);\n\n    ValueArray<std::string> newLabels{};\n    const auto& oldLabels = \n        _dependentsMetaData.getValueArrayForKey("labels");\n\n    OPENSIM_THROW_IF(columnIndex >= oldLabels.size(),\n                     ColumnIndexOutOfRange,\n                     columnIndex, 0, \n                     static_cast<unsigned>(oldLabels.size() - 1));\n\n    for(unsigned i = 0; i < oldLabels.size(); ++i) {\n        if(i == columnIndex) {\n            newLabels.upd().push_back(Value<string>(columnLabel));\n        } else {\n            auto value = Value<string>(oldLabels[i].getValue<string>());\n            newLabels.upd().push_back(value);\n        }\n    }\n\n    _dependentsMetaData.removeValueArrayForKey("labels");\n    _dependentsMetaData.setValueArrayForKey("labels", newLabels);\n\n    validateDependentsMetaData();\n}\n\nsize_t \n    const auto& absArray = \n        _dependentsMetaData.getValueArrayForKey("labels");\n    for(size_t i = 0; i < absArray.size(); ++i)\n        if(absArray[i].getValue<std::string>() == columnLabel)\n            return i;\n\n    OPENSIM_THROW(KeyNotFound, columnLabel);\n}\n\nbool \nAbstractDataTable::hasColumn(const std::string& columnLabel) const {\n    OPENSIM_THROW_IF(!hasColumnLabels(),\n                     NoColumnLabels);\n\n    const auto& absArray = \n        _dependentsMetaData.getValueArrayForKey("labels");\n    for(size_t i = 0; i < absArray.size(); ++i)\n        if(absArray[i].getValue<std::string>() == columnLabel)\n            return true;\n\n    return false;\n}\n\nbool \nAbstractDataTable::hasColumn(const size_t columnIndex) const {\n    return columnIndex < getNumColumns();\n}\n\nvoid\nAbstractDataTable::appendColumnLabel(const std::string& columnLabel) {\n    auto& absArray = _dependentsMetaData.updValueArrayForKey("labels");\n    auto& labels = static_cast<ValueArray<std::string>&>(absArray);\n    labels.upd().push_back(SimTK::Value<std::string>{columnLabel});\n\n    validateDependentsMetaData();\n}\n\n} // namespace OpenSim\n', 'message': 'Make setColumnLabel use setColumnLabels to take advantage of labels validation being performed by setColumnLabels\nStreamline setColumnLabel().\n', 'author_name': 'Ajay Seth', 'lang': 'cpp'}