<commit_before>/**********************************************************************
 *
 * GEOS - Geometry Engine Open Source
 * http://geos.osgeo.org
 *
 * Copyright (C) 2020 <NAME> <<EMAIL>>
 *
 * This is free software; you can redistribute and/or modify it under
 * the terms of the GNU Lesser General Public Licence as published
 * by the Free Software Foundation.
 * See the COPYING file for more information.
 *
 **********************************************************************/

#include <util/NodingTestUtil.h>


using namespace geos::geom;
using namespace geos::noding;
using geos::noding::Noder;
using geos::io::WKTReader;
using geos::io::WKTWriter;
using geos::geom::util::LinearComponentExtracter;

namespace geos {

/*private static*/
std::unique_ptr<Geometry>
NodingTestUtil::toLines(const std::vector<SegmentString*>* nodedList, const GeometryFactory* geomFact)
{
    std::vector<std::unique_ptr<Geometry>> lines;

    for (auto nss : *nodedList) {
      CoordinateSequence* pts = nss->getCoordinates();
      // pts is owned by nss, so we make a copy to build the line
      // on top of. Lines are 100% self-contained and own all their parts.
      // Input nodedList can be freed.
      lines.emplace_back(geomFact->createLineString(pts->clone()));
    }
    if (lines.size() == 1) return std::move(lines[0]);

    // move the lines to pass ownership to the multiLineString
    return geomFact->createMultiLineString(std::move(lines));
}


/*private static*/
std::vector<SegmentString*>
NodingTestUtil::toSegmentStrings(std::vector<const LineString*>& lines)
{
    std::vector<SegmentString*> nssList;
    for (auto line : lines) {
        // line->getCoordinates() clones CoordinateSequence
        // into a unique_ptr<> which we have to release() to the
        // NodedSegmentString constructor, so
        // nss now owns nss->pts
        NodedSegmentString* nss = new NodedSegmentString(line->getCoordinates().release(), line);
        nssList.push_back(nss);
    }
    return nssList;
}

/*public static*/
std::unique_ptr<Geometry>
NodingTestUtil::nodeValidated(const Geometry* geom1, const Geometry* geom2, Noder* noder)
{
    std::vector<const LineString*> lines;
    // lines are const* to linear components of geom1, geom1 still
    // owns all coordinates, etc
    LinearComponentExtracter::getLines(*geom1, lines);
    if (geom2 != nullptr) {
        LinearComponentExtracter::getLines(*geom2, lines);
    }

    // ssList needs to be disposed after noder is done working
    std::vector<SegmentString*> ssList = toSegmentStrings(lines);

    ValidatingNoder noderValid(*noder);
    // computeNotes might alster ssList, but ssList still
    // holds all memory
    noderValid.computeNodes(&ssList);

    // getNodedSubstrings calls NodedSegmentString::getNodedSubStrings()
    // which creates new NodedSegmentString and new pts member, so complete
    // new copy of data. Can be disposed of after geometries are constructed
    // std::vector<SegmentString*>* nodedList = noderValid.getNodedSubstrings();
    std::vector<SegmentString*>* nodedList = noderValid.getNodedSubstrings();

    // Dispose of ssList
    for (auto ss: ssList) {
        delete ss;
    }

    std::unique_ptr<Geometry> lineGeom = toLines(nodedList, geom1->getFactory());

    // Dispose of nodedList
    for (auto nss: *nodedList) {
        delete nss;
    }

    return lineGeom;
}

} // geos

<commit_msg>remove leak in noding test?<commit_after>/**********************************************************************
 *
 * GEOS - Geometry Engine Open Source
 * http://geos.osgeo.org
 *
 * Copyright (C) 2020 <NAME> <<EMAIL>>
 *
 * This is free software; you can redistribute and/or modify it under
 * the terms of the GNU Lesser General Public Licence as published
 * by the Free Software Foundation.
 * See the COPYING file for more information.
 *
 **********************************************************************/

#include <util/NodingTestUtil.h>


using namespace geos::geom;
using namespace geos::noding;
using geos::noding::Noder;
using geos::io::WKTReader;
using geos::io::WKTWriter;
using geos::geom::util::LinearComponentExtracter;

namespace geos {

/*private static*/
std::unique_ptr<Geometry>
NodingTestUtil::toLines(const std::vector<SegmentString*>* nodedList, const GeometryFactory* geomFact)
{
    std::vector<std::unique_ptr<Geometry>> lines;

    for (auto nss : *nodedList) {
      CoordinateSequence* pts = nss->getCoordinates();
      // pts is owned by nss, so we make a copy to build the line
      // on top of. Lines are 100% self-contained and own all their parts.
      // Input nodedList can be freed.
      lines.emplace_back(geomFact->createLineString(pts->clone()));
    }
    if (lines.size() == 1) return std::move(lines[0]);

    // move the lines to pass ownership to the multiLineString
    return geomFact->createMultiLineString(std::move(lines));
}


/*private static*/
std::vector<SegmentString*>
NodingTestUtil::toSegmentStrings(std::vector<const LineString*>& lines)
{
    std::vector<SegmentString*> nssList;
    for (auto line : lines) {
        // line->getCoordinates() clones CoordinateSequence
        // into a unique_ptr<> which we have to release() to the
        // NodedSegmentString constructor, so
        // nss now owns nss->pts
        NodedSegmentString* nss = new NodedSegmentString(line->getCoordinates().release(), line);
        nssList.push_back(nss);
    }
    return nssList;
}

/*public static*/
std::unique_ptr<Geometry>
NodingTestUtil::nodeValidated(const Geometry* geom1, const Geometry* geom2, Noder* noder)
{
    std::vector<const LineString*> lines;
    // lines are const* to linear components of geom1, geom1 still
    // owns all coordinates, etc
    LinearComponentExtracter::getLines(*geom1, lines);
    if (geom2 != nullptr) {
        LinearComponentExtracter::getLines(*geom2, lines);
    }

    // ssList needs to be disposed after noder is done working
    std::vector<SegmentString*> ssList = toSegmentStrings(lines);

    ValidatingNoder noderValid(*noder);
    // computeNotes might alster ssList, but ssList still
    // holds all memory
    noderValid.computeNodes(&ssList);

    // getNodedSubstrings calls NodedSegmentString::getNodedSubStrings()
    // which creates new NodedSegmentString and new pts member, so complete
    // new copy of data. Can be disposed of after geometries are constructed
    // std::vector<SegmentString*>* nodedList = noderValid.getNodedSubstrings();
    std::vector<SegmentString*>* nodedList = noderValid.getNodedSubstrings();

    // Dispose of ssList
    for (auto ss: ssList) {
        delete ss;
    }

    std::unique_ptr<Geometry> lineGeom = toLines(nodedList, geom1->getFactory());

    // Dispose of nodedList
    for (auto nss: *nodedList) {
        delete nss;
    }
    delete nodedList;

    return lineGeom;
}

} // geos


{'fraction_non_alphanumeric': 0.15058139534883722, 'fraction_numerical': 0.004941860465116279, 'mean_word_length': 3.8836053938963806, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7301323', 'n_tokens_mistral': 2088, 'n_tokens_neox': 1996, 'n_words': 580}