# アーキテクチャ構成図

## システム全体構成図

```mermaid
graph TB
    subgraph "User Interface"
        U[ユーザーコード]
    end

    subgraph "dplyr Public API"
        M[mutate]
        F[filter]
        S[select]
        SUM[summarise]
        A[arrange]
        GB[group_by]
        J[join系]
        SL[slice]
    end

    subgraph "Core Layer"
        DM[DataMask<br/>R6 Class]
        GDF[grouped_df]
        RDF[rowwise_df]
        CTX[Context Functions<br/>cur_group, n, etc.]
    end

    subgraph "Native Layer C++"
        CMT[dplyr_mask_eval_all_mutate]
        CFT[dplyr_mask_eval_all_filter]
        CSM[dplyr_mask_eval_all_summarise]
        CGP[dplyr_group_by]
    end

    subgraph "External Dependencies"
        RL[rlang<br/>Tidy Evaluation]
        VCT[vctrs<br/>Vector Operations]
        TBL[tibble<br/>Data Frame]
        TS[tidyselect<br/>Column Selection]
        R6L[R6<br/>OOP]
    end

    subgraph "Optional Backends"
        DBP[dbplyr<br/>Database]
        DTP[dtplyr<br/>data.table]
        ARR[arrow<br/>Apache Arrow]
    end

    U --> M
    U --> F
    U --> S
    U --> SUM
    U --> A
    U --> GB
    U --> J
    U --> SL

    M --> DM
    F --> DM
    SUM --> DM
    GB --> GDF

    DM --> CTX
    DM --> CMT
    DM --> CFT
    DM --> CSM

    GDF --> CGP

    DM --> RL
    DM --> VCT
    GDF --> TBL
    S --> TS
    DM --> R6L

    M -.-> DBP
    F -.-> DBP
    SUM -.-> DBP
    M -.-> DTP
    M -.-> ARR
```

## レイヤー構成図

```mermaid
graph TD
    subgraph "Layer 1: Public API"
        API[Verb Functions<br/>mutate, filter, select, summarise, arrange, join, group_by, slice, distinct]
    end

    subgraph "Layer 2: Core Implementation"
        VERB[Verb Implementations<br/>mutate.data.frame, filter.data.frame, etc.]
        MASK[DataMask<br/>Expression Evaluation Engine]
        GROUP[Grouped Operations<br/>grouped_df, rowwise_df]
    end

    subgraph "Layer 3: Native Performance"
        CPP[C++ Functions<br/>filter.cpp, mutate.cpp, summarise.cpp, group_by.cpp]
    end

    subgraph "Layer 4: Dependencies"
        DEPS[External Packages<br/>rlang, vctrs, tibble, tidyselect, R6, pillar, glue]
    end

    API --> VERB
    VERB --> MASK
    VERB --> GROUP
    MASK --> CPP
    GROUP --> CPP
    CPP --> DEPS
    MASK --> DEPS
    GROUP --> DEPS
```

## コンポーネント依存関係図

```mermaid
graph LR
    subgraph "Single Table Verbs"
        mutate[mutate.R]
        filter[filter.R]
        select[select.R]
        summarise[summarise.R]
        arrange[arrange.R]
        slice[slice.R]
        distinct[distinct.R]
    end

    subgraph "Two-Table Verbs"
        join[join.R]
        join_by[join-by.R]
        join_cols[join-cols.R]
        join_rows[join-rows.R]
        sets[sets.R]
    end

    subgraph "Grouping"
        group_by[group-by.R]
        grouped_df[grouped-df.R]
        group_data[group-data.R]
        rowwise[rowwise.R]
    end

    subgraph "Core"
        data_mask[data-mask.R]
        context[context.R]
        across[across.R]
        conditions[conditions.R]
    end

    subgraph "Helpers"
        case_when[case-when.R]
        if_else[if-else.R]
        coalesce[coalesce.R]
        recode[recode.R]
    end

    mutate --> data_mask
    filter --> data_mask
    summarise --> data_mask

    mutate --> grouped_df
    filter --> grouped_df
    summarise --> grouped_df

    mutate --> across
    summarise --> across

    data_mask --> context
    data_mask --> conditions

    group_by --> grouped_df
    grouped_df --> group_data
    rowwise --> grouped_df

    join --> join_by
    join --> join_cols
    join --> join_rows
```

## DataMask処理フロー図

```mermaid
sequenceDiagram
    participant User as ユーザーコード
    participant Verb as mutate()
    participant Mask as DataMask
    participant Ctx as Context
    participant Cpp as C++ Layer
    participant Vctrs as vctrs

    User->>Verb: mutate(df, new_col = expr)
    Verb->>Verb: compute_by(.by)
    Verb->>Mask: DataMask$new(data, by, "mutate")
    Mask->>Mask: initialize rows, chops
    Mask->>Cpp: dplyr_lazy_vec_chop()
    Mask->>Cpp: dplyr_make_mask_bindings()

    loop 各式に対して
        Verb->>Mask: mutate_cols()
        Mask->>Mask: expand_pick(), expand_across()
        Mask->>Cpp: dplyr_mask_eval_all_mutate(quo)
        Cpp->>Ctx: DPLYR_MASK_SET_GROUP()
        Cpp->>Cpp: rlang::eval_tidy(quo)
        Cpp->>Vctrs: vec_recycle()
        Cpp-->>Mask: chunks
        Mask->>Mask: add_one(name, chunks, result)
    end

    Verb->>Verb: dplyr_col_modify(data, cols)
    Verb->>Verb: mutate_relocate(), mutate_keep()
    Verb-->>User: 結果のdata.frame
```

