Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compilation problems with multi-dimensional arrays #452

Open
henrywood opened this issue Sep 25, 2020 · 1 comment
Open

Compilation problems with multi-dimensional arrays #452

henrywood opened this issue Sep 25, 2020 · 1 comment

Comments

@henrywood
Copy link

henrywood commented Sep 25, 2020

First let me thank you for PHPCPP which is very cool and way nicer than doing PHP extensions in C !

However, I have stumbled on a problem I have no idea how to solve !

I am trying to translate this PHP function to CPP:

    private function merge_and_clear() {
        $det_regions = array();
        $this->skin_regions = array();

        foreach ($this->merge_regions as $i => $mr) {

            if (!isset($det_regions[$i])) {
                $det_regions[$i] = array();
            }

            foreach ($mr as $m) {
                if (!empty($this->detected_regions[$m])) {
                    $det_regions[$i] = array_merge($det_regions[$i], $this->detected_regions[$m]);
                }
                unset($this->detected_regions[$m]);
            }
        }

        if (!empty($this->detected_regions)) {
            foreach ($this->detected_regions as $dr) {
                $det_regions[] = $dr;
            }
        }

        // only pushes regions which are bigger than a specific amount to the final result
        foreach ($det_regions as $dt) {
            $count_dt = count($dt);
            if ($count_dt > 30) {
                $this->skin_regions[] = $count_dt;
            }
        }
    }

All arrays are multi-dimensional in the PHP code above.

My translation:

    /**
    * Get merge_regions pixel data, get only regions of certain size and store to skin_regions
    * data in skin_regions will be used to determine if picture contains nudity or not
    */
    private:
    void merge_and_clear() {

        Php::Value _s_det_regions = Php::Array();
        this->_s_skin_regions = this->_s_skin_regions(); // Empty the _s_skin_regions array

        //foreach ($this->merge_regions as $i => $mr) {
        //while(list(_s_i, _s_mr) = each(this->_s_merge_regions)) {
        for (auto &iter : this->_s_merge_regions) {

            int _s_i = (int) iter.first;
            Php::Value _s_mr = iter.second;

            if (! Php::isset(_s_det_regions[_s_i])) {

                Php::Value empty = Php::Array();
                _s_det_regions[_s_i] = empty;
            }

            //foreach ($mr as $m) {
            //for(size_t _s_kk=0; _s_kk < Php::count(_s_mr); _s_kk++) {
            for (auto &iter2 : _s_mr) {

                int _s_m = (int) iter2.second;

                if (!Php::empty(this->_s_detected_regions[_s_m])) {
                    //_s_det_regions[_s_i] = array_merge(_s_det_regions[_s_i], this->_s_detected_regions[_s_m]);

                    Php::Value temp = Php::call("array_merge", _s_det_regions[_s_i], this->_s_detected_regions[_s_m]); // THIS IS THE OFFENDING LINE
                    Php::Array temparr = temp;
                    _s_det_regions[_s_i] = temparr;

                    // FIXME
                    //_s_det_regions[_s_i] = Php::call("array_merge", _s_det_regions[_s_i], this->_s_detected_regions[_s_m]);
                    // FIXME END
                }

                Php::unset(this->_s_detected_regions[_s_m]);
            }

        } // for

        if (Php::count(this->_s_detected_regions) > 0) {

            //foreach ($this->detected_regions as $dr) {
            //for(size_t _s_kkk=0; _s_kkk < Php::count(this->_s_detected_regions); _s_kkk++) {
            for (auto &iter3 : this->_s_detected_regions) {

                int _s_dr = (int) iter3.second;
                //Php::Value _s_dr = this->_s_detected_regions[_s_kkk];

                //_s_det_regions[_APPEND_] = _s_dr;
                Php::array_push(_s_det_regions, _s_dr);
            }
        }

        // only pushes regions which are bigger than a specific amount to the final result
        //foreach ($det_regions as $dt) {
        //for (auto &iter4 : _s_det_regions) {
        int _s_det_regions_count = Php::count(_s_det_regions);
        for(size_t _s_k=0; _s_k < _s_det_regions_count; _s_k++) {

            Php::Value _s_dt = _s_det_regions[_s_k];
            //Php::Array _s_dt = iter4.second;

            int _s_count_dt = Php::count(_s_dt);

            if (_s_count_dt > 30) {
                //this->_s_skin_regions[_APPEND_] = _s_count_dt;
                Php::array_push(this->_s_skin_regions, _s_count_dt);
            }
        }
    }

The error I am getting is

Main.cpp: In member function ‘void NudityFilter::merge_and_clear()’:
Main.cpp:565:80: error: call of overloaded ‘Value(Php::HashMember)’ is ambiguous
Php::Value temp = Php::call("array_merge", _s_det_regions[_s_i], this->_s_detected_regions[_s_m]);

For information, the properties of the CPP class are declared as:

    private:
    std::string _s_file;            // full path of image file
    std::string  _s_filename;       // image file name
    int  _s_img_w;                  // image width
    int  _s_img_h;                  // image height
    int  _s_last_from;              // previous `from` region number
    int  _s_last_to;                // previous `to` region number
    Php::Value _s_pixel_map;        // array of skin pixel holding region number
    Php::Value _s_merge_regions;    // array of pixel number which are merged in a region
    Php::Value _s_detected_regions; // array of skin pixel for arranging region number
    Php::Value _s_skin_regions;     // array of skin regions, store number of pixels in a region
    public:
    Php::Array _s_log;              // array of log message

How can I fix this ?

Please help !

@henrywood
Copy link
Author

henrywood commented Sep 25, 2020

I found this function to merge arrays/vectors:

    /**
    * @author: Rosario Gueli <[email protected]>
    * @param std::vector<Type> array_1
    * @param std::vector<Type> array_2
    * @return std::vector<Type> mergedArrays
    *
    * Usage: std::vector<Rect> new_merged_vectors = arrMerge(vector_list_1, vector_list_2);
    */
    private:
    template <typename Type>
    std::vector<Type> arrMerge(std::vector<Type> array_1, std::vector<Type> array_2)
    {
         std::vector<Type> mergedArrays;
         mergedArrays.reserve( array_1.size() + array_2.size() ); // preallocate memory
         mergedArrays.insert( mergedArrays.end(), array_1.begin(), array_1.end() );
         mergedArrays.insert( mergedArrays.end(), array_2.begin(), array_2.end() );
         return mergedArrays;
    }

Maybe I can be used in place of the call to PHP's array_merge ?

However, I don't know how. Maybe you please show me ? Or you might add the arrMerge() function to PHPCPP in some form? (I am a beginner at c++)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant