0

I have a container of containers which I need to sort by some function I get as a parameter. Here's the header files of the containers :

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);
    }
};

The second container which containes the previous one :

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);
    };
}

provided an ParkingLot object and a compare function which compares by ParkingSpot which is a Vehicle member variable , how can I sort the whole parking lot at once ?

Thought of using std::sort but i'm not sure on how to implement this.

EDIT:

Vechile header :

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 header :

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;
    };

}

EDIT 3 : Updated the header file for UniqueArray - including iterators

Eliran Turgeman
  • 1,526
  • 2
  • 16
  • 34
  • Is `Vehicle::compareVehicles` a static or non static member function? – NathanOliver Jan 14 '20 at 17:09
  • @NathanOliver non static. here's how It is defined (a nested class inside Vehicle class) ```class compareVehicles{ public: compareVehicles() = default; bool operator() (const Vehicle& v1, const Vehicle& v2){ return (v1.licensePlate.compare(v2.licensePlate) == 0); } }; ``` – Eliran Turgeman Jan 14 '20 at 17:11
  • Oh, its a functor. [This](https://stackoverflow.com/questions/1380463/sorting-a-vector-of-custom-objects) should answer your question then. – NathanOliver Jan 14 '20 at 17:13
  • @NathanOliver correct me if i'm wrong but according to the post you linked to this code should work ? ```vector> LotVector = {ParkingLot::car_parking, ParkingLot::motorbike_parking, ParkingLot::handicapped_parking}; sort(LotVector.begin(), LotVector.end(), ParkingSpot::operator<()); ``` Im confused about what ```LotVector.begin()``` holds.. is it ```car_parking```? if so , it actually need to iterate through the ```Vehicles``` of each ```UniqueArray``` – Eliran Turgeman Jan 14 '20 at 17:17
  • If your type has an `operator <` defined for it you just need `sort(LotVector.begin(), LotVector.end())`. You only need the third parameter when you don't want to use, or don't have, `operator <` – NathanOliver Jan 14 '20 at 17:18
  • I have an overriden ```operator < ``` for ```ParkingSpot``` but if im right ```LotVector.begin()``` holds an ```UniqueArray``` so isn't that necessary to specify ? – Eliran Turgeman Jan 14 '20 at 17:22
  • Oh. I'm not sure exactly how you would call it since I don't know what a `ParkingSpot` is and how it relates to an `UniqueArray` – NathanOliver Jan 14 '20 at 17:29
  • I have edited the post providing the header files of ```Vehicle``` and ```ParkingSpot``` – Eliran Turgeman Jan 14 '20 at 17:33
  • @EliranTurgeman I'm sorry, but I see absolutely no usage of `vector`, or any types suitable for `std::sort` in your code. You have the home-made `UniqueArray`, but nothing in there can be used for the first two parameters for `std::sort`, i.e. no iterator type that is compatible. – PaulMcKenzie Jan 14 '20 at 18:34
  • @PaulMcKenzie can you elaborate on the changes that has to be made? – Eliran Turgeman Jan 14 '20 at 18:37
  • 1
    @EliranTurgeman For example, try to get this to compile: `std::sort(motorbike_parking.begin(), motorbike_parking.end());` -- When `std::sort` needs to pick two items to give to the sorting criteria, how will it do it? The iterator type for `UniqueArray` must support random access. I think you needed to solve your issue by working bottom up, not top down. Get `UniqueArray` to be "sortable" first before thinking about sorting 3 `UniqueArrays` at once. – PaulMcKenzie Jan 14 '20 at 18:41
  • @PaulMcKenzie How can I ensure that the iterator for ```UniqueArray``` supports random access ? – Eliran Turgeman Jan 15 '20 at 09:07
  • I have made an edit to the post, implementing an `Iterator` class. – Eliran Turgeman Jan 15 '20 at 12:02

0 Answers0