## グループ処理フロー図

```mermaid
flowchart TD
    A[group_by呼び出し] --> B{既にグループ化?}
    B -->|Yes| C[既存グループ取得]
    B -->|No| D[新規グループ作成]

    C --> E{.add = TRUE?}
    E -->|Yes| F[グループ追加]
    E -->|No| G[グループ置換]

    D --> H[group_by_prepare]
    F --> H
    G --> H

    H --> I[add_computed_columns]
    I --> J[mutate_cols for expressions]
    J --> K[grouped_df作成]

    K --> L[compute_groups]
    L --> M[dplyr_locate_sorted_groups]
    M --> N[expand_groups if .drop=FALSE]
    N --> O[new_grouped_df]

    O --> P[グループ化されたdata.frame]
```

## 結合処理フロー図

```mermaid
flowchart TD
    A[join関数呼び出し] --> B[by引数の解析]
    B --> C{by = NULL?}
    C -->|Yes| D[join_by_common<br/>共通列検出]
    C -->|No| E[as_join_by<br/>join_by解析]

    D --> F[join_cols]
    E --> F

    F --> G[キー列の型キャスト<br/>join_cast_common]
    G --> H[join_rows]

    H --> I{join type}
    I -->|inner| J[内部結合]
    I -->|left| K[左外部結合]
    I -->|right| L[右外部結合]
    I -->|full| M[完全外部結合]
    I -->|semi| N[セミ結合]
    I -->|anti| O[アンチ結合]

    J --> P[結果行インデックス生成]
    K --> P
    L --> P
    M --> P
    N --> P
    O --> P

    P --> Q[vec_slice による行抽出]
    Q --> R[dplyr_reconstruct]
    R --> S[結果data.frame]
```

## パッケージ構造図

```mermaid
graph TB
    subgraph "R/"
        subgraph "Core Verbs"
            MR[mutate.R]
            FR[filter.R]
            SR[select.R]
            SMR[summarise.R]
            AR[arrange.R]
        end

        subgraph "Join Operations"
            JR[join.R]
            JBR[join-by.R]
            JCR[join-cols.R]
            JRR[join-rows.R]
        end

        subgraph "Grouping"
            GBR[group-by.R]
            GDFR[grouped-df.R]
            RWR[rowwise.R]
        end

        subgraph "Data Mask"
            DMR[data-mask.R]
            CTR[context.R]
            ACR[across.R]
        end

        subgraph "Helpers"
            CWR[case-when.R]
            IER[if-else.R]
            RCR[recode.R]
        end
    end

    subgraph "src/"
        subgraph "C++ Implementation"
            DH[dplyr.h]
            FC[filter.cpp]
            MC[mutate.cpp]
            SC[summarise.cpp]
            GC[group_by.cpp]
            MKC[mask.cpp]
            RCC[reconstruct.cpp]
        end
    end

    DMR --> DH
    MR --> MC
    FR --> FC
    SMR --> SC
    GBR --> GC
```

## 外部依存関係図

```mermaid
graph TD
    subgraph "dplyr"
        CORE[Core Functions]
    end

    subgraph "Required Dependencies"
        RL[rlang >= 1.1.7<br/>Tidy Evaluation]
        VCT[vctrs >= 0.7.1<br/>Vector Operations]
        TBL[tibble >= 3.2.0<br/>Data Frame]
        TS[tidyselect >= 1.2.0<br/>Column Selection]
        R6[R6<br/>OOP]
        CLI[cli >= 3.6.2<br/>User Interface]
        GLU[glue >= 1.3.2<br/>String Interpolation]
        PIL[pillar >= 1.9.0<br/>Formatting]
        LC[lifecycle >= 1.0.5<br/>Deprecation]
        MG[magrittr >= 1.5<br/>Pipe Operator]
        GEN[generics<br/>Generic Functions]
    end

    subgraph "Suggested Dependencies"
        DBP[dbplyr >= 2.2.1<br/>Database Backend]
        DBI[DBI<br/>Database Interface]
        TDR[tidyr >= 1.3.0<br/>Data Tidying]
        TTH[testthat >= 3.1.5<br/>Testing]
        KNT[knitr<br/>Documentation]
    end

    CORE --> RL
    CORE --> VCT
    CORE --> TBL
    CORE --> TS
    CORE --> R6
    CORE --> CLI
    CORE --> GLU
    CORE --> PIL
    CORE --> LC
    CORE --> MG
    CORE --> GEN

    CORE -.-> DBP
    CORE -.-> DBI
    CORE -.-> TDR
    CORE -.-> TTH
```
