Сортировка контейнера контейнеров по доставленной функции - PullRequest
0 голосов
/ 14 января 2020

У меня есть контейнер контейнеров, который мне нужно отсортировать по некоторой функции, которую я получаю в качестве параметра. Вот заголовочные файлы контейнеров:

using namespace std;
template <class Element, class Compare = std::equal_to<Element>>
class UniqueArray {

public:
    Element** data;
    unsigned int curr_size;
    unsigned int max_size;
    int* availability_array;

    explicit UniqueArray(unsigned int size);
    UniqueArray(const UniqueArray& other);
    ~UniqueArray();
//    UniqueArray& operator=(const UniqueArray&) = delete;
    unsigned int insert(const Element& element);
    bool getIndex(const Element& element, unsigned int& index) const;
    const Element* operator[] (const Element& element) const;
    bool remove(const Element& element);
    unsigned int getCount() const;
    unsigned int getSize() const;

    class Filter {
    public:
        virtual bool operator() (const Element&) const = 0;
    };
    UniqueArray filter(const Filter& f) const;

    class UniqueArrayIsFullException{};

    class Iterator{
    public:
        using iterator_category = std::random_access_iterator_tag;
        using value_type = Element;
        using refrence = Element&;

        Iterator(Element** data): data(data){}

        refrence operator*(){
            return **data;
        }

        Iterator& operator++(){
            ++data;
            return *this;
        }

        friend bool operator!=(Iterator it1, Iterator it2){
            return it1.data != it2.data;
        }

    private:
        Element** data;
    };

    Iterator begin(){
        return Iterator(data);
    }

    Iterator end(){
        return Iterator(data + max_size);
    }
};

Второй контейнер, который содержит предыдущий:

using namespace ParkingLotUtils;
using std::ostream;

namespace MtmParkingLot {


    class ParkingLot {
    public:

        UniqueArray<Vehicle, Vehicle::compareVehicles> motorbike_parking;
        UniqueArray<Vehicle, Vehicle::compareVehicles> car_parking;
        UniqueArray<Vehicle, Vehicle::compareVehicles> handicapped_parking;

        ParkingLot(unsigned int parkingBlockSizes[]);
        ~ParkingLot() = default;
        ParkingResult enterParking(VehicleType vehicleType, LicensePlate licensePlate, Time entranceTime);
        ParkingResult exitParking(LicensePlate licensePlate, Time exitTime);
        ParkingResult getParkingSpot(LicensePlate licensePlate, ParkingSpot& parkingSpot) const;
        void inspectParkingLot(Time inspectionTime);
        friend ostream& operator<<(ostream& os, const ParkingLot& parkingLot);
        int calculateFee(Time entryTime, Time exitTime, VehicleType type, Vehicle& v);
        int calculateFeeRecursive(Time entryTime, Time exitTime, VehicleType type, int iter, int totalPrice);
        bool isVehicleInLot(LicensePlate licensePlate, VehicleType& type, unsigned int& index);
    };
}

предоставил объект ParkingLot и функцию сравнения, которая сравнивает на ParkingSpot которая является Vehicle переменной-членом, как я могу отсортировать всю парковку сразу?

Мысль об использовании std::sort, но я не уверен, как это реализовать.

РЕДАКТИРОВАТЬ:

Заголовок Vechile:

using namespace ParkingLotUtils;
namespace MtmParkingLot {
    class Vehicle {
    public:

        LicensePlate licensePlate;
        Time entryTime;
        VehicleType type;
        ParkingSpot spot;
        bool fine;

        Vehicle(LicensePlate plate, ParkingSpot spot, Time entry_time = 0, VehicleType type = CAR, bool fine = false);
        ~Vehicle() = default;
        Time getEntryTime() const;
        VehicleType getType() const;
        LicensePlate getLicensePlate() const;
        ParkingSpot vehicleGetParkingSpot() const;
        bool isVehicleFined() const;

        class compareVehicles{
        public:
            compareVehicles() = default;
            bool operator() (const Vehicle& v1, const Vehicle& v2){
                return (v1.licensePlate.compare(v2.licensePlate) == 0);
            }
        };
    };
}

Заголовок ParkingSpot:

namespace ParkingLotUtils {

    using std::ostream;

    /**
     * @brief Represents a Parking Spot (Block + Number) of a Vehicle.
     * 
     */
    class ParkingSpot {
    public:

        /**
         * @brief Construct a new Parking Spot object
         * 
         * @param parkingBlock The Parking Block of the vehicle (represented by VehicleType enum)
         * @param parkingNumber The number of the parking spot within the block
         */
        ParkingSpot(VehicleType parkingBlock = FIRST, unsigned int parkingNumber = 0);

        /**
         * @brief Get the Parking Block of this ParkingSpot
         * 
         * @return VehicleType The Parking Block (represented by VehicleType enum)
         */
        VehicleType getParkingBlock() const;

        /**
         * @brief Get the Parking Number of this ParkingSpot
         * 
         * @return unsigned int The number of the parking spot within the block
         */
        unsigned int getParkingNumber() const;

        /**
         * @brief Compares given ParkingSpot objects
         * 
         * @param ps1
         * @param ps2 
         * @return true If ps1 < ps2
         * @return false otherwise
         */
        friend bool operator< (const ParkingSpot& ps1, const ParkingSpot& ps2);

        /**
         * @brief Prints given ParkingSpot object
         * 
         * @param os output stream to print into
         * @param parkingSpot ParkingSpot object to print
         * @return ostream& output stream after the print
         */
        friend ostream& operator<< (ostream& os, const ParkingSpot& parkingSpot);

    private:
        enum VehicleType parkingBlock;
        unsigned int parkingNumber;
    };

}

РЕДАКТИРОВАТЬ 3: Обновлен файл заголовка для UniqueArray - включая итераторы

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...