// File: /workspace/github_cpp_makefile_star2_O3/jkom-cloud[P]mmseg/src/Mmseg.cpp
// Function: mmsegSpace::MMSeg::segment(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, int)
std::string MMSeg::segment(const std::string& ss, int depth) {
    std::vector<String> retv;
    std::string ret = "";
    mtx.lock();
    if (is_inited == 0) {
      fprintf(stderr, "PLease load dictionary first!\n");
      mtx.unlock();
      return ret;
    }

    String s = TransCode::from_utf8(ss);

    auto start = s.begin(), end = s.end();
    while (start != end) {
      auto chunks = get_chunks(start, end, depth);
      //std::cout<<"Chunks Length:"<<chunks.size()<<std::endl;

      auto best = std::max_element(chunks.begin(), chunks.end(), [&](const Chunk& x, const Chunk& y) {
        mtx.unlock();
        return std::tie(x.length_, x.mean_, x.var_, x.degree_) < std::tie(y.length_, y.mean_, y.var_, y.degree_);
      });

      auto& word = best->words_.front();// 取出分词结果最好的第一个分词结果
      start += length(word);// 处理后续字串

      auto& inDict = best->inDict_.front();
      if (inDict != 0)
        retv.emplace_back(String(word.first, word.second));

#ifdef DEBUG_LEVEL // 输出计算过程
      static int times;
      if(s.begin() == word.first)
        times = 1;//处理一个新的字串

      std::cout<<"Step: "<<times<<std::endl;

      for(auto &item:chunks) {
        cout<<item.to_string()<<std::endl;
      }

      //std::cout<<"Best one is: "<<TransCode::to_utf8(retv.back())<<std::endl;
      std::cout<<"Best one is: "<<TransCode::to_utf8(String(word.first, word.second))<<std::endl;
      ++times;
#endif
    }// while
#ifdef DEBUG_LEVEL
    std::cout<<"Result is: ";
#endif

    for(auto &m : retv) {
      ret += TransCode::to_utf8(m);
      ret += " ";
    }

    mtx.unlock();
    return ret;
  }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
vxorps %xmm0, %xmm0, %xmm0
leaq 0x10(%rdi), %r15
movl %ecx, 0x2c(%rsp)
movq %rdx, %rbx
movq %rsi, %rbp
movq $0x0, 0x40(%rsp)
vmovaps %xmm0, 0x30(%rsp)
movq %r15, (%rdi)
leaq 0x3868(%rip), %rdx      # 0x61ba
movq %rdi, (%rsp)
movq %rdx, %rsi
callq 0x42c0
leaq 0x8(%rbp), %r14
movq %r14, %rdi
callq 0x2320
movq %r15, 0x70(%rsp)
testl %eax, %eax
jne 0x2bad
cmpb $0x0, (%rbp)
je 0x2ba3
leaq 0x60(%rsp), %r15
movq %r15, -0x10(%r15)
movq $0x0, -0x8(%r15)
movw $0x0, (%r15)
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
leaq 0x50(%rsp), %rdx
callq 0x3aff
movq 0x58(%rsp), %rax
testq %rax, %rax
je 0x2ae9
movq 0x50(%rsp), %r12
leaq 0x80(%rsp), %rbx
leaq (%r12,%rax,2), %rcx
movq %rcx, 0x78(%rsp)
movl 0x2c(%rsp), %r8d
movq %rbx, %rdi
movq %rbp, %r13
movq %rbp, %rsi
movq %r12, %rdx
callq 0x27ec
movq 0x80(%rsp), %rbp
movq 0x88(%rsp), %r15
cmpq %r15, %rbp
movq %rbp, %rbx
sete %al
addq $0x48, %rbx
cmpq %r15, %rbx
sete %cl
orb %al, %cl
je 0x2a85
movq 0x18(%rbp), %rcx
movq (%rbp), %rax
movq %r13, %rbp
movq (%rcx), %rbx
movq 0x8(%rcx), %r15
cmpl $0x0, (%rax)
je 0x2a5f
leaq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
leaq 0x8(%rsp), %r13
movq %rbx, %rsi
movq %r15, %rdx
movq %r13, %rdi
callq 0x4356
leaq 0x30(%rsp), %rdi
movq %r13, %rsi
callq 0x375a
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x2a5f
callq 0x2240
subq %rbx, %r15
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
addq %r15, %r12
callq 0x3732
movq 0x78(%rsp), %rcx
cmpq %rcx, %r12
jne 0x29cd
jmp 0x2ae9
movq %r14, %rdi
callq 0x21d0
movq 0x30(%rbp), %rax
movq 0x30(%rbx), %rcx
cmpq %rcx, %rax
jae 0x2a9f
movq %rbx, %rbp
jmp 0x2aa1
jbe 0x2aaf
addq $0x48, %rbx
cmpq %r15, %rbx
jne 0x2a85
jmp 0x2a0a
vmovss 0x38(%rbp), %xmm0
vmovss 0x38(%rbx), %xmm1
vucomiss %xmm1, %xmm0
jb 0x2a9a
vucomiss %xmm0, %xmm1
jb 0x2aa1
vmovss 0x3c(%rbp), %xmm0
vmovss 0x3c(%rbx), %xmm1
vucomiss %xmm1, %xmm0
jb 0x2a9a
vucomiss %xmm0, %xmm1
jb 0x2aa1
vmovss 0x40(%rbp), %xmm0
vucomiss 0x40(%rbx), %xmm0
jb 0x2a9a
jmp 0x2aa1
movq 0x30(%rsp), %r13
movq 0x38(%rsp), %rbp
cmpq %rbp, %r13
je 0x2b67
leaq 0x18(%rsp), %rbx
leaq 0x8(%rsp), %r15
leaq 0x37bb(%rip), %r12      # 0x62c4
movq %rbx, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
movb $0x0, 0x18(%rsp)
movq 0x8(%r13), %rax
movq (%r13), %rdi
leaq (%rdi,%rax,2), %rsi
movq %r15, %rdx
callq 0x3ddc
movq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rsi
movq (%rsp), %rdi
callq 0x20c0
movq 0x8(%rsp), %rdi
cmpq %rbx, %rdi
je 0x2b52
callq 0x2240
movq (%rsp), %rdi
movq %r12, %rsi
callq 0x23f0
addq $0x20, %r13
cmpq %rbp, %r13
jne 0x2b09
movq %r14, %rdi
callq 0x21d0
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x2b83
callq 0x2240
leaq 0x30(%rsp), %rdi
callq 0x37b2
movq (%rsp), %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x2420
jmp 0x2b83
movl %eax, %edi
callq 0x2170
movq 0x50(%rsp), %rdi
movq %rax, %r14
cmpq %r15, %rdi
jne 0x2c22
jmp 0x2c27
movq %rax, %r14
jmp 0x2c27
movq %rax, %r14
jmp 0x2c3a
movq 0x8(%rsp), %rdi
movq %rax, %r14
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x2be9
callq 0x2240
jmp 0x2be9
movq %rax, %r14
leaq 0x80(%rsp), %rdi
callq 0x3732
jmp 0x2c13
jmp 0x2bfa
movq 0x8(%rsp), %rdi
movq %rax, %r14
cmpq %rbx, %rdi
je 0x2c13
callq 0x2240
jmp 0x2c13
jmp 0x2c10
movq %rax, %r14
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x2c27
callq 0x2240
movq (%rsp), %rax
movq (%rax), %rdi
cmpq 0x70(%rsp), %rdi
je 0x2c3a
callq 0x2240
leaq 0x30(%rsp), %rdi
callq 0x37b2
movq %r14, %rdi
callq 0x2390