// File: jkom-cloud[P]mmseg/src/Mmseg.cpp
// Function: mmsegSpace::MMSeg::get_chunks(__gnu_cxx::__normal_iterator<char16_t const*, std::__cxx11::basic_string<char16_t, std::char_traits<char16_t>, std::allocator<char16_t>>>, __gnu_cxx::__normal_iterator<char16_t const*, std::__cxx11::basic_string<char16_t, std::char_traits<char16_t>, std::allocator<char16_t>>>, int)
std::vector<MMSeg::Chunk> MMSeg::get_chunks(StringIt _start, StringIt _end, int depth) {
    std::vector<Chunk> ret;

    // "递归"实现
    std::function<void(StringIt, StringIt, int, std::vector<StringP>, std::vector<int>)> get_chunks_it = [&] (StringIt start, StringIt end, int n, std::vector<StringP> segs, std::vector<int> inDict) {
      if (n == 0 || start == end) {
        ret.emplace_back(std::move(segs), std::move(inDict), char_freqs_);
      }
      else {
        auto m = dict_.match_all(start, end);
        //std::cout<<"Match_all Length : "<<m.size()<<std::endl;
        for (auto& w: m) {
          auto nsegs = segs;
          auto ninDict = inDict;
          auto len = length(w);
          //std::cout<<TransCode::to_utf8(String(start, start+len))<<std::endl;
          nsegs.emplace_back(std::move(w));
          ninDict.emplace_back(1);
          get_chunks_it(start + len, end, n - 1, std::move(nsegs), std::move(ninDict));
        }

        //std::cout<<TransCode::to_utf8(String(start, start+1))<<std::endl;
        segs.emplace_back(start, start +1 );
        inDict.emplace_back(0);
        get_chunks_it(start + 1, end, n - 1, std::move(segs), std::move(inDict));
      }
    };

    get_chunks_it(_start, _end, depth, std::vector<StringP>{}, std::vector<int>{});
    return ret;
  }

// Assembly:
subq $0xf8, %rsp
movq %rdi, 0x30(%rsp)
movq %rdi, %rax
movq %rax, 0x28(%rsp)
movq %rdi, 0xf0(%rsp)
movq %rdx, 0xe8(%rsp)
movq %rcx, 0xe0(%rsp)
movq %rsi, 0xd8(%rsp)
movl %r8d, 0xd4(%rsp)
movq 0xd8(%rsp), %rax
movq %rax, 0x38(%rsp)
movb $0x0, 0xd3(%rsp)
callq 0x5310
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rax
movq %rdi, 0x98(%rsp)
movq %rax, 0xa0(%rsp)
leaq 0xb0(%rsp), %rdi
movq %rdi, 0xa8(%rsp)
leaq 0x98(%rsp), %rsi
callq 0x3890
jmp 0x373f
movq 0xe8(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0xe0(%rsp), %rax
movq %rax, 0x78(%rsp)
movl 0xd4(%rsp), %eax
movl %eax, 0x14(%rsp)
vpxor %xmm0, %xmm0, %xmm0
vmovaps %xmm0, (%rsp)
vmovdqa %xmm0, 0x60(%rsp)
movq $0x0, 0x70(%rsp)
leaq 0x60(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x5390
vmovaps (%rsp), %xmm0
vmovdqa %xmm0, 0x40(%rsp)
movq $0x0, 0x50(%rsp)
leaq 0x40(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0x53a0
movl 0x14(%rsp), %ecx
movq 0x18(%rsp), %r8
movq 0x20(%rsp), %r9
movq 0x80(%rsp), %rsi
movq 0x78(%rsp), %rdx
leaq 0xb0(%rsp), %rdi
callq 0x5320
jmp 0x37db
leaq 0x40(%rsp), %rdi
callq 0x53b0
leaq 0x60(%rsp), %rdi
callq 0x5410
movb $0x1, 0xd3(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0x5470
testb $0x1, 0xd3(%rsp)
jne 0x3867
jmp 0x385d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x90(%rsp)
movl %eax, 0x8c(%rsp)
jmp 0x3874
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x90(%rsp)
movl %eax, 0x8c(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x53b0
leaq 0x60(%rsp), %rdi
callq 0x5410
leaq 0xb0(%rsp), %rdi
callq 0x5470
jmp 0x3874
movq 0x30(%rsp), %rdi
callq 0x5480
movq 0x28(%rsp), %rax
addq $0xf8, %rsp
retq
movq 0x30(%rsp), %rdi
callq 0x5480
movq 0x90(%rsp), %rdi
callq 0x3470
nopl (%rax,%rax)