Thread: optimizaton

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    8

    optimizaton

    Let’s say you have few functions:
    func_1(arg1, arg2, arg3) ,
    func_2 (arg1, arg2) and
    func_3 (arg1, arg2, arg3).
    All 3 performing similar steps:
    Code:
    func (arg1, arg2, arg3)
    {
    Read line from the file
    validate_func()
    Store results into arg3
    }
    So my question is – what would best way to approach this in order to combine these 3 functions into 1?
    My thoughts to approach this problem to use function with variable number of arguments (va_start, va_end) . What i’m not sure how to approach is to call appropriate validation function – will it be possible to use in this case function pointer as one of the arguments?Not sure at this point
    Any suggestions, links, examples are welcome
    Thanks

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Is the validation function the only thing that's different?

    If so, this would be a typical use case for a function pointer.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    30
    If this is C++, you can use functors. Functors are basically objects with operator() overloaded. And the variable number of arguments to functions can be used to initialize these functor objects.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    8
    Cyberfish & Salquest, thanks for your replay
    that is corect - only validation is different.
    I'll look at both suggestions, by any chance do you have link where I could look at the 'way' you suggested?

    Thanks again

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    8
    Guys,
    Just would like to point out one thing that all ‘func’ could have different numbers of arguments and ‘storage’ argument is different type

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Can you give a minimal compilable example?

    If they have different number of arguments, how can the functions be identical?

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    8
    Extra argment used in validation
    Sorry, no code now

  8. #8
    Registered User
    Join Date
    Jul 2010
    Posts
    8
    Code:
    #include "stdafx.h"
    #include "vector"
    #include "list"
    #include "map"
    #include "fstream"
    
    using namespace std;
    
    void ReadFile_FillStruct_1(string _fileName, vector<string>& _vectorStorage, bool _boolArg);
    void ReadFile_FillStruct_2(string _fileName, vector<int>& _vectorStorage);
    void ReadFile_FillStruct_3(string _fileName, list<string>& _listStorage, bool _boolArg);
    void ReadFile_FillStruct_4(string _fileName, map<int, string>& _mapStorage, bool _boolArg);
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        vector<string> _vector_1;
        vector<int> _vector_2;
        list<string> _list;
        map <int,string> _map;
        string _fileName = "C:\\Temp\\FileName.txt";
        ReadFile_FillStruct_1(_fileName, _vector_1, false);
    
        _fileName = "C:\\Temp\\FileName1.txt";
        ReadFile_FillStruct_3(_fileName, _list, true);
    
        _fileName = "C:\\Temp\\FileName2.txt";
        ReadFile_FillStruct_2(_fileName, _vector_2);
    
        _fileName = "C:\\Temp\\FileName3.txt";
        ReadFile_FillStruct_4(_fileName, _map, false);
    	return 0;
    }
    
    void ReadFile_FillStruct_1(string _fileName, vector<string>& _vectorStorage, bool _boolArg)
    {
    	ifstream infile;
        infile.open (_fileName.c_str());
        while(!infile.eof()) 
        {
        // parse data, check if it is meaningful(same for all func)******
        // perform necessary munipulation/validation based on _boolArg
        // fill the container
        }
    	infile.close();
    }
    
    void ReadFile_FillStruct_2(string _fileName, vector<int>& _vectorStorage)
    {
        ifstream infile;
        infile.open (_fileName.c_str());
        while(!infile.eof()) 
        {
        // parse data, check if it is meaningful(same for all func)******
        // fill the container
        }
    	infile.close();
    }
    
    void ReadFile_FillStruct_3(string _fileName, list<string>& _listStorage, bool _boolArg)
    {
        ifstream infile;
        infile.open (_fileName.c_str());
        while(!infile.eof()) 
        {
        // parse data, check if it is meaningful(same for all func)******
        // perform necessary munipulation based on _boolArg
        // fill the container
        }
    	infile.close();
    }
    
    void ReadFile_FillStruct_4(string _fileName, map<int, string>& _mapStorage, bool _boolArg)
    {	
        ifstream infile;
        infile.open (_fileName.c_str());
        while(!infile.eof()) 
        {
        // parse data, check if it is meaningful(same for all func)******
        // perform necessary munipulation based on _boolArg
        // fill the container
        }
    	infile.close();
    }

  9. #9
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    If the validate function can have different signatures, I can't think of an easier way.

    You could try taking out the common code and put them in functions instead.

    Sidenotes:
    1) standard headers should use <> not "".
    Code:
    #include <vector>
    "" is for your own headers.

    2) names starting with an underscore (_) are reserved for implementation use. Don't use them.

  10. #10
    Registered User
    Join Date
    Jul 2010
    Posts
    8
    Thanks Cyberfish,
    All noted

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Well, you could store the additional arguments in the function objects. That might get you back to identical call signatures.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed