Пример #1
0
  int findNthDigit(int n) {
    std::size_t num_pos(1), nine(9), digits_len(1);

    while (n > digits_len * nine) {
      n -= digits_len * nine;
      digits_len += 1; // digits_len is 1,2,3,4,.....
      nine *= 10; // 9, 90, 900,... nine represents how many numbers have 1,2,3,... digits
      num_pos *= 10; // 10, 100, 1000
    }

    num_pos += (n - 1) / digits_len;
    std::string num_str = std::to_string(num_pos);
    return num_str[(n - 1) % digits_len] - '0';

  }
Пример #2
0
/**
 * This function calculates the inverse of the entropy for a given set of examples on a special attribute.
 * This may be used to find the most important attribute of a set.
 */
double
gain(obj** examples, int num_examples, int attrib_num)
{
    if (!num_examples)
        return 0;
    int pos, pospos, ipos, ipospos;

    /* Calculate number of examples where attrib is 1 and 2 (i and not i) and where class is 1
     * See num_pos function comment for more expl.
     */
    num_pos(examples, num_examples, attrib_num, &pos, &pospos, &ipos, &ipospos);

    /* Sum all weighted entropies */
    double inv_gain = 0.0f;
    if (pos)
        inv_gain = ((double) pos / num_examples) * func_b((float) pospos / pos);
    if (ipos)
        inv_gain += ((double) ipos / num_examples)
                    * func_b((double) ipospos / ipos);

    /* And return the inverse */
    return 1 - inv_gain;
}
Пример #3
0
void eos::TextEditor::OnPaint()
{
    ax::GC gc;
    ax::Rect rect(GetDrawingRect());
    
    gc.SetColor(_info.bg_color);
    gc.DrawRectangle(ax::Rect(0, 0, rect.size.x, rect.size.y));
    
    // Draw line number background.
    gc.SetColor(_info.line_number_bg_color);
    gc.DrawRectangle(ax::Rect(0, 0, 25, rect.size.y));
    
    ax::Point num_pos(4, 2);
    
    gc.SetColor(_info.line_number_color);
    
    // Draw line number.
    for(int i = 0; i < _n_line_shown; i++)
    {
        int num = i + _file_start_index;
        std::string num_str = std::to_string(num);
        
        if(num < 10)
        {
            num_str = "  " + num_str;
        }
        else if(num < 100)
        {
            num_str = " " + num_str;
        }

        gc.DrawString(_line_num_font, num_str, num_pos);
        
        num_pos += ax::Point(0, 15);
    }
    
    
    
    
    // Text initial position.
    //ax::Point line_pos(4, 0);
    ax::Point line_pos(25 + 4, 0);
    
    _next_pos_data.clear();
    
    const ax::StringVector& data = _logic.GetFileData();
    
    // Set text color.
    gc.SetColor(_info.text_color);
    
    // Draw text.
    for(int i = 0, k = _file_start_index;
        k < data.size() && i < _n_line_shown; i++, k++)
    {
        const std::string& text = data[k];
        
        std::vector<int> next_vec(text.size() + 1);
        
        // Draw string.
        if(_font)
        {
            int x = line_pos.x;
            
            next_vec[0] = x;
//
            //----------------------------------------
//            ax::StringVector words = ax::Utils::String::Split(text, " ");
//            
//            int index = 0;
//            
//            for(auto& w : words)
//            {
//                std::string clean_word = RemoveSpecialChar(w);
//                
//                ax::Color word_color = _info.text_color;
//                
//                if(_key_words_cpp.find(clean_word) != _key_words_cpp.end())
//                {
//                    word_color = ax::Color(0.6627451, 0.05098039, 0.5686275);
//                }
//
//                for (int i = 0; i < w.size(); i++)
//                {
//                    gc.SetColor(word_color);
//                    
//                    if(text[index] == ' ')
//                    {
//                        i--;
//                        _font.SetChar(' ');
//                    }
//                    else
//                    {
//                        _font.SetChar(w[i]);
//                        
//                        if(is_special(w[i]))
//                        {
//                            gc.SetColor(_info.text_color);
//                        }
//                        else if(std::isdigit(w[i]))
//                        {
//                            gc.SetColor(ax::Color(0.0, 0.0, 1.0));
//                        }
//                    }
//                    
//                    ax::Point d = _font.GetDelta();
//                    
//                    ax::Point txtPos(x + d.x,
//                                     line_pos.y - d.y + _font.GetFontSize());
//                    
//                    ax::Rect txtRect(txtPos, _font.GetSize());
//                    gc.DrawTexture(_font.GetTexture(), txtRect);
//                    
//                    x += _font.GetNextPosition();
//                    
//                    next_vec[index + 1] = x;
//                    
//                    index++;
//                    
//                }
//            }
//            
//            _font.SetChar(' ');
//            ax::Point d = _font.GetDelta();
//            
//            while(index < text.size())
//            {
//                ax::Point txtPos(x + d.x,
//                                 line_pos.y - d.y + _font.GetFontSize());
//                ax::Rect txtRect(txtPos, _font.GetSize());
//                gc.DrawTexture(_font.GetTexture(), txtRect);
//                x += _font.GetNextPosition();
//                next_vec[index + 1] = x;
//                index++;
//            }
            //------------------------
        
            
            for (int i = 0; i < text.size(); i++)
            {
                _font.SetChar(text[i]);
                ax::Point d = _font.GetDelta();
                
                ax::Point txtPos(x + d.x,
                                 line_pos.y - d.y + _font.GetFontSize());
                
                ax::Rect txtRect(txtPos, _font.GetSize());
                gc.DrawTexture(_font.GetTexture(), txtRect);
                
                x += _font.GetNextPosition();
                
                next_vec[i + 1] = x;
            }
        }
        
        _next_pos_data.push_back(next_vec);
        line_pos += ax::Point(0, 15);
    }
    
    // Line cursor.
    ax::Point cursor_index = FileCursorPosToNextPosIndex();
    
    if(cursor_index.x != -1 && cursor_index.y != -1)
    {
        ax::Print("Draw cursor");
        int x = _next_pos_data[cursor_index.y][cursor_index.x];
        int y = cursor_index.y * _line_height;
        
//        gc.SetColor(_info.cursor_color);
        gc.SetColor(ax::Color(1.0, 0.0, 0.0));
        gc.DrawLine(ax::Point(x, y), ax::Point(x, y + _line_height));
    }
}