{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "90f2a7a7",
   "metadata": {},
   "source": [
    "# vo001_AdoptOrOwnMemory\n",
    "In this tutorial we learn how the RVec class can be used to\n",
    "adopt existing memory or allocate some.\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "**Author:** Danilo Piparo  \n",
    "<i><small>This notebook tutorial was automatically generated with <a href= \"https://github.com/root-project/root/blob/master/documentation/doxygen/converttonotebook.py\">ROOTBOOK-izer</a> from the macro found in the ROOT repository  on Tuesday, May 19, 2026 at 08:27 PM.</small></i>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b5cf3e5c",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:27:58.362436Z",
     "iopub.status.busy": "2026-05-19T20:27:58.362321Z",
     "iopub.status.idle": "2026-05-19T20:27:59.302532Z",
     "shell.execute_reply": "2026-05-19T20:27:59.301900Z"
    }
   },
   "outputs": [],
   "source": [
    "import ROOT"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0648a6f6",
   "metadata": {},
   "source": [
    "We use this class for didactic purposes: upon copy, a line is printed to the terminal."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b1a0750b",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:27:59.312657Z",
     "iopub.status.busy": "2026-05-19T20:27:59.312500Z",
     "iopub.status.idle": "2026-05-19T20:27:59.448165Z",
     "shell.execute_reply": "2026-05-19T20:27:59.447507Z"
    }
   },
   "outputs": [],
   "source": [
    "ROOT.gInterpreter.Declare('''\n",
    "class UponCopyPrinter {\n",
    "public:\n",
    "   UponCopyPrinter() = default;\n",
    "   UponCopyPrinter(UponCopyPrinter &&) = default;\n",
    "   UponCopyPrinter(const UponCopyPrinter &) { std::cout << \"Invoking copy c'tor!\" << std::endl; }\n",
    "};\n",
    "''')\n",
    "\n",
    "RVec_UponCopyPrinter = ROOT.ROOT.VecOps.RVec(ROOT.UponCopyPrinter)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed375b84",
   "metadata": {},
   "source": [
    "One of the essential features of RVec is its ability of adopting and owning memory."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e09a0f57",
   "metadata": {},
   "source": [
    "Let's create an RVec of UponCopyPrinter instances. We expect no printout:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9e088bc3",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:27:59.450095Z",
     "iopub.status.busy": "2026-05-19T20:27:59.449970Z",
     "iopub.status.idle": "2026-05-19T20:27:59.587807Z",
     "shell.execute_reply": "2026-05-19T20:27:59.587209Z"
    }
   },
   "outputs": [],
   "source": [
    "v = RVec_UponCopyPrinter(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a920e578",
   "metadata": {},
   "source": [
    "Let's adopt the memory from v into v2. We expect no printout:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "fb883918",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:27:59.589854Z",
     "iopub.status.busy": "2026-05-19T20:27:59.589727Z",
     "iopub.status.idle": "2026-05-19T20:27:59.704112Z",
     "shell.execute_reply": "2026-05-19T20:27:59.703502Z"
    }
   },
   "outputs": [],
   "source": [
    "v2 = RVec_UponCopyPrinter(v.data(), v.size())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e7c3377",
   "metadata": {},
   "source": [
    "OK, let's check the addresses of the memory associated to the two RVecs It is the same!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f77891a3",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:27:59.706167Z",
     "iopub.status.busy": "2026-05-19T20:27:59.706042Z",
     "iopub.status.idle": "2026-05-19T20:27:59.815901Z",
     "shell.execute_reply": "2026-05-19T20:27:59.815271Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<cppyy.gbl.UponCopyPrinter object at 0x563759018080> and <cppyy.gbl.UponCopyPrinter object at 0x563759018080>\n"
     ]
    }
   ],
   "source": [
    "print(\"%s and %s\" %(v.data(), v2.data()))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31231f81",
   "metadata": {},
   "source": [
    "Now, upon reallocation, the RVec stops adopting the memory and starts owning it. And yes,\n",
    "a copy is triggered. Indeed internally the storage of the RVec is an std::vector. Moreover,\n",
    "the interface of the RVec is very, very similar to the one of std::vector: you have already\n",
    "noticed it when the `data()` method was invoked, right?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e6a2be42",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:27:59.817625Z",
     "iopub.status.busy": "2026-05-19T20:27:59.817491Z",
     "iopub.status.idle": "2026-05-19T20:27:59.929521Z",
     "shell.execute_reply": "2026-05-19T20:27:59.928958Z"
    }
   },
   "outputs": [],
   "source": [
    "v2.resize(4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26af04ac",
   "metadata": {},
   "source": [
    "Of course, now the addresses are different."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "5cd2e514",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:27:59.931528Z",
     "iopub.status.busy": "2026-05-19T20:27:59.931403Z",
     "iopub.status.idle": "2026-05-19T20:28:00.035950Z",
     "shell.execute_reply": "2026-05-19T20:28:00.034780Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<cppyy.gbl.UponCopyPrinter object at 0x563759018080> and <cppyy.gbl.UponCopyPrinter object at 0x56375f8f1f80>\n"
     ]
    }
   ],
   "source": [
    "print(\"%s and %s\" %(v.data(), v2.data()))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
