// File: 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:
subq $0x198, %rsp            # imm = 0x198
movq %rdi, 0x38(%rsp)
movq %rdi, %rax
movq %rax, 0x28(%rsp)
movq %rdi, 0x190(%rsp)
movq %rsi, 0x188(%rsp)
movq %rdx, 0x180(%rsp)
movl %ecx, 0x17c(%rsp)
movq 0x188(%rsp), %rax
movq %rax, 0x30(%rsp)
leaq 0x160(%rsp), %rdi
callq 0x54e0
movb $0x0, 0x15f(%rsp)
leaq 0x15e(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0x3480
movq 0x38(%rsp), %rdi
movq 0x40(%rsp), %rdx
leaq 0xd725(%rip), %rsi      # 0x110dd
callq 0x3360
jmp 0x39bf
leaq 0x15e(%rsp), %rdi
callq 0x32e0
movq 0x30(%rsp), %rdi
addq $0x8, %rdi
callq 0x54f0
jmp 0x39dc
movq 0x30(%rsp), %rax
movb (%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x3a6c
movq 0x155cc(%rip), %rax     # 0x18fc0
movq (%rax), %rdi
leaq 0xd6c1(%rip), %rsi      # 0x110bf
xorl %eax, %eax
callq 0x3380
movq 0x30(%rsp), %rdi
addq $0x8, %rdi
callq 0x5520
jmp 0x3a15
movb $0x1, 0x15f(%rsp)
movl $0x1, 0x148(%rsp)
jmp 0x3e32
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
leaq 0x15e(%rsp), %rdi
callq 0x32e0
jmp 0x3e6a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
jmp 0x3e60
movq 0x180(%rsp), %rsi
leaq 0x128(%rsp), %rdi
callq 0x5530
jmp 0x3a83
leaq 0x128(%rsp), %rdi
callq 0x55b0
movq %rax, 0x120(%rsp)
leaq 0x128(%rsp), %rdi
callq 0x55e0
movq %rax, 0x118(%rsp)
leaq 0x120(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0x5640
testb $0x1, %al
jne 0x3acb
jmp 0x3d2c
leaq 0xf8(%rsp), %rdi
leaq 0x120(%rsp), %rsi
callq 0x5680
leaq 0xf0(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0x5680
movq 0x30(%rsp), %rsi
movl 0x17c(%rsp), %r8d
movq 0xf8(%rsp), %rdx
movq 0xf0(%rsp), %rcx
leaq 0x100(%rsp), %rdi
callq 0x36b0
jmp 0x3b21
leaq 0x100(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x56c0
movq 0x18(%rsp), %rdi
movq %rax, 0xe0(%rsp)
callq 0x56f0
movq %rax, %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0xd8(%rsp)
movq %rax, 0xd0(%rsp)
movq 0xe0(%rsp), %rdi
movq 0xd8(%rsp), %rsi
movq 0xd0(%rsp), %rdx
callq 0x3e90
movq %rax, 0x20(%rsp)
jmp 0x3b81
movq 0x20(%rsp), %rax
movq %rax, 0xe8(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x5720
movq %rax, %rdi
addq $0x18, %rdi
callq 0x5730
movq %rax, 0xc8(%rsp)
movq 0xc8(%rsp), %rdi
callq 0x5760
movq %rax, 0x10(%rsp)
jmp 0x3bc3
movq 0x10(%rsp), %rsi
leaq 0x120(%rsp), %rdi
callq 0x57a0
leaq 0xe8(%rsp), %rdi
callq 0x5720
movq %rax, %rdi
callq 0x57c0
movq %rax, 0xc0(%rsp)
movq 0xc0(%rsp), %rax
cmpl $0x0, (%rax)
je 0x3d08
movq 0xc8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x98(%rsp)
movq 0xc8(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x90(%rsp)
leaq 0x8f(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x5860
movq 0x8(%rsp), %rcx
movq 0x98(%rsp), %rsi
movq 0x90(%rsp), %rdx
leaq 0xa0(%rsp), %rdi
callq 0x5870
jmp 0x3c60
leaq 0x160(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x57f0
jmp 0x3c77
leaq 0xa0(%rsp), %rdi
callq 0x5900
leaq 0x8f(%rsp), %rdi
callq 0x5940
jmp 0x3d08
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
jmp 0x3e23
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
jmp 0x3d1a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
jmp 0x3cf9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0x5900
leaq 0x8f(%rsp), %rdi
callq 0x5940
jmp 0x3d1a
leaq 0x100(%rsp), %rdi
callq 0x5480
jmp 0x3aad
leaq 0x100(%rsp), %rdi
callq 0x5480
jmp 0x3e23
leaq 0x160(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0x80(%rsp), %rdi
callq 0x5950
movq %rax, 0x78(%rsp)
movq 0x80(%rsp), %rdi
callq 0x5980
movq %rax, 0x70(%rsp)
leaq 0x78(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x59b0
testb $0x1, %al
jne 0x3d75
jmp 0x3df1
leaq 0x78(%rsp), %rdi
callq 0x59f0
movq %rax, 0x68(%rsp)
movq 0x68(%rsp), %rsi
leaq 0x48(%rsp), %rdi
callq 0x5a00
jmp 0x3d95
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x3310
jmp 0x3da6
leaq 0x48(%rsp), %rdi
callq 0x3160
movq 0x38(%rsp), %rdi
leaq 0xd42b(%rip), %rsi      # 0x111e7
callq 0x3280
jmp 0x3dc3
jmp 0x3dc5
leaq 0x78(%rsp), %rdi
callq 0x5aa0
jmp 0x3d60
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
leaq 0x48(%rsp), %rdi
callq 0x3160
jmp 0x3e23
movq 0x30(%rsp), %rdi
addq $0x8, %rdi
callq 0x5520
jmp 0x3e01
movb $0x1, 0x15f(%rsp)
movl $0x1, 0x148(%rsp)
leaq 0x128(%rsp), %rdi
callq 0x5900
jmp 0x3e32
leaq 0x128(%rsp), %rdi
callq 0x5900
jmp 0x3e60
testb $0x1, 0x15f(%rsp)
jne 0x3e46
movq 0x38(%rsp), %rdi
callq 0x3160
leaq 0x160(%rsp), %rdi
callq 0x5ac0
movq 0x28(%rsp), %rax
addq $0x198, %rsp            # imm = 0x198
retq
movq 0x38(%rsp), %rdi
callq 0x3160
leaq 0x160(%rsp), %rdi
callq 0x5ac0
movq 0x150(%rsp), %rdi
callq 0x3470
nopw %cs:(%rax,%rax)