├── .Rbuildignore ├── .clang-format ├── .codecov.yml ├── .gitattributes ├── .github └── workflows │ └── ci.yaml ├── .gitignore ├── .lintr ├── DESCRIPTION ├── LICENSE ├── NAMESPACE ├── NEWS.md ├── R ├── checks.R ├── datastructures-package.R ├── ds_deque.R ├── ds_deque_queue.R ├── ds_deque_stack.R ├── ds_heap.R ├── ds_heap_binomial.R ├── ds_heap_fibonacci.R ├── ds_map.R ├── ds_map_bimap.R ├── ds_map_hashmap.R ├── ds_map_multimap.R ├── ds_map_unordered.R ├── methods_at.R ├── methods_clear.R ├── methods_decrease.R ├── methods_erase.R ├── methods_handle.R ├── methods_insert.R ├── methods_keys.R ├── methods_peek.R ├── methods_pop.R ├── methods_size.R ├── methods_values.R └── zzz.R ├── README.md ├── cleanup ├── datastructures.Rproj ├── docs ├── 404.html ├── LICENSE-text.html ├── LICENSE.html ├── TODO.html ├── articles │ ├── datastructures.html │ ├── datastructures_files │ │ └── accessible-code-block-0.0.1 │ │ │ └── empty-anchor.js │ └── index.html ├── authors.html ├── bootstrap-toc.css ├── bootstrap-toc.js ├── docsearch.css ├── docsearch.js ├── extra.css ├── index.html ├── jquery.sticky-kit.min.js ├── link.svg ├── news │ └── index.html ├── pkgdown.css ├── pkgdown.js ├── pkgdown.yml └── reference │ ├── at-methods.html │ ├── bimap-class.html │ ├── bimap.html │ ├── binomial_heap-class.html │ ├── binomial_heap.html │ ├── clear-methods.html │ ├── datastructures-package.html │ ├── decrease_key-methods.html │ ├── deque-class.html │ ├── erase-methods.html │ ├── fibonacci_heap-class.html │ ├── fibonacci_heap.html │ ├── get-methods.html │ ├── handle-methods.html │ ├── hashmap-class.html │ ├── hashmap.html │ ├── head-methods.html │ ├── heap-class.html │ ├── index.html │ ├── insert-methods.html │ ├── keys-methods.html │ ├── map-class.html │ ├── multimap-class.html │ ├── multimap.html │ ├── peek-methods.html │ ├── pop-methods.html │ ├── queue-class.html │ ├── queue.html │ ├── remove-methods.html │ ├── size-methods.html │ ├── stack-class.html │ ├── stack.html │ ├── sub-unordered_map-vector-missing-missing-method.html │ ├── subset-bimap-vector-missing-vector-method.html │ ├── subset-heap-vector-missing-list-method.html │ ├── subset-heap-vector-missing-matrix-method.html │ ├── subset-heap-vector-missing-vector-method.html │ ├── subset-unordered_map-vector-missing-ANY-method.html │ ├── subset-unordered_map-vector-missing-list-method.html │ ├── subset-unordered_map-vector-missing-matrix-method.html │ ├── subset-unordered_map-vector-missing-vector-method.html │ ├── unordered_map-class.html │ └── values-methods.html ├── inst ├── heap │ ├── heap.aux │ ├── heap.bbl │ ├── heap.blg │ ├── heap.dvi │ ├── heap.jpg │ ├── heap.log │ ├── heap.pdf │ ├── heap.png │ ├── heap.synctex.gz │ └── heap.tex └── include │ ├── bimap.hpp │ ├── binomial_heap.hpp │ ├── fibonacci_heap.hpp │ ├── hashmap.hpp │ ├── heap.hpp │ ├── map.hpp │ ├── multimap.hpp │ ├── queue.hpp │ └── stack.hpp ├── joss ├── paper.bib └── paper.md ├── man ├── at-methods.Rd ├── bimap-class.Rd ├── bimap.Rd ├── binomial_heap-class.Rd ├── binomial_heap.Rd ├── clear-methods.Rd ├── datastructures-package.Rd ├── decrease_key-methods.Rd ├── deque-class.Rd ├── erase-methods.Rd ├── fibonacci_heap-class.Rd ├── fibonacci_heap.Rd ├── handle-methods.Rd ├── hashmap-class.Rd ├── hashmap.Rd ├── heap-class.Rd ├── insert-methods.Rd ├── keys-methods.Rd ├── map-class.Rd ├── multimap-class.Rd ├── multimap.Rd ├── peek-methods.Rd ├── pop-methods.Rd ├── queue-class.Rd ├── queue.Rd ├── size-methods.Rd ├── stack-class.Rd ├── stack.Rd ├── sub-unordered_map-vector-missing-missing-method.Rd ├── subset-bimap-vector-missing-vector-method.Rd ├── subset-heap-vector-missing-list-method.Rd ├── subset-heap-vector-missing-matrix-method.Rd ├── subset-heap-vector-missing-vector-method.Rd ├── subset-unordered_map-vector-missing-ANY-method.Rd ├── subset-unordered_map-vector-missing-list-method.Rd ├── subset-unordered_map-vector-missing-vector-method.Rd ├── unordered_map-class.Rd └── values-methods.Rd ├── pkgdown ├── _pkgdown.yml └── extra.css ├── src ├── Makevars ├── Makevars.win ├── bimap_module.cpp ├── binomial_heap_module.cpp ├── fibonacci_heap_module.cpp ├── hashmap_module.cpp ├── init.c ├── multimap_module.cpp ├── queue_module.cpp └── stack_module.cpp ├── tests ├── testthat.R └── testthat │ ├── tests_deque.R │ ├── tests_deque_queue.R │ ├── tests_deque_stack.R │ ├── tests_heap.R │ ├── tests_map.R │ ├── tests_map_bimap.R │ ├── tests_map_hashmap.R │ ├── tests_map_multimap.R │ └── tests_map_unordered_map.R └── vignettes └── datastructures.Rmd /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^.*\.Rproj$ 2 | ^\.Rproj\.user$ 3 | .gitignore 4 | .git 5 | .gitattributes 6 | .idea 7 | ^datastructures.Rcheck$ 8 | ^datastructures.Rproj$ 9 | ^datastructures*tar.gz$ 10 | .build*timestamp 11 | ^VERSIONS.md$ 12 | ^LICENSE$ 13 | ^.lintr$ 14 | ^.travis.yml$ 15 | ^.codecov.yml$ 16 | ^appveyor\.yml$ 17 | ^README\.md$ 18 | benchmark 19 | TODO.md 20 | ^NEWS.md$ 21 | .codecov.ym 22 | .yo-rc.json 23 | .clang-format 24 | CMakeLists.txt 25 | cmake-build-debug 26 | cmake-build-debug/* 27 | ^docs$ 28 | ^pkgdown$ 29 | ^joss 30 | -------------------------------------------------------------------------------- /.clang-format: -------------------------------------------------------------------------------- 1 | --- 2 | Language: Cpp 3 | 4 | AccessModifierOffset: -1 5 | AlignAfterOpenBracket: AlwaysBreak 6 | AlignConsecutiveAssignments: true 7 | AlignConsecutiveDeclarations: false 8 | AlignEscapedNewlinesLeft: false 9 | AlignOperands: false 10 | AlignTrailingComments: true 11 | AllowAllParametersOfDeclarationOnNextLine: true 12 | AllowShortBlocksOnASingleLine: false 13 | AllowShortCaseLabelsOnASingleLine: false 14 | AllowShortFunctionsOnASingleLine: None 15 | AllowShortIfStatementsOnASingleLine: false 16 | AllowShortLoopsOnASingleLine: false 17 | AlwaysBreakAfterDefinitionReturnType: None 18 | AlwaysBreakAfterReturnType: None 19 | AlwaysBreakBeforeMultilineStrings: false 20 | AlwaysBreakTemplateDeclarations: true 21 | BinPackArguments: true 22 | BinPackParameters: true 23 | BraceWrapping: 24 | AfterClass: true 25 | AfterControlStatement: true 26 | AfterEnum: true 27 | AfterFunction: true 28 | AfterNamespace: true 29 | AfterObjCDeclaration: true 30 | AfterStruct: true 31 | AfterUnion: true 32 | BeforeCatch: true 33 | BeforeElse: true 34 | IndentBraces: false 35 | BreakBeforeBinaryOperators: None 36 | BreakBeforeBraces: Custom 37 | BreakBeforeTernaryOperators: true 38 | BreakConstructorInitializersBeforeComma: false 39 | BreakAfterJavaFieldAnnotations: false 40 | BreakStringLiterals: true 41 | ColumnLimit: 80 42 | CommentPragmas: '' 43 | ConstructorInitializerAllOnOneLineOrOnePerLine: false 44 | ConstructorInitializerIndentWidth: 4 45 | ContinuationIndentWidth: 2 46 | Cpp11BracedListStyle: true 47 | DerivePointerAlignment: true 48 | DisableFormat: false 49 | ExperimentalAutoDetectBinPacking: false 50 | ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ] 51 | IncludeCategories: 52 | - Regex: '^<.*\.h>' 53 | Priority: 1 54 | - Regex: '^<.*' 55 | Priority: 2 56 | - Regex: '.*' 57 | Priority: 3 58 | IncludeIsMainRegex: '([-_](test|unittest))?$' 59 | IndentCaseLabels: true 60 | IndentWidth: 4 61 | IndentWrappedFunctionNames: true 62 | JavaScriptQuotes: Leave 63 | JavaScriptWrapImports: true 64 | 65 | KeepEmptyLinesAtTheStartOfBlocks: false 66 | MacroBlockBegin: '' 67 | MacroBlockEnd: '' 68 | MaxEmptyLinesToKeep: 1 69 | NamespaceIndentation: All 70 | ObjCBlockIndentWidth: 2 71 | ObjCSpaceAfterProperty: false 72 | ObjCSpaceBeforeProtocolList: false 73 | PenaltyBreakBeforeFirstCallParameter: 1 74 | PenaltyBreakComment: 300 75 | PenaltyBreakFirstLessLess: 120 76 | PenaltyBreakString: 1000 77 | PenaltyExcessCharacter: 1000000 78 | PenaltyReturnTypeOnItsOwnLine: 200 79 | PointerAlignment: Left 80 | ReflowComments: true 81 | SortIncludes: false 82 | SpaceAfterCStyleCast: false 83 | SpaceAfterTemplateKeyword: false 84 | SpaceBeforeAssignmentOperators: true 85 | SpaceBeforeParens: ControlStatements 86 | SpaceInEmptyParentheses: false 87 | SpacesBeforeTrailingComments: 2 88 | SpacesInAngles: false 89 | SpacesInContainerLiterals: true 90 | SpacesInCStyleCastParentheses: false 91 | SpacesInParentheses: false 92 | SpacesInSquareBrackets: false 93 | Standard: Auto 94 | TabWidth: 4 95 | UseTab: Never 96 | ... 97 | -------------------------------------------------------------------------------- /.codecov.yml: -------------------------------------------------------------------------------- 1 | comment: 2 | behavior: default 3 | layout: header, diff 4 | require_changes: false 5 | 6 | coverage: 7 | precision: 2 8 | range: 9 | - 70.0 10 | - 100.0 11 | round: down 12 | status: 13 | changes: false 14 | patch: true 15 | project: true 16 | 17 | parsers: 18 | gcov: 19 | branch_detection: 20 | conditional: true 21 | loop: true 22 | macro: false 23 | method: false 24 | javascript: 25 | enable_partials: false 26 | 27 | ignore: 28 | - "inst/include/" 29 | - "inst/" 30 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | data/* binary 3 | src/* text=lf 4 | R/* text=lf -------------------------------------------------------------------------------- /.github/workflows/ci.yaml: -------------------------------------------------------------------------------- 1 | name: ci 2 | 3 | on: 4 | push: 5 | pull_request: 6 | 7 | jobs: 8 | R-CMD-check: 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | os: ['windows-latest', 'macOS-latest', 'ubuntu-18.04'] 13 | include: 14 | - os: ubuntu-18.04 15 | cran: https://demo.rstudiopm.com/all/__linux__/bionic/latest 16 | runs-on: ${{ matrix.os }} 17 | name: ${{ matrix.os }} 18 | env: 19 | R_REMOTES_NO_ERRORS_FROM_WARNINGS: true 20 | TF_VERSION: ${{ matrix.tf }} 21 | TFP_VERSION: ${{ matrix.tfp }} 22 | PIP_NO_WARN_SCRIPT_LOCATION: false 23 | RETICULATE_AUTOCONFIGURE: 'FALSE' 24 | CRAN: ${{ matrix.cran }} 25 | steps: 26 | - uses: actions/checkout@v1 27 | - uses: r-lib/actions/setup-r@master 28 | - uses: r-lib/actions/setup-pandoc@master 29 | - name: install system dependencies 30 | if: runner.os == 'Linux' 31 | env: 32 | RHUB_PLATFORM: linux-x86_64-ubuntu-gcc 33 | run: | 34 | Rscript -e "install.packages('remotes')" -e "remotes::install_github('r-hub/sysreqs')" 35 | sysreqs=$(Rscript -e "cat(sysreqs::sysreq_commands('DESCRIPTION'))") 36 | sudo -s eval "$sysreqs" 37 | sudo apt-get install -y qpdf 38 | - name: install dependencies 39 | run: Rscript -e "install.packages('remotes')" -e "remotes::install_deps(dependencies = TRUE)" -e "remotes::install_cran('rcmdcheck')" 40 | - name: install 41 | run: | 42 | Rscript -e "remotes::install_local()" 43 | - name: check 44 | continue-on-error: ${{ contains(matrix.allow_failure, 'true') }} 45 | run: Rscript -e "rcmdcheck::rcmdcheck(args = '--no-manual', error_on = 'warning', check_dir = 'check')" 46 | - name: coverage 47 | continue-on-error: true 48 | run: | 49 | tar -C .. -xf $PKG_TARBALL- Rscript -e 'covr::codecov()' 50 | Rscript -e 'lintr::lint_package()' 51 | Rscript -e "remotes::install_github('dirmeier/datastructures')" 52 | if [[ "${TRAVIS_OS_NAME}" == "linux" ]]; then 53 | R -d "valgrind --tool=memcheck --leak-check=full --track-origins=yes" --vanilla < datastructures.Rcheck/datastructures-Ex.R 54 | fi 55 | 56 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | CMakeLists.txt 2 | check 3 | check.* 4 | cmake-build-debug/* 5 | cmake-build-debug 6 | inst/doc/* 7 | build/ 8 | src/test 9 | src/stamp-h1 10 | src/**/*.o 11 | src/**/*.a 12 | src/**/*.so 13 | src/a.out 14 | **/Makefile 15 | **/.deps 16 | **/.dirstamp 17 | *tar.gz 18 | dist 19 | .idea/ 20 | .idea/dataSources.ids 21 | .idea/dataSources.xml 22 | .idea/dictionaries 23 | .idea/dynamic.xml 24 | .idea/gradle.xml 25 | .idea/libraries 26 | .idea_modules/ 27 | .idea/mongoSettings.xml 28 | .idea/sqlDataSources.xml 29 | .idea/tasks.xml 30 | .idea/uiDesigner.xml 31 | .idea/workspace.xml 32 | *.iml 33 | *.ipr 34 | *.iws 35 | out/ 36 | .Rproj.user 37 | -------------------------------------------------------------------------------- /.lintr: -------------------------------------------------------------------------------- 1 | linters: list(linelen=line_length_linter(80), 2 | abspathlintr=absolute_paths_linter, 3 | assignlintr=assignment_linter, 4 | notab=no_tab_linter, 5 | camellinter=camel_case_linter, 6 | snakelinter=snake_case_linter, 7 | closedcurly=closed_curly_linter, 8 | trailingwhites=trailing_whitespace_linter) 9 | exclusions: list("tests/testthat.R", 10 | "tests/testthat/tests_deque.R", 11 | "tests/testthat/tests_heap.R", 12 | "tests/testthat/tests_map.R", 13 | "tests/testthat/tests_map_bimap.R", 14 | "tests/testthat/tests_map_multimap.R", 15 | "tests/testthat/tests_map_unordered_map.R") 16 | 17 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: datastructures 2 | Type: Package 3 | Title: Implementation of Core Data Structures 4 | Version: 0.2.9 5 | Authors@R: person("Simon", "Dirmeier", 6 | email = "simon.dirmeier@web.de", 7 | role = c("aut", "cre")) 8 | Maintainer: Simon Dirmeier 9 | Description: Implementation of advanced data structures such as hashmaps, 10 | heaps, or queues. Advanced data structures are essential 11 | in many computer science and statistics problems, for example graph 12 | algorithms or string analysis. The package uses 'Boost' and 'STL' data 13 | types and extends these to R with 'Rcpp' modules. 14 | URL: https://github.com/dirmeier/datastructures 15 | BugReports: https://github.com/dirmeier/datastructures/issues 16 | License: GPL-3 17 | Encoding: UTF-8 18 | Depends: 19 | R (>= 3.3), 20 | Rcpp 21 | Suggests: 22 | testthat, 23 | knitr, 24 | styler, 25 | rmarkdown, 26 | lintr 27 | VignetteBuilder: knitr 28 | RoxygenNote: 6.0.1 29 | SystemRequirements: C++11 30 | Imports: 31 | methods, 32 | purrr 33 | LinkingTo: 34 | Rcpp, 35 | BH 36 | NeedsCompilation: yes 37 | Collate: 38 | 'checks.R' 39 | 'datastructures-package.R' 40 | 'methods_clear.R' 41 | 'methods_insert.R' 42 | 'methods_size.R' 43 | 'methods_pop.R' 44 | 'methods_peek.R' 45 | 'ds_deque.R' 46 | 'ds_deque_queue.R' 47 | 'ds_deque_stack.R' 48 | 'methods_values.R' 49 | 'methods_decrease.R' 50 | 'methods_handle.R' 51 | 'ds_heap.R' 52 | 'ds_heap_binomial.R' 53 | 'ds_heap_fibonacci.R' 54 | 'methods_erase.R' 55 | 'ds_map.R' 56 | 'methods_keys.R' 57 | 'methods_at.R' 58 | 'ds_map_bimap.R' 59 | 'ds_map_unordered.R' 60 | 'ds_map_hashmap.R' 61 | 'ds_map_multimap.R' 62 | 'zzz.R' 63 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export(at) 4 | export(bimap) 5 | export(binomial_heap) 6 | export(clear) 7 | export(decrease_key) 8 | export(erase) 9 | export(fibonacci_heap) 10 | export(handle) 11 | export(hashmap) 12 | export(keys) 13 | export(multimap) 14 | export(peek) 15 | export(pop) 16 | export(queue) 17 | export(size) 18 | export(stack) 19 | export(values) 20 | exportClasses(bimap) 21 | exportClasses(binomial_heap) 22 | exportClasses(fibonacci_heap) 23 | exportClasses(hashmap) 24 | exportClasses(multimap) 25 | exportClasses(queue) 26 | exportClasses(stack) 27 | exportMethods(insert) 28 | import(Rcpp) 29 | importFrom(methods,is) 30 | importFrom(methods,new) 31 | importFrom(purrr,map) 32 | useDynLib(datastructures, .registration = TRUE) 33 | -------------------------------------------------------------------------------- /NEWS.md: -------------------------------------------------------------------------------- 1 | ## Version 0.2.6 2 | - Allows to use arbitray data objects for classes 3 | - Adds lifecycle badge 4 | 5 | ## Version 0.2.5 6 | - Adds `multimap` 7 | 8 | ## Version 0.2.4 9 | - Removes `binomial_heap` and `fibonacci_heap` implementations and replaces with `heap` template class. 10 | - Adds `value` method for heaps. 11 | - Replaces hashs for storing `value` in heaps with linear search (to make it conformab;e with introduction of data frame and other data structures). 12 | 13 | ## Version 0.2.3 14 | - Adds `handles_value` functions for heaps. 15 | 16 | ## Version 0.2.2 17 | - Adds `decrease_key` and `handle` methods for heap. 18 | - Adds tests for these two methods. 19 | - Updates vignette. 20 | 21 | ## Version 0.2.1 22 | - Adds vectorial values for `hashmap`, `fibonacci_heap`, `binomial_heap`, `stack` and `queue` 23 | - Adds more unit tests and examples 24 | 25 | ## Version 0.2 26 | - Adds `binomial_heap` and `bimap` 27 | - Overhaul to testing suite 28 | - Introduced abstract super classes: `heap`, `map` and `deque` 29 | - Constructor methods for classes to replace call to `new` 30 | - Removed `initialize` methods that broke copy construction contract 31 | - Several fixes 32 | 33 | ## Version 0.1.1 34 | - Fixes typos in vignette 35 | 36 | ## Version 0.1 37 | - Basic setup with required files 38 | - Implementation of `hashmap`, `fibonacci_heap`, `stack` and `queue` 39 | - Registration 40 | - Vignette 41 | 42 | 43 | -------------------------------------------------------------------------------- /R/checks.R: -------------------------------------------------------------------------------- 1 | # datastructures: Implementation of core datastructures for R. 2 | # 3 | # Copyright (C) Simon Dirmeier 4 | # 5 | # This file is part of datastructures. 6 | # 7 | # datastructures is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # datastructures is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with datastructures. If not, see . 19 | 20 | 21 | #' @noRd 22 | .check.key.value.classes <- function(obj, x, y) { 23 | .check.key.class(obj, x) 24 | .check.value.class(obj, y) 25 | } 26 | 27 | 28 | #' @noRd 29 | .check.value.class <- function(obj, y, vc = obj@.value.class) { 30 | if (any(is.null(y))) stop("y cannot be NULL") 31 | if (is.list(y)) { 32 | if (any(lapply(y, class) != vc)) stop(paste("class(y) is not", vc)) 33 | } 34 | else { 35 | if (class(y) != vc) stop(paste("class(y) is not", vc)) 36 | } 37 | } 38 | 39 | 40 | #' @noRd 41 | .check.key.class <- function(obj, x, kc = obj@.key.class) { 42 | if (any(is.null(x))) stop("x/y cannot be NULL") 43 | if (any(is.na(x))) stop("x cannot be NA") 44 | if (is.list(x)) { 45 | if (any(lapply(x, class) != kc)) stop(paste("class(x) is not", kc)) 46 | } 47 | else { 48 | if (class(x) != kc) stop(paste("class(x) is not", kc)) 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /R/datastructures-package.R: -------------------------------------------------------------------------------- 1 | # datastructures: Implementation of core datastructures for R. 2 | # 3 | # Copyright (C) Simon Dirmeier 4 | # 5 | # This file is part of datastructures. 6 | # 7 | # datastructures is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # datastructures is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with datastructures. If not, see . 19 | 20 | 21 | #' datastructures 22 | #' 23 | #' Implementation of advanced data structures such as hashmaps, 24 | #' heaps, or queues. Advanced data structures are essential 25 | #' in many computer science and statistics problems, for example graph 26 | #' algorithms or string analysis. The package uses 'Boost' and 'STL' data 27 | #' types and extends these to R with 'Rcpp' modules. 28 | #' 29 | #' @author Simon Dirmeier 30 | #' @name datastructures-package 31 | #' 32 | #' @useDynLib datastructures, .registration = TRUE 33 | #' @docType package 34 | #' @keywords package 35 | NULL 36 | -------------------------------------------------------------------------------- /R/ds_deque.R: -------------------------------------------------------------------------------- 1 | # datastructures: Implementation of core datastructures for R. 2 | # 3 | # Copyright (C) Simon Dirmeier 4 | # 5 | # This file is part of datastructures. 6 | # 7 | # datastructures is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # datastructures is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with datastructures. If not, see . 19 | 20 | 21 | #' @include methods_peek.R 22 | #' @include methods_pop.R 23 | #' @include methods_size.R 24 | #' @include methods_insert.R 25 | #' @include methods_clear.R 26 | NULL 27 | 28 | 29 | #' @title Deque class 30 | #' 31 | #' @name deque-class 32 | #' @rdname deque-class 33 | #' 34 | #' @description Abstract deque class 35 | #' 36 | #' @slot .deque \code{C++} object representing a deque 37 | #' 38 | setClass( 39 | "deque", 40 | contains = "VIRTUAL", 41 | slots = list(.deque = "ANY"), 42 | prototype = prototype(.deque = NULL) 43 | ) 44 | 45 | 46 | .clear.deque <- function(obj) { 47 | obj@.deque$clear() 48 | obj 49 | } 50 | 51 | #' @noRd 52 | .peek.deque <- function(obj) { 53 | if (obj@.deque$size()) { 54 | obj@.deque$peek() 55 | } else { 56 | NULL 57 | } 58 | } 59 | 60 | 61 | #' @noRd 62 | .pop.deque <- function(obj) { 63 | if (obj@.deque$size()) { 64 | obj@.deque$pop() 65 | } else { 66 | NULL 67 | } 68 | } 69 | 70 | 71 | #' @noRd 72 | .show.deque <- function(object) { 73 | cat(paste0("An object of class ", class(object)[1], "\n\n")) 74 | li <- peek(object) 75 | cat(paste0("Peek: ", class(li), ", ...\n")) 76 | } 77 | 78 | 79 | #' @noRd 80 | .size.deque <- function(obj) { 81 | obj@.deque$size() 82 | } 83 | 84 | 85 | #' @noRd 86 | .insert.deque <- function(obj, x) { 87 | obj@.deque$insert(x) 88 | obj 89 | } 90 | 91 | 92 | #' @rdname peek-methods 93 | setMethod("peek", "deque", .peek.deque) 94 | 95 | 96 | #' @rdname pop-methods 97 | setMethod("pop", "deque", .pop.deque) 98 | 99 | 100 | setMethod("show", "deque", .show.deque) 101 | 102 | 103 | #' @rdname size-methods 104 | setMethod("size", "deque", .size.deque) 105 | 106 | 107 | #' @rdname insert-methods 108 | setMethod( 109 | "insert", 110 | signature = signature(obj = "deque", x = "ANY", y = "missing"), 111 | function(obj, x) .insert.deque(obj, list(x)) 112 | ) 113 | 114 | 115 | #' @rdname insert-methods 116 | setMethod( 117 | "insert", 118 | signature = signature(obj = "deque", x = "list", y = "missing"), 119 | function(obj, x) { 120 | x <- if (is.data.frame(x) || length(x) == 1) list(x) else x 121 | .insert.deque(obj, x) 122 | } 123 | ) 124 | 125 | 126 | #' @rdname clear-methods 127 | setMethod("clear", "deque", .clear.deque) 128 | -------------------------------------------------------------------------------- /R/ds_deque_queue.R: -------------------------------------------------------------------------------- 1 | # datastructures: Implementation of core datastructures for R. 2 | # 3 | # Copyright (C) Simon Dirmeier 4 | # 5 | # This file is part of datastructures. 6 | # 7 | # datastructures is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # datastructures is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with datastructures. If not, see . 19 | 20 | 21 | #' @title Queue class 22 | #' 23 | #' @export 24 | #' @name queue-class 25 | #' @rdname queue-class 26 | #' 27 | #' @description Implementation of a queue data structure, i.e. a list 28 | #' implementation with FIFO principle. \code{queue} uses a \code{std::deque} 29 | #' as default container, so inserting, peeking and popping functions require 30 | #' constant \emph{O(1)}. See \code{\linkS4class{stack}} for a class using 31 | #' the LIFO principle. 32 | #' 33 | #' @slot .deque \code{C++} object representing a deque 34 | #' 35 | #' @seealso \code{\link{queue}} for creating a new \code{queue} object. 36 | #' 37 | setClass("queue", contains = "deque") 38 | 39 | 40 | #' @title Create a new \code{queue} 41 | #' 42 | #' @export 43 | #' @importFrom methods new 44 | #' 45 | #' @description Instantiates a new \code{\linkS4class{queue}} object, 46 | #' i.e. a list implementation with FIFO principle. 47 | #' 48 | #' @return returns a new \code{queue} object 49 | #' 50 | #' @examples 51 | #' # returns a new queue 52 | #' q <- queue() 53 | queue <- function() { 54 | queue <- methods::new(queue_sexp) 55 | methods::new("queue", .deque = queue) 56 | } 57 | -------------------------------------------------------------------------------- /R/ds_deque_stack.R: -------------------------------------------------------------------------------- 1 | # datastructures: Implementation of core datastructures for R. 2 | # 3 | # Copyright (C) Simon Dirmeier 4 | # 5 | # This file is part of datastructures. 6 | # 7 | # datastructures is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # datastructures is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with datastructures. If not, see . 19 | 20 | 21 | #' @title Stack class 22 | #' 23 | #' @export 24 | #' @name stack-class 25 | #' @rdname stack-class 26 | #' 27 | #' @description Implementation of a stack data structure, i.e. a list 28 | #' implementation with LIFO principle. \code{stack} uses a \code{std::deque} 29 | #' as default container, so inserting, peeking and popping functions require 30 | #' constant \emph{O(1)}. See \code{\linkS4class{queue}} for a class using 31 | #' the FIFO principle. 32 | #' 33 | #' @slot .deque \code{C++} object representing a deque 34 | #' 35 | #' @seealso \code{\link{stack}} for creating a new \code{stack} object. 36 | #' 37 | setClass("stack", contains = "deque") 38 | 39 | 40 | #' @title Create a new \code{stack} 41 | #' 42 | #' @export 43 | #' @importFrom methods new 44 | #' 45 | #' @description Instantiates a new \code{\linkS4class{stack}} object, 46 | #' i.e. a list implementation with LIFO principle. 47 | #' 48 | #' @param ... parameters that are only needed if \code{utils::stack} should be 49 | #' called 50 | #' 51 | #' @return returns a new \code{stack} object 52 | #' 53 | #' @examples 54 | #' # creates a new stack 55 | #' s <- stack() 56 | stack <- function(...) { 57 | l <- list(...) 58 | if (length(l)) { 59 | return(utils::stack(...)) 60 | } 61 | 62 | stack <- methods::new(stack_sexp) 63 | methods::new("stack", .deque = stack) 64 | } 65 | -------------------------------------------------------------------------------- /R/ds_heap_binomial.R: -------------------------------------------------------------------------------- 1 | # datastructures: Implementation of core datastructures for R. 2 | # 3 | # Copyright (C) Simon Dirmeier 4 | # 5 | # This file is part of datastructures. 6 | # 7 | # datastructures is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # datastructures is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with datastructures. If not, see . 19 | 20 | 21 | #' @title Binomial heap class 22 | #' 23 | #' @exportClass binomial_heap 24 | #' @name binomial_heap-class 25 | #' @rdname binomial_heap-class 26 | #' 27 | #' @description Implementation of a binomial heap data structure, i.e. a 28 | #' priority datastructure with \code{push} and \code{pop} in amortized 29 | #' \emph{O(log n)}. \code{binomial_heap} wraps a \code{boost::binomial_heap} 30 | #' using Rcpp modules. The heap consists of nodes with 31 | #' keys and values where the key determines the priority in the heap. 32 | #' Also see the \code{\linkS4class{binomial_heap}} class. 33 | #' 34 | #' @slot .heap \code{C++} object representing a heap 35 | #' @slot .key.class the class of the keys 36 | #' 37 | #' @seealso \code{\link{binomial_heap}} for creating a new \code{binomial_heap} object 38 | #' 39 | setClass("binomial_heap", contains = "heap") 40 | 41 | 42 | #' @title Create a new \code{binomial_heap} 43 | #' 44 | #' @export 45 | #' @importFrom methods new 46 | #' 47 | #' @description Instantiates a new \code{\linkS4class{binomial_heap}} object, 48 | #' i.e. a tree-like data structure satisfying the \emph{min-heap} property. 49 | #' 50 | #' @param key.class the primitive class type of the keys 51 | #' 52 | #' @return returns a new \code{binomial_heap} object 53 | #' 54 | #' @examples 55 | #' # creates a binomial_heap 56 | #' b_heap <- binomial_heap() 57 | #' 58 | #' # creates a binomial_heap 59 | #' b_heap <- binomial_heap("numeric") 60 | #' 61 | #' # creates a binomial_heap 62 | #' b_heap <- binomial_heap("character") 63 | binomial_heap <- function( 64 | key.class = c("character", "numeric", "integer")) { 65 | key.class <- match.arg(key.class) 66 | heap <- switch(key.class, 67 | "character" = methods::new(binomial_heap_s), 68 | "numeric" = methods::new(binomial_heap_d), 69 | "integer" = methods::new(binomial_heap_i), 70 | stop("Error defining key class") 71 | ) 72 | 73 | methods::new("binomial_heap", 74 | .key.class = key.class, 75 | .heap = heap 76 | ) 77 | } 78 | -------------------------------------------------------------------------------- /R/ds_heap_fibonacci.R: -------------------------------------------------------------------------------- 1 | # datastructures: Implementation of core datastructures for R. 2 | # 3 | # Copyright (C) Simon Dirmeier 4 | # 5 | # This file is part of datastructures. 6 | # 7 | # datastructures is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # datastructures is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with datastructures. If not, see . 19 | 20 | 21 | #' @title Fibonacci heap class 22 | #' 23 | #' @exportClass fibonacci_heap 24 | #' @name fibonacci_heap-class 25 | #' @rdname fibonacci_heap-class 26 | #' 27 | #' @description Implementation of a Fibonacci heap data structure, i.e. a 28 | #' priority datastructure with \code{push} in amortized \emph{O(1)} and 29 | #' \code{pop} in \emph{O(log n)}. \code{fibonacci_heap} wraps a 30 | #' \code{boost::fibonacci_heap} using Rcpp modules. The heap consists of 31 | #' nodes with keys and values where the key determines the priority in the 32 | #' heap. Also see the \code{\linkS4class{binomial_heap}} class. 33 | #' 34 | #' @slot .heap \code{C++} object representing a heap 35 | #' @slot .key.class the class of the keys 36 | #' 37 | #' @seealso \code{\link{fibonacci_heap}} for creating a new \code{fibonacci_heap} object 38 | #' 39 | setClass("fibonacci_heap", contains = "heap") 40 | 41 | 42 | #' @title Create a new \code{fibonacci_heap} 43 | #' 44 | #' @export 45 | #' @importFrom methods new 46 | #' 47 | #' @description Instantiates a new \code{\linkS4class{fibonacci_heap}} object, 48 | #' i.e. a tree-like data structure satisfying the \emph{min-heap} property. 49 | #' 50 | #' @param key.class the primitive class type of the keys 51 | #' 52 | #' @return returns a new \code{fibonacci_heap} object 53 | #' 54 | #' @examples 55 | #' # creates a fibonacci_heap 56 | #' f_heap <- fibonacci_heap() 57 | #' 58 | #' # creates a fibonacci_heap 59 | #' f_heap <- fibonacci_heap("numeric") 60 | #' 61 | #' # creates a fibonacci_heap 62 | #' f_heap <- fibonacci_heap("character") 63 | fibonacci_heap <- function( 64 | key.class = c("character", "numeric", "integer")) { 65 | key.class <- match.arg(key.class) 66 | heap <- switch(key.class, 67 | "character" = methods::new(fibonacci_heap_s), 68 | "numeric" = methods::new(fibonacci_heap_d), 69 | "integer" = methods::new(fibonacci_heap_i), 70 | stop("Error defining key class") 71 | ) 72 | 73 | methods::new("fibonacci_heap", 74 | .key.class = key.class, 75 | .heap = heap 76 | ) 77 | } 78 | -------------------------------------------------------------------------------- /R/ds_map.R: -------------------------------------------------------------------------------- 1 | # datastructures: Implementation of core datastructures for R. 2 | # 3 | # Copyright (C) Simon Dirmeier 4 | # 5 | # This file is part of datastructures. 6 | # 7 | # datastructures is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # datastructures is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with datastructures. If not, see . 19 | 20 | 21 | #' @include methods_peek.R 22 | #' @include methods_size.R 23 | #' @include methods_erase.R 24 | #' @include methods_clear.R 25 | 26 | 27 | #' @title Map class 28 | #' 29 | #' @name map-class 30 | #' @rdname map-class 31 | #' 32 | #' @description Abstract map class 33 | #' 34 | #' @slot .map \code{C++} object representing a mapping 35 | #' @slot .key.class the class of the keys 36 | #' 37 | setClass( 38 | "map", 39 | contains = "VIRTUAL", 40 | slots = list(.map = "ANY", .key.class = "character"), 41 | prototype = prototype(.map = NULL, .key.class = NA_character_) 42 | ) 43 | 44 | 45 | #' @noRd 46 | .head.map <- function(obj) { 47 | if (obj@.map$size()) { 48 | obj@.map$head() 49 | } else { 50 | NULL 51 | } 52 | } 53 | 54 | 55 | #' @noRd 56 | .show.map <- function(object) { 57 | clazz <- class(object)[1] 58 | pf <- ifelse(clazz == "bimap", " <--> ", " -> ") 59 | cat(paste0( 60 | "An object of class ", clazz, "<", 61 | object@.key.class, ",T", 62 | ">\n\n" 63 | )) 64 | li <- peek(object) 65 | for (l in names(li)) 66 | { 67 | e <- li[[l]] 68 | if (is.null(e)) e <- "NULL" 69 | cat(paste0(l, pf, class(e), "\n")) 70 | } 71 | if (is.null(li)) { 72 | cat(paste0("NULL", pf, "NULL", "\n")) 73 | } 74 | } 75 | 76 | 77 | #' @noRd 78 | .size.map <- function(obj) { 79 | obj@.map$size() 80 | } 81 | 82 | 83 | #' @noRd 84 | .clear.map <- function(obj) { 85 | obj@.map$clear() 86 | obj 87 | } 88 | 89 | 90 | #' @noRd 91 | .erase.map <- function(obj, key) { 92 | .check.key.class(obj, key) 93 | obj@.map$remove(key) 94 | 95 | obj 96 | } 97 | 98 | 99 | #' @rdname erase-methods 100 | setMethod( 101 | "erase", 102 | signature = signature(obj = "map", key = "vector", value = "missing"), 103 | function(obj, key) .erase.map(obj, key) 104 | ) 105 | 106 | 107 | #' @rdname clear-methods 108 | setMethod("clear", "map", .clear.map) 109 | 110 | 111 | #' @noRd 112 | setMethod("show", "map", .show.map) 113 | 114 | 115 | #' @rdname peek-methods 116 | setMethod("peek", "map", .head.map) 117 | 118 | 119 | #' @rdname size-methods 120 | setMethod("size", "map", .size.map) 121 | -------------------------------------------------------------------------------- /R/ds_map_hashmap.R: -------------------------------------------------------------------------------- 1 | # datastructures: Implementation of core datastructures for R. 2 | # 3 | # Copyright (C) Simon Dirmeier 4 | # 5 | # This file is part of datastructures. 6 | # 7 | # datastructures is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # datastructures is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with datastructures. If not, see . 19 | 20 | 21 | #' @include ds_map_unordered.R 22 | 23 | 24 | #' @title Hashmap class 25 | #' 26 | #' @exportClass hashmap 27 | #' @name hashmap-class 28 | #' @rdname hashmap-class 29 | #' 30 | #' @description Implementation of a hashmap data structure, i.e. an unordered 31 | #' collection of key-value pairs: 32 | #' \deqn{f: keys -> values.} 33 | #' Hashmaps only to store unique keys-value pairs. For a data structure 34 | #' where multiple identical keys can be stores see \code{\link{multimap}}. 35 | #' Inserting and accessing is amortized in \emph{O(1)}. 36 | #' \code{hashmap} wraps a C++ \code{unordered_map} using Rcpp modules. 37 | #' Also see \code{\linkS4class{bimap}} for mappings in both ways. 38 | #' 39 | #' @slot .map \code{C++} object representing a mapping 40 | #' @slot .key.class the class of the keys 41 | #' 42 | #' @seealso \code{\link{hashmap}} for creating a new \code{hashmap} object 43 | #' 44 | setClass("hashmap", contains = "unordered_map") 45 | 46 | 47 | #' @title Create a new \code{hashmap} 48 | #' 49 | #' @export 50 | #' @importFrom methods new 51 | #' 52 | #' @description Instantiates a new \code{\linkS4class{hashmap}} object, 53 | #' i.e. an unordered collection of key-value pairs with mapping 54 | #' \deqn{f: keys -> values}, where only unique key-value pairs 55 | #' can be stored. 56 | #' 57 | #' @param key.class the primitive class type of the keys 58 | #' 59 | #' @return returns a new \code{hashmap} object 60 | #' 61 | #' @examples 62 | #' # creates a hashmap 63 | #' h <- hashmap() 64 | #' 65 | #' # creates a hashmap 66 | #' h <- hashmap("integer") 67 | #' 68 | #' # creates a hashmap 69 | #' h <- hashmap("numeric") 70 | hashmap <- function(key.class = c("character", "numeric", "integer")) { 71 | key.class <- match.arg(key.class) 72 | map <- switch( 73 | key.class, 74 | "character" = methods::new(hashmap_s), 75 | "numeric" = methods::new(hashmap_d), 76 | "integer" = methods::new(hashmap_i), 77 | stop("Error defining key class") 78 | ) 79 | 80 | methods::new("hashmap", 81 | .key.class = key.class, 82 | .map = map 83 | ) 84 | } 85 | -------------------------------------------------------------------------------- /R/ds_map_multimap.R: -------------------------------------------------------------------------------- 1 | # datastructures: Implementation of core datastructures for R. 2 | # 3 | # Copyright (C) Simon Dirmeier 4 | # 5 | # This file is part of datastructures. 6 | # 7 | # datastructures is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # datastructures is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with datastructures. If not, see . 19 | 20 | 21 | #' @include ds_map_unordered.R 22 | #' @include methods_erase.R 23 | 24 | 25 | #' @title Multimap class 26 | #' 27 | #' @exportClass multimap 28 | #' @name multimap-class 29 | #' @rdname multimap-class 30 | #' 31 | #' @description Implementation of a multimap data structure, i.e. an unordered 32 | #' collection of key-value pairs: 33 | #' \deqn{f: keys -> values.} 34 | #' Multimaps are able to store several identical keys. For a data structure 35 | #' which unique keys, see \code{\link{hashmap}}. 36 | #' Inserting and accessing is amortized in \emph{O(1)}. 37 | #' \code{hashmap} wraps a C++ \code{unordered_multimap} using Rcpp modules. 38 | #' Also see \code{\linkS4class{bimap}} for mappings in both ways. 39 | #' 40 | #' @slot .map \code{C++} object representing a mapping 41 | #' @slot .key.class the class of the keys 42 | #' 43 | #' @seealso \code{\link{multimap}} for creating a new \code{multimap} object 44 | #' 45 | setClass("multimap", contains = "unordered_map") 46 | 47 | 48 | #' @title Create a new \code{multimap} 49 | #' 50 | #' @export 51 | #' @importFrom methods new 52 | #' 53 | #' @description Instantiates a new \code{\linkS4class{multimap}} object, 54 | #' i.e. an unordered collection of key-value pairs with mapping 55 | #' \deqn{f: keys -> values}, where multiple identical key-value paors 56 | #' can be stored. 57 | #' 58 | #' @param key.class the primitive class type of the keys 59 | #' 60 | #' @return returns a new \code{multimap} object 61 | #' 62 | #' @examples 63 | #' # creates a new multimap 64 | #' m <- multimap() 65 | #' 66 | #' # creates a new multimap 67 | #' m <- multimap("numeric") 68 | #' 69 | #' # creates a new multimap 70 | #' m <- multimap("integer") 71 | multimap <- function(key.class = c("character", "numeric", "integer")) { 72 | key.class <- match.arg(key.class) 73 | key.class <- match.arg(key.class) 74 | map <- switch( 75 | key.class, 76 | "character" = methods::new(multimap_s), 77 | "numeric" = methods::new(multimap_d), 78 | "integer" = methods::new(multimap_i), 79 | stop("Error defining key class") 80 | ) 81 | 82 | methods::new("multimap", 83 | .key.class = key.class, 84 | .map = map 85 | ) 86 | } 87 | 88 | 89 | #' @noRd 90 | .erase.multimap <- function(obj, key, value) { 91 | .check.key.class(obj, key) 92 | if (length(key) != length(value)) { 93 | stop("dimensions of keys and values do not match") 94 | } 95 | obj@.map$remove_with_value(key, value) 96 | 97 | obj 98 | } 99 | 100 | 101 | #' @rdname erase-methods 102 | setMethod( 103 | "erase", 104 | signature = signature(obj = "multimap", key = "vector", value = "vector"), 105 | function(obj, key, value) { 106 | if (length(key) == 1) { 107 | value <- list(value) 108 | } else if (length(key) == length(value) && is.vector(value)) { 109 | value <- as.list(value) 110 | } 111 | .erase.multimap(obj, key, value) 112 | } 113 | ) 114 | 115 | 116 | #' @rdname erase-methods 117 | setMethod( 118 | "erase", 119 | signature = signature(obj = "multimap", key = "vector", value = "list"), 120 | function(obj, key, value) { 121 | if (length(key) == 1) value <- list(value) 122 | .erase.multimap(obj, key, value) 123 | } 124 | ) 125 | 126 | 127 | #' @rdname erase-methods 128 | setMethod( 129 | "erase", 130 | signature = signature(obj = "multimap", key = "vector", value = "ANY"), 131 | function(obj, key, value) { 132 | .erase.multimap(obj, key, list(value)) 133 | } 134 | ) 135 | -------------------------------------------------------------------------------- /R/ds_map_unordered.R: -------------------------------------------------------------------------------- 1 | # datastructures: Implementation of core datastructures for R. 2 | # 3 | # Copyright (C) Simon Dirmeier 4 | # 5 | # This file is part of datastructures. 6 | # 7 | # datastructures is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # datastructures is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with datastructures. If not, see . 19 | 20 | 21 | #' @include ds_map.R 22 | #' @include methods_insert.R 23 | #' @include methods_at.R 24 | #' @include methods_keys.R 25 | #' @include methods_values.R 26 | NULL 27 | 28 | 29 | #' @title Abstract unordered map class 30 | #' 31 | #' @name unordered_map-class 32 | #' @rdname unordered_map-class 33 | #' 34 | #' @description Abstract unordered map class 35 | #' 36 | #' @slot .map \code{C++} object representing a mapping 37 | #' @slot .key.class the class of the keys 38 | #' 39 | setClass( 40 | "unordered_map", 41 | contains = c("map", "VIRTUAL"), 42 | prototype = prototype(.map = NULL, .key.class = NA_character_) 43 | ) 44 | 45 | 46 | #' @noRd 47 | #' @importFrom methods is 48 | .insert.unordered_map <- function(obj, x, y) { 49 | if (length(x) != length(y)) { 50 | stop("dimensions of keys and values do not match", call. = FALSE) 51 | } 52 | 53 | .check.key.class(obj, x) 54 | if (methods::is(obj, "hashmap")) erase(obj, x) 55 | obj@.map$insert(x, y) 56 | 57 | obj 58 | } 59 | 60 | 61 | #' @rdname insert-methods 62 | setMethod( 63 | "insert", 64 | signature = signature(obj = "unordered_map", x = "vector", y = "vector"), 65 | function(obj, x, y) { 66 | if (length(x) == 1) { 67 | y <- list(y) 68 | } else if (length(x) == length(y) && is.vector(y)) { 69 | y <- as.list(y) 70 | } 71 | .insert.unordered_map(obj, x, y) 72 | } 73 | ) 74 | 75 | 76 | #' @rdname insert-methods 77 | setMethod( 78 | "insert", 79 | signature = signature(obj = "unordered_map", x = "vector", y = "list"), 80 | function(obj, x, y) { 81 | if (length(x) == 1) y <- list(y) 82 | .insert.unordered_map(obj, x, y) 83 | } 84 | ) 85 | 86 | 87 | #' @rdname insert-methods 88 | setMethod( 89 | "insert", 90 | signature = signature(obj = "unordered_map", x = "vector", y = "ANY"), 91 | function(obj, x, y) .insert.unordered_map(obj, x, list(y)) 92 | ) 93 | 94 | 95 | #' Insert parts to an object 96 | #' 97 | #' @description Inserts pairs to an unordered_map. 98 | #' 99 | #' @param x x an unorderd map object, such as a \code{\link{hashmap}} or 100 | #' \code{\link{multimap}} 101 | #' @param i a vector of keys 102 | #' @param value a vector of values for the keys 103 | setMethod( 104 | "[<-", 105 | signature = signature( 106 | x = "unordered_map", i = "vector", j = "missing", value = "ANY" 107 | ), 108 | function(x, i, value) insert(x, i, value) 109 | ) 110 | 111 | 112 | #' Insert parts to an object 113 | #' 114 | #' @description Inserts pairs to an unordered_map. 115 | #' 116 | #' @param x x an unorderd map object, such as a \code{\link{hashmap}} or 117 | #' \code{\link{multimap}} 118 | #' @param i a vector of keys 119 | #' @param value a vector of values for the keys 120 | setMethod( 121 | "[<-", 122 | signature = signature( 123 | x = "unordered_map", i = "vector", j = "missing", value = "vector" 124 | ), 125 | function(x, i, value) insert(x, i, value) 126 | ) 127 | 128 | 129 | #' Insert parts to an object 130 | #' 131 | #' @description Inserts pairs to an unordered_map. 132 | #' 133 | #' @param x x an unorderd map object, such as a \code{\link{hashmap}} or 134 | #' \code{\link{multimap}} 135 | #' @param i a vector of keys 136 | #' @param value a vector of values for the keys 137 | setMethod( 138 | "[<-", 139 | signature = signature( 140 | x = "unordered_map", i = "vector", j = "missing", value = "list" 141 | ), 142 | function(x, i, value) insert(x, i, value) 143 | ) 144 | 145 | 146 | #' @rdname at-methods 147 | setMethod( 148 | "at", 149 | signature = signature(obj = "unordered_map", x = "vector", which = "missing"), 150 | function(obj, x) { 151 | .check.key.class(obj, x) 152 | obj@.map$get(x) 153 | } 154 | ) 155 | 156 | 157 | #' @title Extract elements from an object 158 | #' 159 | #' @description Access pairs of an unordered map using a 160 | #' set of keys. 161 | #' 162 | #' @param x an unorderd map object, such as a \code{\link{hashmap}} or 163 | #' \code{\link{multimap}} 164 | #' @param i a vector of keys 165 | setMethod( 166 | "[", 167 | signature = signature( 168 | x = "unordered_map", i = "vector", j = "missing", drop = "missing" 169 | ), 170 | function(x, i) at(x, i) 171 | ) 172 | 173 | 174 | #' @rdname keys-methods 175 | setMethod("keys", "unordered_map", function(obj) obj@.map$keys()) 176 | 177 | 178 | #' @rdname values-methods 179 | setMethod("values", "unordered_map", function(obj) obj@.map$values()) 180 | -------------------------------------------------------------------------------- /R/methods_at.R: -------------------------------------------------------------------------------- 1 | #' # datastructures: Implementation of core datastructures for R. 2 | #' # 3 | #' # Copyright (C) Simon Dirmeier 4 | #' # 5 | #' # This file is part of datastructures. 6 | #' # 7 | #' # datastructures is free software: you can redistribute it and/or modify 8 | #' # it under the terms of the GNU General Public License as published by 9 | #' # the Free Software Foundation, either version 3 of the License, or 10 | #' # (at your option) any later version. 11 | #' # 12 | #' # datastructures is distributed in the hope that it will be useful, 13 | #' # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | #' # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | #' # GNU General Public License for more details. 16 | #' # 17 | #' # You should have received a copy of the GNU General Public License 18 | #' # along with datastructures. If not, see . 19 | 20 | 21 | #' @title Access elements from an object 22 | #' 23 | #' @description Extracts a set of pairs. For \code{hashmaps} 24 | #' mappings from 25 | #' \deqn{f: keys -> values,} 26 | #' exist so argument \code{which} is per default \code{values} (since these 27 | #' are going to be retrieved). For \code{bimaps} also 28 | #' \deqn{f: values -> keys,} 29 | #' mappings exist, such that \code{which} can also be \code{keys} if the keys 30 | #' from the object should be retrieved. 31 | #' 32 | #' @export 33 | #' @docType methods 34 | #' @rdname at-methods 35 | #' 36 | #' @param obj object to extract values from 37 | #' @param x the set of keys to match the values 38 | #' @param which choose either \code{values} if the values should get returned 39 | #' @param ... other arguments 40 | #' or \code{keys} if the keys should get returned 41 | #' 42 | #' @return returns extracted keys or values from \code{obj} 43 | #' @examples 44 | #' 45 | #' # access values from a hashmap 46 | #' h_map <- hashmap("integer") 47 | #' h_map[seq(2)] <- list(data.frame(a = rexp(3), b = rnorm(3)), environment()) 48 | #' h_map[1L] 49 | #' 50 | #' # access values or keys from a bimap 51 | #' b_map <- bimap("integer", "character") 52 | #' b_map[seq(5)] <- letters[seq(5)] 53 | #' at(b_map, c(1L, 3L)) 54 | #' at(b_map, c(1L, 3L), which = "values") 55 | #' at(b_map, c("a", "c"), which = "keys") 56 | #' 57 | #' # access values from a multimap 58 | #' m_map <- multimap("integer") 59 | #' m_map[c(seq(5), seq(5))] <- letters[seq(10)] 60 | #' at(m_map, 1L) 61 | setGeneric( 62 | "at", 63 | function(obj, x, which = c("values", "keys"), ...) { 64 | standardGeneric("at") 65 | }, 66 | package = "datastructures" 67 | ) 68 | -------------------------------------------------------------------------------- /R/methods_clear.R: -------------------------------------------------------------------------------- 1 | # datastructures: Implementation of core datastructures for R. 2 | # 3 | # Copyright (C) Simon Dirmeier 4 | # 5 | # This file is part of datastructures. 6 | # 7 | # datastructures is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # datastructures is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with datastructures. If not, see . 19 | 20 | 21 | #' @title Remove all elements from a datastructure 22 | #' 23 | #' @description Removes every element that is stored in a data structure and 24 | #' resets everything. 25 | #' 26 | #' @export 27 | #' @docType methods 28 | #' @rdname clear-methods 29 | #' 30 | #' @param obj the object to clear 31 | #' 32 | #' @examples 33 | #' 34 | #' # clears a multimap 35 | #' m_map <- multimap() 36 | #' m_map <- insert(m_map, c("a", "b"), 1:2) 37 | #' m_map <- insert(m_map, c("a", "b"), list(1, list(a = 1))) 38 | #' m_map <- clear(m_map) 39 | #' 40 | #' 41 | #' # clears a heap 42 | #' f_heap <- fibonacci_heap("integer") 43 | #' f_heap <- insert(f_heap, 1:2, 1:2) 44 | #' f_heap[3:4] <- list(1, list(a = 1)) 45 | #' f_heap <- clear(f_heap) 46 | #' 47 | #' # clears a \code{deque} 48 | #' s <- stack() 49 | #' s <- insert(s, list(1, vector(), list(3), data.frame(rnorm(3)))) 50 | #' s <- clear(s) 51 | setGeneric( 52 | "clear", 53 | function(obj) { 54 | standardGeneric("clear") 55 | }, 56 | package = "datastructures" 57 | ) 58 | -------------------------------------------------------------------------------- /R/methods_decrease.R: -------------------------------------------------------------------------------- 1 | # datastructures: Implementation of core datastructures for R. 2 | # 3 | # Copyright (C) Simon Dirmeier 4 | # 5 | # This file is part of datastructures. 6 | # 7 | # datastructures is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # datastructures is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with datastructures. If not, see . 19 | 20 | 21 | #' @title Decreases the key of a node in a heap 22 | #' 23 | #' @description Decreases the key of a node in a heap and updates the complete 24 | #' heap. The key is decreases \code{from} a value \code{to} a value by that moving the 25 | #' node's position in the heap. If a node cannot uniquely be identified using 26 | #' the \code{to} key, a \code{\link{handle}} needs to be given in addition. 27 | #' 28 | #' @export 29 | #' @docType methods 30 | #' @rdname decrease_key-methods 31 | #' 32 | #' @param obj a heap object 33 | #' @param from a key in the heap for which the node should be decreased 34 | #' @param to the new value of the heap 35 | #' @param handle the handle of the specific node that is decreased 36 | #' 37 | #' @return returns extracted handles and values from \code{obj} 38 | #' 39 | #' @examples 40 | #' 41 | #' # decreases the key of a heap 42 | #' f_heap <- fibonacci_heap("integer") 43 | #' f_heap <- insert(f_heap, 1:5, letters[1:5]) 44 | #' peek(f_heap) 45 | #' 46 | #' decrease_key(f_heap, 5L, -1L) 47 | #' peek(f_heap) 48 | #' 49 | #' hand <- handle(f_heap, value = letters[3]) 50 | #' decrease_key(f_heap, hand[[1]]$key, -2L) 51 | #' peek(f_heap) 52 | setGeneric( 53 | "decrease_key", 54 | function(obj, from, to, handle) { 55 | standardGeneric("decrease_key") 56 | }, 57 | package = "datastructures" 58 | ) 59 | -------------------------------------------------------------------------------- /R/methods_erase.R: -------------------------------------------------------------------------------- 1 | # datastructures: Implementation of core datastructures for R. 2 | # 3 | # Copyright (C) Simon Dirmeier 4 | # 5 | # This file is part of datastructures. 6 | # 7 | # datastructures is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # datastructures is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with datastructures. If not, see . 19 | 20 | 21 | #' @title Erase an entry from a map 22 | #' 23 | #' @description Erase a vector of key-value pair from a \code{map} object. 24 | #' 25 | #' @export 26 | #' @docType methods 27 | #' @rdname erase-methods 28 | #' 29 | #' @param obj the object to pop an element from 30 | #' @param key a vector of keys that should be removed 31 | #' @param value optionally a list of values needs to be supplied for some data 32 | #' structures such as \code{multimap}s if a single key-value pair should removed. If 33 | #' not provided removes all key-value pairs with a specific key. 34 | 35 | #' @return returns \code{obj} with removed values 36 | #' @examples 37 | #' 38 | #' # erases keys from a hashmap or bimap 39 | #' h_map <- hashmap() 40 | #' h_map[letters] <- rnorm(length(letters)) 41 | #' h_map <- erase(h_map, "a") 42 | #' h_map <- erase(h_map, letters[2:5]) 43 | #' 44 | #' # erases keys from a multimap 45 | #' m_map <- multimap() 46 | #' m_map[c("a", "a", "a", "b", "b", "c")] <- rep(1:2, 3) 47 | #' m_map <- erase(m_map, "a") 48 | #' m_map <- erase(m_map, "b", 1) 49 | setGeneric( 50 | "erase", 51 | function(obj, key, value) { 52 | standardGeneric("erase") 53 | }, 54 | package = "datastructures" 55 | ) 56 | -------------------------------------------------------------------------------- /R/methods_handle.R: -------------------------------------------------------------------------------- 1 | # datastructures: Implementation of core datastructures for R. 2 | # 3 | # Copyright (C) Simon Dirmeier 4 | # 5 | # This file is part of datastructures. 6 | # 7 | # datastructures is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # datastructures is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with datastructures. If not, see . 19 | 20 | 21 | 22 | #' @title Get the handles and values for nodes of a specific key in a heap. 23 | #' 24 | #' @description Returns a list of handles and values for node elements that have 25 | #' a specific key. That means for a given key, the reference to the node 26 | #' (the handle) as well as the value of the node are returned. If one key fits 27 | #' fits multiple nodes, all of the values and handles are returned. This is 28 | #' needed in order to uniquely identify a node if, for example, 29 | #' \code{decrease_key} on a specific node is going to be called. 30 | #' 31 | #' @export 32 | #' @docType methods 33 | #' @rdname handle-methods 34 | #' 35 | #' @param obj a heap object 36 | #' @param key a key in the heap 37 | #' @param value a value in the heap 38 | #' 39 | #' @return returns extracted handles and values from \code{obj} 40 | #' 41 | #' @examples 42 | #' # returns the handle of a heap 43 | #' f_heap <- fibonacci_heap("integer") 44 | #' f_heap <- insert(f_heap, 1:5, letters[1:5]) 45 | #' 46 | #' handle(f_heap, key = 3L) 47 | #' 48 | #' handle(f_heap, value = letters[3]) 49 | setGeneric( 50 | "handle", 51 | function(obj, key, value) { 52 | standardGeneric("handle") 53 | }, 54 | package = "datastructures" 55 | ) 56 | -------------------------------------------------------------------------------- /R/methods_insert.R: -------------------------------------------------------------------------------- 1 | # datastructures: Implementation of core datastructures for R. 2 | # 3 | # Copyright (C) Simon Dirmeier 4 | # 5 | # This file is part of datastructures. 6 | # 7 | # datastructures is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # datastructures is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with datastructures. If not, see . 19 | 20 | 21 | #' @title Add elements to an object 22 | #' 23 | #' @description Adds keys or pairs to an object and returns the 24 | #' object. Depending on the datastructure used, either only keys are required 25 | #' or pairs of . Insertion of elements with vectors, i.e. giving 26 | #' multiple arguments at the same time is faster than inserting elements 27 | #' iteratively. 28 | #' 29 | #' @exportMethod insert 30 | #' @docType methods 31 | #' @rdname insert-methods 32 | #' 33 | #' @param obj object to insert into 34 | #' @param x the values/keys to insert into 35 | #' @param y values to be inserted which are required for some datastructures 36 | #' 37 | #' @return returns \code{obj} with inserted values 38 | #' 39 | #' @examples 40 | #' 41 | #' # inserts values into a multimap 42 | #' m_map <- multimap() 43 | #' m_map <- insert(m_map, c("a", "b"), 1:2) 44 | #' m_map <- insert(m_map, c("a", "b"), list(1, list(a = 1))) 45 | #' m_map["a"] <- rnorm(length(letters)) 46 | #' m_map[c("a", "b", "c")] <- list(1, data.frame(a = 2), environment()) 47 | #' 48 | #' # inserts values into a fibonacci_heap 49 | #' f_heap <- fibonacci_heap("integer") 50 | #' f_heap <- insert(f_heap, 1:2, 1:2) 51 | #' f_heap[3:4] <- list(1, list(a = 1)) 52 | #' f_heap <- insert(f_heap, 5:6, list(data.frame(a = rnorm(3)), diag(2))) 53 | #' 54 | #' # inserts elements into a queue or stack 55 | #' s <- stack() 56 | #' s <- insert(s, list(1, vector(), list(3), data.frame(rnorm(3)))) 57 | setGeneric( 58 | "insert", 59 | function(obj, x, y) { 60 | standardGeneric("insert") 61 | }, 62 | package = "datastructures" 63 | ) 64 | -------------------------------------------------------------------------------- /R/methods_keys.R: -------------------------------------------------------------------------------- 1 | # datastructures: Implementation of core datastructures for R. 2 | # 3 | # Copyright (C) Simon Dirmeier 4 | # 5 | # This file is part of datastructures. 6 | # 7 | # datastructures is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # datastructures is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with datastructures. If not, see . 19 | 20 | 21 | #' @title Get keys from an object 22 | #' 23 | #' @description Extracts the keys from a \code{map} object. 24 | #' 25 | #' @export 26 | #' @docType methods 27 | #' @rdname keys-methods 28 | #' 29 | #' @param obj object to extract keys from 30 | #' 31 | #' @return returns the extracted keys as vector 32 | #' 33 | #' @examples 34 | #' 35 | #' # returns the keys of a hashmap 36 | #' h_map <- hashmap("numeric") 37 | #' h_map[rnorm(3)] <- list(1, 2, 3) 38 | #' keys(h_map) 39 | #' 40 | #' # returns the keys of a multimap 41 | #' m_map <- multimap("numeric") 42 | #' m_map[c(1, 2, 1)] <- list(rnorm(1), rgamma(1, 1), rexp(1)) 43 | #' keys(m_map) 44 | setGeneric( 45 | "keys", 46 | function(obj) { 47 | standardGeneric("keys") 48 | } 49 | ) 50 | -------------------------------------------------------------------------------- /R/methods_peek.R: -------------------------------------------------------------------------------- 1 | # datastructures: Implementation of core datastructures for R. 2 | # 3 | # Copyright (C) Simon Dirmeier 4 | # 5 | # This file is part of datastructures. 6 | # 7 | # datastructures is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # datastructures is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with datastructures. If not, see . 19 | 20 | 21 | #' @title Have a look at the first element from an object without removing it 22 | #' 23 | #' @description Peeks into an object, i.e. takes the first element and returns 24 | #' it without removing it from the object. The data structure that has a peek 25 | #' method usually uses some sort of priority of its elements. 26 | #' 27 | #' @export 28 | #' @docType methods 29 | #' @rdname peek-methods 30 | #' 31 | #' @param obj the object to peek 32 | #' 33 | #' @return returns the first element from \code{obj} as list 34 | #' 35 | #' @examples 36 | #' 37 | #' # peeks into a queue 38 | #' q <- queue() 39 | #' q <- insert(q, list(environment(), data.frame(a = 1))) 40 | #' peek(q) 41 | #' 42 | #' # peeks into a fibonacci heap 43 | #' b_heap <- binomial_heap() 44 | #' b_heap <- insert(b_heap, letters[seq(3)], list(1, diag(3), rnorm(2))) 45 | #' peek(b_heap) 46 | #' 47 | #' # peeks into a \code{hashmap} 48 | #' h_map <- hashmap() 49 | #' h_map[letters] <- rnorm(length(letters)) 50 | #' peek(h_map) 51 | #' 52 | #' # peeks into a \code{bimap} 53 | #' b_map <- bimap("integer", "integer") 54 | #' b_map[seq(10)] <- seq(10, 1) 55 | #' peek(b_map) 56 | setGeneric( 57 | "peek", 58 | function(obj) { 59 | standardGeneric("peek") 60 | }, 61 | package = "datastructures" 62 | ) 63 | -------------------------------------------------------------------------------- /R/methods_pop.R: -------------------------------------------------------------------------------- 1 | # datastructures: Implementation of core datastructures for R. 2 | # 3 | # Copyright (C) Simon Dirmeier 4 | # 5 | # This file is part of datastructures. 6 | # 7 | # datastructures is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # datastructures is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with datastructures. If not, see . 19 | 20 | 21 | #' @title Pop a single element from an object 22 | #' 23 | #' @description Remove and return the first element from a data structure that 24 | #' has a priority, such as a \code{heap} or \code{deque}. 25 | #' 26 | #' @export 27 | #' @docType methods 28 | #' @rdname pop-methods 29 | #' 30 | #' @param obj the object to pop an element from 31 | 32 | #' @return returns the first element from \code{obj} as list 33 | #' 34 | #' @examples 35 | #' 36 | #' # pops from a queue 37 | #' q <- queue() 38 | #' q <- insert(q, list(environment(), data.frame(a = 1))) 39 | #' pop(q) 40 | #' 41 | #' # pops from a stack 42 | #' s <- stack() 43 | #' s <- insert(s, list(environment(), data.frame(a = 1))) 44 | #' pop(s) 45 | #' 46 | #' # pops from a fibonacci heap 47 | #' b_heap <- binomial_heap() 48 | #' b_heap <- insert(b_heap, letters[seq(3)], list(1, diag(3), rnorm(2))) 49 | #' pop(b_heap) 50 | setGeneric( 51 | "pop", 52 | function(obj) { 53 | standardGeneric("pop") 54 | }, 55 | package = "datastructures" 56 | ) 57 | -------------------------------------------------------------------------------- /R/methods_size.R: -------------------------------------------------------------------------------- 1 | # datastructures: Implementation of core datastructures for R. 2 | # 3 | # Copyright (C) Simon Dirmeier 4 | # 5 | # This file is part of datastructures. 6 | # 7 | # datastructures is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # datastructures is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with datastructures. If not, see . 19 | 20 | 21 | #' @title Get the size of an object 22 | #' 23 | #' @description Computes the size of an object, i.e. the number of keys or 24 | #' pairs depending on the object. 25 | #' 26 | #' @export 27 | #' @docType methods 28 | #' @rdname size-methods 29 | #' 30 | #' @param obj the object to get the size from 31 | #' 32 | #' @return returns the size of \code{obj} 33 | #' 34 | #' @examples 35 | #' 36 | #' # get the size of a hashmap 37 | #' h_map <- hashmap() 38 | #' h_map[letters] <- rnorm(length(letters)) 39 | #' size(h_map) 40 | #' 41 | #' # get the size of a fibonacci heap 42 | #' f_heap <- fibonacci_heap() 43 | #' f_heap <- insert(f_heap, letters[seq(3)], list(1, diag(3), rnorm(2))) 44 | #' size(f_heap) 45 | #' 46 | #' # get the size of a stack 47 | #' s <- stack() 48 | #' s <- insert(s, list(1)) 49 | #' size(s) 50 | setGeneric( 51 | "size", 52 | function(obj) { 53 | standardGeneric("size") 54 | }, 55 | package = "datastructures" 56 | ) 57 | -------------------------------------------------------------------------------- /R/methods_values.R: -------------------------------------------------------------------------------- 1 | # datastructures: Implementation of core datastructures for R. 2 | # 3 | # Copyright (C) Simon Dirmeier 4 | # 5 | # This file is part of datastructures. 6 | # 7 | # datastructures is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # datastructures is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with datastructures. If not, see . 19 | 20 | 21 | #' @title Get values from an object 22 | #' 23 | #' @description Extracts the values from a data structure such as a \code{map} 24 | #' or \code{heap} object. 25 | #' 26 | #' @export 27 | #' @docType methods 28 | #' @rdname values-methods 29 | #' 30 | #' @param obj object to extract values from 31 | #' 32 | #' @return returns the extracted values as a \code{list} or, when primitive, as 33 | #' a \code{vector}. In case of a \code{heap} also returns \code{key} and \code{handle} 34 | #' of the heap node. 35 | #' 36 | #' @examples 37 | #' 38 | #' # shows the values of a hashmap 39 | #' h_map <- hashmap("integer") 40 | #' h_map <- insert(h_map, seq(2), list(data.frame(a = 1), 3)) 41 | #' values(h_map) 42 | #' 43 | #' # shows the values of a multimap 44 | #' m_map <- multimap("integer") 45 | #' m_map[seq(2)] <- list(diag(2), rnorm(3)) 46 | #' values(m_map) 47 | #' 48 | #' # shows the values of a heap 49 | #' f_heap <- fibonacci_heap("integer") 50 | #' f_heap <- insert(f_heap, 1:2, list(diag(2), rnorm(3))) 51 | #' values(f_heap) 52 | setGeneric( 53 | "values", 54 | function(obj) { 55 | standardGeneric("values") 56 | }, 57 | package = "datastructures" 58 | ) 59 | -------------------------------------------------------------------------------- /R/zzz.R: -------------------------------------------------------------------------------- 1 | # datastructures: Implementation of core datastructures for R. 2 | # 3 | # Copyright (C) Simon Dirmeier 4 | # 5 | # This file is part of datastructures. 6 | # 7 | # datastructures is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # datastructures is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with datastructures. If not, see . 19 | 20 | #' @import Rcpp 21 | .onLoad <- function(libname, pkgname) { 22 | Rcpp::loadModule("bimap_module", TRUE) 23 | Rcpp::loadModule("hashmap_module", TRUE) 24 | Rcpp::loadModule("multimap_module", TRUE) 25 | 26 | Rcpp::loadModule("binomial_heap_module", TRUE) 27 | Rcpp::loadModule("fibonacci_heap_module", TRUE) 28 | 29 | Rcpp::loadModule("queue_module", TRUE) 30 | Rcpp::loadModule("stack_module", TRUE) 31 | } 32 | 33 | 34 | .onUnload <- function(libpath) { 35 | library.dynam.unload("datastructures", libpath) 36 | } 37 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # datastructures 2 | 3 | [![Project Status](http://www.repostatus.org/badges/latest/active.svg)](http://www.repostatus.org/#active) 4 | [![Project Life](https://img.shields.io/badge/lifecycle-maturing-blue.svg)](https://www.tidyverse.org/lifecycle/#maturing) 5 | [![ci](https://github.com/dirmeier/datastructures/workflows/ci/badge.svg)](https://github.com/dirmeier/datastructures/actions?query=workflow%3Aci) 6 | [![codecov](https://codecov.io/gh/dirmeier/datastructures/branch/master/graph/badge.svg)](https://codecov.io/gh/dirmeier/datastructures) 7 | [![CRAN](http://www.r-pkg.org/badges/version/datastructures?color=brightgreen)](https://cran.r-project.org/package=datastructures) 8 | 9 | Implementation of core data structures for R. 10 | 11 | ## Introduction 12 | 13 | Implementation of advanced data structures such as hashmaps, heaps, or queues in `R`. 14 | Advanced data structures are essential in many computer science and statistics 15 | problems, for example graph algorithms or string analysis. The package uses 16 | `Boost` and `STL` data types and extends these to `R` with `Rcpp` modules. 17 | 18 | So far `datastructures` has implementations for: 19 | 20 | * Fibonacci and binomial heaps, 21 | * queues and stacks, 22 | * hashmaps, multimaps and bimaps. 23 | 24 | As an introductory example, imagine that you want to compute shortest paths on a 25 | graph and decide to use a Fibonacci heap for keeping the distances. A Fibonacci heap is an efficient tree-like data structure 26 | that satisfies the *min-heap property*. We can use it to quickly get the node with the shortest distance in *O(log n)* time like this: 27 | 28 | ```R 29 | fh <- fibonacci_heap("numeric") 30 | node.labels <- paste0("n", 10:1) 31 | node.distances <- seq(1, 0, length.out=length(node.labels)) 32 | fh <- insert(fh, node.distances, node.labels) 33 | 34 | peek(fh) 35 | $`0` 36 | [1] "n1" 37 | ``` 38 | 39 | `datastructures` also allows storing non-orimitive objects, like `data.frames`, `matrices` or `environments`. 40 | For instance, we could use a hashmap for storing such objects: 41 | 42 | ```R 43 | hm <- hashmap("integer") 44 | keys <- 1:2 45 | values <- list( 46 | environment(), 47 | data.frame(A=rbeta(3, .5, .5), B=rgamma(3, 1))) 48 | hm[keys] <- values 49 | 50 | hm[1L] 51 | [[1]] 52 | 53 | ``` 54 | 55 | ## Installation 56 | 57 | Get the package from *CRAN* using: 58 | 59 | ```R 60 | install.packages("datastructures") 61 | ``` 62 | 63 | You can also download the tarball of the latest release and install with: 64 | 65 | ```bash 66 | R CMD install 67 | ``` 68 | 69 | where `` is your downloaded tarball. If you want 70 | to you can also use devtools, but I don't recommend it since it might give unstable 71 | versions: 72 | 73 | ```R 74 | devtools::install_github("dirmeier/datastructures") 75 | ``` 76 | 77 | ## Documentation 78 | 79 | Load the library using `library(datastructures)`. We provide a vignette for 80 | the package that can be called using: `vignette("datastructures")`. If there 81 | are any questions let met know. 82 | 83 | ## Citation 84 | 85 | If you want to cite `datastructures`, please use the following entry: 86 | 87 | > Dirmeier, Simon (2018). `datastructures`: An R package for organisation and storage of data. Journal of Open Source Software, 3(28), 910, https://doi.org/10.21105/joss.00910 88 | 89 | ## Feature requests and contributing 90 | 91 | If you want to have another datastructure added, say from `boost` or the `STL`, 92 | just open up a new issue. Alternatively it would be great if you provided a PR. 93 | 94 | ## Author 95 | 96 | * Simon Dirmeier simon.dirmeier@web.de 97 | -------------------------------------------------------------------------------- /cleanup: -------------------------------------------------------------------------------- 1 | #!/bin/env bash 2 | 3 | echo "Cleaning up..." 4 | rm -rf \ 5 | src/datastructures.so \ 6 | src/*.o \ 7 | src/*.a 8 | -------------------------------------------------------------------------------- /datastructures.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 4 10 | Encoding: UTF-8 11 | 12 | RnwWeave: knitr 13 | LaTeX: pdfLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | StripTrailingWhitespace: Yes 17 | 18 | BuildType: Package 19 | PackageUseDevtools: Yes 20 | PackageInstallArgs: --no-multiarch --with-keep.source 21 | PackageCheckArgs: --no-multiarch --with-keep.source --as-cran 22 | PackageRoxygenize: rd,collate,namespace 23 | -------------------------------------------------------------------------------- /docs/articles/datastructures_files/accessible-code-block-0.0.1/empty-anchor.js: -------------------------------------------------------------------------------- 1 | // Hide empty tag within highlighted CodeBlock for screen reader accessibility (see https://github.com/jgm/pandoc/issues/6352#issuecomment-626106786) --> 2 | // v0.0.1 3 | // Written by JooYoung Seo (jooyoung@psu.edu) and Atsushi Yasumoto on June 1st, 2020. 4 | 5 | document.addEventListener('DOMContentLoaded', function() { 6 | const codeList = document.getElementsByClassName("sourceCode"); 7 | for (var i = 0; i < codeList.length; i++) { 8 | var linkList = codeList[i].getElementsByTagName('a'); 9 | for (var j = 0; j < linkList.length; j++) { 10 | if (linkList[j].innerHTML === "") { 11 | linkList[j].setAttribute('aria-hidden', 'true'); 12 | } 13 | } 14 | } 15 | }); 16 | -------------------------------------------------------------------------------- /docs/articles/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Articles • datastructures 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 57 | 58 | 59 | 60 | 61 | 62 | 63 |
64 |
65 | 125 | 126 | 127 | 128 |
129 | 130 |
131 |
132 | 135 | 136 |
137 |

All vignettes

138 |

139 | 140 |
141 |
Datastructures tutorial
142 |
143 |
144 |
145 |
146 |
147 | 148 | 149 |
150 | 153 | 154 |
155 |

Site built with pkgdown 1.5.1.

156 |
157 | 158 |
159 |
160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | -------------------------------------------------------------------------------- /docs/authors.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Authors • datastructures 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 57 | 58 | 59 | 60 | 61 | 62 | 63 |
64 |
65 | 125 | 126 | 127 | 128 |
129 | 130 |
131 |
132 | 135 | 136 | 142 | 143 |
144 | 145 |
146 | 147 | 148 | 149 |
150 | 153 | 154 |
155 |

Site built with pkgdown 1.5.1.

156 |
157 | 158 |
159 |
160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | -------------------------------------------------------------------------------- /docs/bootstrap-toc.css: -------------------------------------------------------------------------------- 1 | /*! 2 | * Bootstrap Table of Contents v0.4.1 (http://afeld.github.io/bootstrap-toc/) 3 | * Copyright 2015 Aidan Feldman 4 | * Licensed under MIT (https://github.com/afeld/bootstrap-toc/blob/gh-pages/LICENSE.md) */ 5 | 6 | /* modified from https://github.com/twbs/bootstrap/blob/94b4076dd2efba9af71f0b18d4ee4b163aa9e0dd/docs/assets/css/src/docs.css#L548-L601 */ 7 | 8 | /* All levels of nav */ 9 | nav[data-toggle='toc'] .nav > li > a { 10 | display: block; 11 | padding: 4px 20px; 12 | font-size: 13px; 13 | font-weight: 500; 14 | color: #767676; 15 | } 16 | nav[data-toggle='toc'] .nav > li > a:hover, 17 | nav[data-toggle='toc'] .nav > li > a:focus { 18 | padding-left: 19px; 19 | color: #563d7c; 20 | text-decoration: none; 21 | background-color: transparent; 22 | border-left: 1px solid #563d7c; 23 | } 24 | nav[data-toggle='toc'] .nav > .active > a, 25 | nav[data-toggle='toc'] .nav > .active:hover > a, 26 | nav[data-toggle='toc'] .nav > .active:focus > a { 27 | padding-left: 18px; 28 | font-weight: bold; 29 | color: #563d7c; 30 | background-color: transparent; 31 | border-left: 2px solid #563d7c; 32 | } 33 | 34 | /* Nav: second level (shown on .active) */ 35 | nav[data-toggle='toc'] .nav .nav { 36 | display: none; /* Hide by default, but at >768px, show it */ 37 | padding-bottom: 10px; 38 | } 39 | nav[data-toggle='toc'] .nav .nav > li > a { 40 | padding-top: 1px; 41 | padding-bottom: 1px; 42 | padding-left: 30px; 43 | font-size: 12px; 44 | font-weight: normal; 45 | } 46 | nav[data-toggle='toc'] .nav .nav > li > a:hover, 47 | nav[data-toggle='toc'] .nav .nav > li > a:focus { 48 | padding-left: 29px; 49 | } 50 | nav[data-toggle='toc'] .nav .nav > .active > a, 51 | nav[data-toggle='toc'] .nav .nav > .active:hover > a, 52 | nav[data-toggle='toc'] .nav .nav > .active:focus > a { 53 | padding-left: 28px; 54 | font-weight: 500; 55 | } 56 | 57 | /* from https://github.com/twbs/bootstrap/blob/e38f066d8c203c3e032da0ff23cd2d6098ee2dd6/docs/assets/css/src/docs.css#L631-L634 */ 58 | nav[data-toggle='toc'] .nav > .active > ul { 59 | display: block; 60 | } 61 | -------------------------------------------------------------------------------- /docs/bootstrap-toc.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * Bootstrap Table of Contents v0.4.1 (http://afeld.github.io/bootstrap-toc/) 3 | * Copyright 2015 Aidan Feldman 4 | * Licensed under MIT (https://github.com/afeld/bootstrap-toc/blob/gh-pages/LICENSE.md) */ 5 | (function() { 6 | 'use strict'; 7 | 8 | window.Toc = { 9 | helpers: { 10 | // return all matching elements in the set, or their descendants 11 | findOrFilter: function($el, selector) { 12 | // http://danielnouri.org/notes/2011/03/14/a-jquery-find-that-also-finds-the-root-element/ 13 | // http://stackoverflow.com/a/12731439/358804 14 | var $descendants = $el.find(selector); 15 | return $el.filter(selector).add($descendants).filter(':not([data-toc-skip])'); 16 | }, 17 | 18 | generateUniqueIdBase: function(el) { 19 | var text = $(el).text(); 20 | var anchor = text.trim().toLowerCase().replace(/[^A-Za-z0-9]+/g, '-'); 21 | return anchor || el.tagName.toLowerCase(); 22 | }, 23 | 24 | generateUniqueId: function(el) { 25 | var anchorBase = this.generateUniqueIdBase(el); 26 | for (var i = 0; ; i++) { 27 | var anchor = anchorBase; 28 | if (i > 0) { 29 | // add suffix 30 | anchor += '-' + i; 31 | } 32 | // check if ID already exists 33 | if (!document.getElementById(anchor)) { 34 | return anchor; 35 | } 36 | } 37 | }, 38 | 39 | generateAnchor: function(el) { 40 | if (el.id) { 41 | return el.id; 42 | } else { 43 | var anchor = this.generateUniqueId(el); 44 | el.id = anchor; 45 | return anchor; 46 | } 47 | }, 48 | 49 | createNavList: function() { 50 | return $(''); 51 | }, 52 | 53 | createChildNavList: function($parent) { 54 | var $childList = this.createNavList(); 55 | $parent.append($childList); 56 | return $childList; 57 | }, 58 | 59 | generateNavEl: function(anchor, text) { 60 | var $a = $(''); 61 | $a.attr('href', '#' + anchor); 62 | $a.text(text); 63 | var $li = $('
  • '); 64 | $li.append($a); 65 | return $li; 66 | }, 67 | 68 | generateNavItem: function(headingEl) { 69 | var anchor = this.generateAnchor(headingEl); 70 | var $heading = $(headingEl); 71 | var text = $heading.data('toc-text') || $heading.text(); 72 | return this.generateNavEl(anchor, text); 73 | }, 74 | 75 | // Find the first heading level (`

    `, then `

    `, etc.) that has more than one element. Defaults to 1 (for `

    `). 76 | getTopLevel: function($scope) { 77 | for (var i = 1; i <= 6; i++) { 78 | var $headings = this.findOrFilter($scope, 'h' + i); 79 | if ($headings.length > 1) { 80 | return i; 81 | } 82 | } 83 | 84 | return 1; 85 | }, 86 | 87 | // returns the elements for the top level, and the next below it 88 | getHeadings: function($scope, topLevel) { 89 | var topSelector = 'h' + topLevel; 90 | 91 | var secondaryLevel = topLevel + 1; 92 | var secondarySelector = 'h' + secondaryLevel; 93 | 94 | return this.findOrFilter($scope, topSelector + ',' + secondarySelector); 95 | }, 96 | 97 | getNavLevel: function(el) { 98 | return parseInt(el.tagName.charAt(1), 10); 99 | }, 100 | 101 | populateNav: function($topContext, topLevel, $headings) { 102 | var $context = $topContext; 103 | var $prevNav; 104 | 105 | var helpers = this; 106 | $headings.each(function(i, el) { 107 | var $newNav = helpers.generateNavItem(el); 108 | var navLevel = helpers.getNavLevel(el); 109 | 110 | // determine the proper $context 111 | if (navLevel === topLevel) { 112 | // use top level 113 | $context = $topContext; 114 | } else if ($prevNav && $context === $topContext) { 115 | // create a new level of the tree and switch to it 116 | $context = helpers.createChildNavList($prevNav); 117 | } // else use the current $context 118 | 119 | $context.append($newNav); 120 | 121 | $prevNav = $newNav; 122 | }); 123 | }, 124 | 125 | parseOps: function(arg) { 126 | var opts; 127 | if (arg.jquery) { 128 | opts = { 129 | $nav: arg 130 | }; 131 | } else { 132 | opts = arg; 133 | } 134 | opts.$scope = opts.$scope || $(document.body); 135 | return opts; 136 | } 137 | }, 138 | 139 | // accepts a jQuery object, or an options object 140 | init: function(opts) { 141 | opts = this.helpers.parseOps(opts); 142 | 143 | // ensure that the data attribute is in place for styling 144 | opts.$nav.attr('data-toggle', 'toc'); 145 | 146 | var $topContext = this.helpers.createChildNavList(opts.$nav); 147 | var topLevel = this.helpers.getTopLevel(opts.$scope); 148 | var $headings = this.helpers.getHeadings(opts.$scope, topLevel); 149 | this.helpers.populateNav($topContext, topLevel, $headings); 150 | } 151 | }; 152 | 153 | $(function() { 154 | $('nav[data-toggle="toc"]').each(function(i, el) { 155 | var $nav = $(el); 156 | Toc.init($nav); 157 | }); 158 | }); 159 | })(); 160 | -------------------------------------------------------------------------------- /docs/docsearch.js: -------------------------------------------------------------------------------- 1 | $(function() { 2 | 3 | // register a handler to move the focus to the search bar 4 | // upon pressing shift + "/" (i.e. "?") 5 | $(document).on('keydown', function(e) { 6 | if (e.shiftKey && e.keyCode == 191) { 7 | e.preventDefault(); 8 | $("#search-input").focus(); 9 | } 10 | }); 11 | 12 | $(document).ready(function() { 13 | // do keyword highlighting 14 | /* modified from https://jsfiddle.net/julmot/bL6bb5oo/ */ 15 | var mark = function() { 16 | 17 | var referrer = document.URL ; 18 | var paramKey = "q" ; 19 | 20 | if (referrer.indexOf("?") !== -1) { 21 | var qs = referrer.substr(referrer.indexOf('?') + 1); 22 | var qs_noanchor = qs.split('#')[0]; 23 | var qsa = qs_noanchor.split('&'); 24 | var keyword = ""; 25 | 26 | for (var i = 0; i < qsa.length; i++) { 27 | var currentParam = qsa[i].split('='); 28 | 29 | if (currentParam.length !== 2) { 30 | continue; 31 | } 32 | 33 | if (currentParam[0] == paramKey) { 34 | keyword = decodeURIComponent(currentParam[1].replace(/\+/g, "%20")); 35 | } 36 | } 37 | 38 | if (keyword !== "") { 39 | $(".contents").unmark({ 40 | done: function() { 41 | $(".contents").mark(keyword); 42 | } 43 | }); 44 | } 45 | } 46 | }; 47 | 48 | mark(); 49 | }); 50 | }); 51 | 52 | /* Search term highlighting ------------------------------*/ 53 | 54 | function matchedWords(hit) { 55 | var words = []; 56 | 57 | var hierarchy = hit._highlightResult.hierarchy; 58 | // loop to fetch from lvl0, lvl1, etc. 59 | for (var idx in hierarchy) { 60 | words = words.concat(hierarchy[idx].matchedWords); 61 | } 62 | 63 | var content = hit._highlightResult.content; 64 | if (content) { 65 | words = words.concat(content.matchedWords); 66 | } 67 | 68 | // return unique words 69 | var words_uniq = [...new Set(words)]; 70 | return words_uniq; 71 | } 72 | 73 | function updateHitURL(hit) { 74 | 75 | var words = matchedWords(hit); 76 | var url = ""; 77 | 78 | if (hit.anchor) { 79 | url = hit.url_without_anchor + '?q=' + escape(words.join(" ")) + '#' + hit.anchor; 80 | } else { 81 | url = hit.url + '?q=' + escape(words.join(" ")); 82 | } 83 | 84 | return url; 85 | } 86 | -------------------------------------------------------------------------------- /docs/extra.css: -------------------------------------------------------------------------------- 1 | /* 2 | # datastructures: Implementation of Core Data Structures 3 | # 4 | # Copyright (C) 2018 Simon Dirmeier 5 | # 6 | # This file is part of datastructures 7 | # 8 | # datastructures is free software: you can redistribute it and/or modify 9 | # it under the terms of the GNU General Public License as published by 10 | # the Free Software Foundation, either version 3 of the License, or 11 | # (at your option) any later version. 12 | # 13 | # datastructures is distributed in the hope that it will be useful, 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | # GNU General Public License for more details. 17 | # 18 | # You should have received a copy of the GNU General Public License 19 | # along with datastructures. If not, see . 20 | # 21 | */ 22 | 23 | @import url('https://fonts.googleapis.com/css?family=Roboto|Hind|Inconsolata'); 24 | 25 | .section { 26 | margin-top: 30px; 27 | margin-bottom: 30px; 28 | } 29 | 30 | code,pre { 31 | font-family: "Inconsolata"; 32 | font-size: 1.4rem; 33 | } 34 | 35 | body { 36 | font-size: 1.4em; 37 | line-height: 1.6; 38 | font-family: "Roboto", "Helvetica Neue", Helvetica, Arial, sans-serif; 39 | color: #222; 40 | } 41 | 42 | h1, h2, h3, h4, h5, h6 { 43 | margin-top: 0; 44 | margin-bottom: 2rem; 45 | font-weight: 300; 46 | } 47 | 48 | h1 { 49 | font-size: 4.0rem; 50 | font-style: italic; 51 | 52 | } 53 | 54 | h2 { 55 | font-size: 3.0rem; 56 | line-height: 1.25; 57 | letter-spacing: -.1rem; 58 | } 59 | 60 | h3 { 61 | font-size: 2.5rem; 62 | line-height: 1.3; 63 | letter-spacing: -.1rem; 64 | } 65 | 66 | h4 { 67 | font-size: 2.0rem; 68 | line-height: 1.35; 69 | letter-spacing: -.08rem; 70 | } 71 | 72 | h5 { 73 | font-size: 1.8rem; 74 | line-height: 1.5; 75 | letter-spacing: -.05rem; 76 | } 77 | 78 | h6 { 79 | font-size: 1.5rem; 80 | line-height: 1.6; 81 | letter-spacing: 0; 82 | } 83 | 84 | .navbar { 85 | font-family: "Roboto", "Helvetica Neue", Helvetica, Arial, sans-serif; 86 | border-width: 0 1px 2px 1px; 87 | position: fixed; 88 | max-height: 20px; 89 | top: 0px; 90 | } 91 | 92 | .navbar .navbar-nav > li > a > .fa { 93 | font-size: 21px; 94 | } 95 | 96 | .navbar .navbar-nav > li > a { 97 | text-transform: uppercase; 98 | font-size: 11px; 99 | letter-spacing: .2rem; 100 | margin-right: 5px; 101 | color: #222; 102 | } 103 | 104 | .navbar .navbar-brand { 105 | text-transform: uppercase; 106 | font-weight: normal; 107 | font-size: 11px; 108 | font-weight: 300; 109 | letter-spacing: .2rem; 110 | font-style: italic; 111 | margin-right: 35px; 112 | text-decoration: none; 113 | color: #222; 114 | } 115 | 116 | .navbar .navbar-brand:hover { 117 | color: #1EAEDB; 118 | } 119 | 120 | .navbar .navbar-nav > li > a:hover, 121 | .navbar .navbar-brand > a:hover { 122 | color: #1EAEDB; 123 | } 124 | -------------------------------------------------------------------------------- /docs/jquery.sticky-kit.min.js: -------------------------------------------------------------------------------- 1 | /* Sticky-kit v1.1.2 | WTFPL | Leaf Corcoran 2015 | */ 2 | /* 3 | Source: https://github.com/leafo/sticky-kit 4 | License: MIT 5 | */ 6 | (function(){var b,f;b=this.jQuery||window.jQuery;f=b(window);b.fn.stick_in_parent=function(d){var A,w,J,n,B,K,p,q,k,E,t;null==d&&(d={});t=d.sticky_class;B=d.inner_scrolling;E=d.recalc_every;k=d.parent;q=d.offset_top;p=d.spacer;w=d.bottoming;null==q&&(q=0);null==k&&(k=void 0);null==B&&(B=!0);null==t&&(t="is_stuck");A=b(document);null==w&&(w=!0);J=function(a,d,n,C,F,u,r,G){var v,H,m,D,I,c,g,x,y,z,h,l;if(!a.data("sticky_kit")){a.data("sticky_kit",!0);I=A.height();g=a.parent();null!=k&&(g=g.closest(k)); 7 | if(!g.length)throw"failed to find stick parent";v=m=!1;(h=null!=p?p&&a.closest(p):b("
    "))&&h.css("position",a.css("position"));x=function(){var c,f,e;if(!G&&(I=A.height(),c=parseInt(g.css("border-top-width"),10),f=parseInt(g.css("padding-top"),10),d=parseInt(g.css("padding-bottom"),10),n=g.offset().top+c+f,C=g.height(),m&&(v=m=!1,null==p&&(a.insertAfter(h),h.detach()),a.css({position:"",top:"",width:"",bottom:""}).removeClass(t),e=!0),F=a.offset().top-(parseInt(a.css("margin-top"),10)||0)-q, 8 | u=a.outerHeight(!0),r=a.css("float"),h&&h.css({width:a.outerWidth(!0),height:u,display:a.css("display"),"vertical-align":a.css("vertical-align"),"float":r}),e))return l()};x();if(u!==C)return D=void 0,c=q,z=E,l=function(){var b,l,e,k;if(!G&&(e=!1,null!=z&&(--z,0>=z&&(z=E,x(),e=!0)),e||A.height()===I||x(),e=f.scrollTop(),null!=D&&(l=e-D),D=e,m?(w&&(k=e+u+c>C+n,v&&!k&&(v=!1,a.css({position:"fixed",bottom:"",top:c}).trigger("sticky_kit:unbottom"))),eb&&!v&&(c-=l,c=Math.max(b-u,c),c=Math.min(q,c),m&&a.css({top:c+"px"})))):e>F&&(m=!0,b={position:"fixed",top:c},b.width="border-box"===a.css("box-sizing")?a.outerWidth()+"px":a.width()+"px",a.css(b).addClass(t),null==p&&(a.after(h),"left"!==r&&"right"!==r||h.append(a)),a.trigger("sticky_kit:stick")),m&&w&&(null==k&&(k=e+u+c>C+n),!v&&k)))return v=!0,"static"===g.css("position")&&g.css({position:"relative"}), 10 | a.css({position:"absolute",bottom:d,top:"auto"}).trigger("sticky_kit:bottom")},y=function(){x();return l()},H=function(){G=!0;f.off("touchmove",l);f.off("scroll",l);f.off("resize",y);b(document.body).off("sticky_kit:recalc",y);a.off("sticky_kit:detach",H);a.removeData("sticky_kit");a.css({position:"",bottom:"",top:"",width:""});g.position("position","");if(m)return null==p&&("left"!==r&&"right"!==r||a.insertAfter(h),h.remove()),a.removeClass(t)},f.on("touchmove",l),f.on("scroll",l),f.on("resize", 11 | y),b(document.body).on("sticky_kit:recalc",y),a.on("sticky_kit:detach",H),setTimeout(l,0)}};n=0;for(K=this.length;n 2 | 3 | 5 | 8 | 12 | 13 | -------------------------------------------------------------------------------- /docs/pkgdown.js: -------------------------------------------------------------------------------- 1 | /* http://gregfranko.com/blog/jquery-best-practices/ */ 2 | (function($) { 3 | $(function() { 4 | 5 | $('.navbar-fixed-top').headroom(); 6 | 7 | $('body').css('padding-top', $('.navbar').height() + 10); 8 | $(window).resize(function(){ 9 | $('body').css('padding-top', $('.navbar').height() + 10); 10 | }); 11 | 12 | $('[data-toggle="tooltip"]').tooltip(); 13 | 14 | var cur_path = paths(location.pathname); 15 | var links = $("#navbar ul li a"); 16 | var max_length = -1; 17 | var pos = -1; 18 | for (var i = 0; i < links.length; i++) { 19 | if (links[i].getAttribute("href") === "#") 20 | continue; 21 | // Ignore external links 22 | if (links[i].host !== location.host) 23 | continue; 24 | 25 | var nav_path = paths(links[i].pathname); 26 | 27 | var length = prefix_length(nav_path, cur_path); 28 | if (length > max_length) { 29 | max_length = length; 30 | pos = i; 31 | } 32 | } 33 | 34 | // Add class to parent
  • , and enclosing
  • if in dropdown 35 | if (pos >= 0) { 36 | var menu_anchor = $(links[pos]); 37 | menu_anchor.parent().addClass("active"); 38 | menu_anchor.closest("li.dropdown").addClass("active"); 39 | } 40 | }); 41 | 42 | function paths(pathname) { 43 | var pieces = pathname.split("/"); 44 | pieces.shift(); // always starts with / 45 | 46 | var end = pieces[pieces.length - 1]; 47 | if (end === "index.html" || end === "") 48 | pieces.pop(); 49 | return(pieces); 50 | } 51 | 52 | // Returns -1 if not found 53 | function prefix_length(needle, haystack) { 54 | if (needle.length > haystack.length) 55 | return(-1); 56 | 57 | // Special case for length-0 haystack, since for loop won't run 58 | if (haystack.length === 0) { 59 | return(needle.length === 0 ? 0 : -1); 60 | } 61 | 62 | for (var i = 0; i < haystack.length; i++) { 63 | if (needle[i] != haystack[i]) 64 | return(i); 65 | } 66 | 67 | return(haystack.length); 68 | } 69 | 70 | /* Clipboard --------------------------*/ 71 | 72 | function changeTooltipMessage(element, msg) { 73 | var tooltipOriginalTitle=element.getAttribute('data-original-title'); 74 | element.setAttribute('data-original-title', msg); 75 | $(element).tooltip('show'); 76 | element.setAttribute('data-original-title', tooltipOriginalTitle); 77 | } 78 | 79 | if(ClipboardJS.isSupported()) { 80 | $(document).ready(function() { 81 | var copyButton = ""; 82 | 83 | $(".examples, div.sourceCode").addClass("hasCopyButton"); 84 | 85 | // Insert copy buttons: 86 | $(copyButton).prependTo(".hasCopyButton"); 87 | 88 | // Initialize tooltips: 89 | $('.btn-copy-ex').tooltip({container: 'body'}); 90 | 91 | // Initialize clipboard: 92 | var clipboardBtnCopies = new ClipboardJS('[data-clipboard-copy]', { 93 | text: function(trigger) { 94 | return trigger.parentNode.textContent; 95 | } 96 | }); 97 | 98 | clipboardBtnCopies.on('success', function(e) { 99 | changeTooltipMessage(e.trigger, 'Copied!'); 100 | e.clearSelection(); 101 | }); 102 | 103 | clipboardBtnCopies.on('error', function() { 104 | changeTooltipMessage(e.trigger,'Press Ctrl+C or Command+C to copy'); 105 | }); 106 | }); 107 | } 108 | })(window.jQuery || window.$) 109 | -------------------------------------------------------------------------------- /docs/pkgdown.yml: -------------------------------------------------------------------------------- 1 | pandoc: 2.7.3 2 | pkgdown: 1.5.1 3 | pkgdown_sha: ~ 4 | articles: [] 5 | last_built: 2020-08-07T18:29Z 6 | 7 | -------------------------------------------------------------------------------- /docs/reference/subset-unordered_map-vector-missing-matrix-method.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Insert parts to an object — [<-,unordered_map,vector,missing,matrix-method • datastructures 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 35 | 36 | 37 | 38 | 39 | 40 |
    41 |
    42 | 88 | 89 | 90 |
    91 | 92 |
    93 |
    94 | 97 | 98 | 99 |

    Inserts <key, value> pairs to an unordered map.

    100 | 101 | 102 |
    # S4 method for unordered_map,vector,missing,matrix
    103 | [(x, i) <- value
    104 | 105 |

    Arguments

    106 | 107 | 108 | 109 | 110 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 |
    x

    x an unorderd map object, such as a hashmap or 111 | multimap

    i

    a vector of keys

    value

    a vector of values for the keys

    122 | 123 | 124 |
    125 | 132 |
    133 | 134 |
    135 | 138 | 139 |
    140 |

    Site built with pkgdown.

    141 |
    142 | 143 |
    144 |
    145 | 146 | 147 | 148 | -------------------------------------------------------------------------------- /inst/heap/heap.aux: -------------------------------------------------------------------------------- 1 | \relax 2 | -------------------------------------------------------------------------------- /inst/heap/heap.bbl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dirmeier/datastructures/c69faa25d7eb5094e20a7410e5bcab44fed4eef9/inst/heap/heap.bbl -------------------------------------------------------------------------------- /inst/heap/heap.blg: -------------------------------------------------------------------------------- 1 | This is BibTeX, Version 0.99d (TeX Live 2015) 2 | Capacity: max_strings=35307, hash_size=35307, hash_prime=30011 3 | The top-level auxiliary file: heap.aux 4 | I found no \citation commands---while reading file heap.aux 5 | I found no \bibdata command---while reading file heap.aux 6 | I found no \bibstyle command---while reading file heap.aux 7 | You've used 0 entries, 8 | 0 wiz_defined-function locations, 9 | 83 strings with 482 characters, 10 | and the built_in function-call counts, 0 in all, are: 11 | = -- 0 12 | > -- 0 13 | < -- 0 14 | + -- 0 15 | - -- 0 16 | * -- 0 17 | := -- 0 18 | add.period$ -- 0 19 | call.type$ -- 0 20 | change.case$ -- 0 21 | chr.to.int$ -- 0 22 | cite$ -- 0 23 | duplicate$ -- 0 24 | empty$ -- 0 25 | format.name$ -- 0 26 | if$ -- 0 27 | int.to.chr$ -- 0 28 | int.to.str$ -- 0 29 | missing$ -- 0 30 | newline$ -- 0 31 | num.names$ -- 0 32 | pop$ -- 0 33 | preamble$ -- 0 34 | purify$ -- 0 35 | quote$ -- 0 36 | skip$ -- 0 37 | stack$ -- 0 38 | substring$ -- 0 39 | swap$ -- 0 40 | text.length$ -- 0 41 | text.prefix$ -- 0 42 | top$ -- 0 43 | type$ -- 0 44 | warning$ -- 0 45 | while$ -- 0 46 | width$ -- 0 47 | write$ -- 0 48 | (There were 3 error messages) 49 | -------------------------------------------------------------------------------- /inst/heap/heap.dvi: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dirmeier/datastructures/c69faa25d7eb5094e20a7410e5bcab44fed4eef9/inst/heap/heap.dvi -------------------------------------------------------------------------------- /inst/heap/heap.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dirmeier/datastructures/c69faa25d7eb5094e20a7410e5bcab44fed4eef9/inst/heap/heap.jpg -------------------------------------------------------------------------------- /inst/heap/heap.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dirmeier/datastructures/c69faa25d7eb5094e20a7410e5bcab44fed4eef9/inst/heap/heap.pdf -------------------------------------------------------------------------------- /inst/heap/heap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dirmeier/datastructures/c69faa25d7eb5094e20a7410e5bcab44fed4eef9/inst/heap/heap.png -------------------------------------------------------------------------------- /inst/heap/heap.synctex.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dirmeier/datastructures/c69faa25d7eb5094e20a7410e5bcab44fed4eef9/inst/heap/heap.synctex.gz -------------------------------------------------------------------------------- /inst/heap/heap.tex: -------------------------------------------------------------------------------- 1 | \documentclass[10.5pt,border={10pt 10pt 10pt 10pt}]{standalone} 2 | \usepackage{tikz} 3 | \usepackage{amsmath} 4 | \usetikzlibrary{shapes.misc, positioning} 5 | \usetikzlibrary{fit,positioning} 6 | 7 | \usepackage{xcolor} 8 | \definecolor{col}{RGB}{31,64,122} 9 | 10 | \begin{document} 11 | \centering 12 | \begin{tikzpicture} 13 | \tikzset{ 14 | ellipse/.style={% 15 | draw, 16 | shape=circle, 17 | line width=1mm, 18 | inner sep=0pt, 19 | node distance=2cm, 20 | minimum size=10mm} 21 | } 22 | 23 | 24 | \tikzstyle{edge_style} = [draw=black!70, line width=2, ultra thick] 25 | 26 | \node[ellipse,draw=col, fill=black!10] (a) at (0, 0) {$1$}; 27 | \node[ellipse,draw=col, below right of= a, fill=black!10] (b) {$23$}; 28 | \node[ellipse,draw=col, below left of= a, fill=black!10] (c) {$42$}; 29 | 30 | 31 | \draw[edge_style] (a) edge (b); 32 | \draw[edge_style] (c) edge (a); 33 | 34 | \end{tikzpicture} 35 | 36 | \end{document} -------------------------------------------------------------------------------- /inst/include/bimap.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * datastructures: Implementation of core datastructures for R. 3 | *

    4 | * Copyright (C) Simon Dirmeier 5 | *

    6 | * This file is part of datastructures. 7 | *

    8 | * datastructures is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | *

    13 | * datastructures is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | *

    18 | * You should have received a copy of the GNU General Public License 19 | * along with datastructures. If not, see . 20 | * 21 | */ 22 | 23 | #ifndef DS_BIMAP 24 | #define DS_BIMAP 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | template 33 | class bimap 34 | { 35 | public: 36 | using position = typename boost::bimap::value_type; 37 | using ri = typename boost::bimap::right_map::const_iterator; 38 | using li = typename boost::bimap::left_map::const_iterator; 39 | 40 | bimap() : map_() 41 | { 42 | } 43 | 44 | size_t size() 45 | { 46 | return map_.size(); 47 | } 48 | 49 | void insert(std::vector& t, std::vector& u) 50 | { 51 | if (t.size() != u.size()) 52 | { 53 | Rcpp::stop("left.size() != right.size()"); 54 | } 55 | for (typename std::vector::size_type i = 0; i < t.size(); ++i) 56 | { 57 | map_.insert(position(t[i], u[i])); 58 | } 59 | } 60 | 61 | void clear() 62 | { 63 | map_.clear(); 64 | } 65 | 66 | void remove(std::vector& t) 67 | { 68 | for (typename std::vector::size_type i = 0; i < t.size(); ++i) 69 | { 70 | map_.left.erase(t[i]); 71 | } 72 | } 73 | 74 | void remove_value(std::vector& u) 75 | { 76 | for (typename std::vector::size_type i = 0; i < u.size(); ++i) 77 | { 78 | map_.right.erase(u[i]); 79 | } 80 | } 81 | 82 | std::vector lefts() 83 | { 84 | std::vector lefts; 85 | lefts.reserve(map_.size()); 86 | for (li left_iter = map_.left.begin(), iend = map_.left.end(); 87 | left_iter != iend; ++left_iter) 88 | { 89 | lefts.push_back(left_iter->first); 90 | } 91 | 92 | return lefts; 93 | } 94 | 95 | std::vector rights() 96 | { 97 | std::vector rights; 98 | rights.reserve(map_.size()); 99 | for (ri right_iter = map_.right.begin(), iend = map_.right.end(); 100 | right_iter != iend; ++right_iter) 101 | { 102 | rights.push_back(right_iter->first); 103 | } 104 | 105 | return rights; 106 | } 107 | 108 | Rcpp::List head() 109 | { 110 | unsigned int i = 0; 111 | std::map heads; 112 | for (li left_iter = map_.left.begin(), iend = map_.left.end(); 113 | left_iter != iend; ++left_iter) 114 | { 115 | if (i++ == 5) 116 | break; 117 | heads.insert(std::pair(left_iter->first, left_iter->second)); 118 | } 119 | 120 | return Rcpp::wrap(heads); 121 | } 122 | 123 | std::vector get_right(std::vector& t) 124 | { 125 | std::vector values(t.size()); 126 | 127 | for (typename std::vector::size_type i = 0; i < t.size(); ++i) 128 | { 129 | T key = t[i]; 130 | if (map_.left.find(key) != map_.left.end()) 131 | { 132 | values[i] = map_.left.at(key); 133 | } 134 | else 135 | { 136 | std::ostringstream ss; 137 | ss << key; 138 | Rcpp::stop( 139 | std::string("Could not find key: ").append(ss.str())); 140 | } 141 | } 142 | 143 | return values; 144 | } 145 | 146 | std::vector get_left(std::vector& u) 147 | { 148 | std::vector values(u.size()); 149 | 150 | for (typename std::vector::size_type i = 0; i < u.size(); ++i) 151 | { 152 | U key = u[i]; 153 | if (map_.right.find(key) != map_.right.end()) 154 | { 155 | values[i] = map_.right.at(key); 156 | } 157 | else 158 | { 159 | std::ostringstream ss; 160 | ss << key; 161 | Rcpp::stop( 162 | std::string("Could not find key: ").append(ss.str())); 163 | } 164 | } 165 | 166 | return values; 167 | } 168 | 169 | private: 170 | boost::bimap map_; 171 | }; 172 | 173 | using bimap_ss = bimap; 174 | using bimap_si = bimap; 175 | using bimap_sb = bimap; 176 | using bimap_sd = bimap; 177 | 178 | using bimap_ds = bimap; 179 | using bimap_di = bimap; 180 | using bimap_db = bimap; 181 | using bimap_dd = bimap; 182 | 183 | using bimap_is = bimap; 184 | using bimap_ii = bimap; 185 | using bimap_ib = bimap; 186 | using bimap_id = bimap; 187 | 188 | #endif 189 | -------------------------------------------------------------------------------- /inst/include/binomial_heap.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * datastructures: Implementation of core datastructures for R. 3 | *

    4 | * Copyright (C) Simon Dirmeier 5 | *

    6 | * This file is part of datastructures. 7 | *

    8 | * datastructures is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | *

    13 | * datastructures is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | *

    18 | * You should have received a copy of the GNU General Public License 19 | * along with datastructures. If not, see . 20 | * 21 | */ 22 | 23 | #ifndef DS_BINOMIALHEAP 24 | #define DS_BINOMIALHEAP 25 | 26 | #include 27 | #include "heap.hpp" 28 | 29 | using binomial_heap_s =heap; 30 | using binomial_heap_d = heap; 31 | using binomial_heap_i = heap; 32 | 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /inst/include/fibonacci_heap.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * datastructures: Implementation of core datastructures for R. 3 | *

    4 | * Copyright (C) Simon Dirmeier 5 | *

    6 | * This file is part of datastructures. 7 | *

    8 | * datastructures is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | *

    13 | * datastructures is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | *

    18 | * You should have received a copy of the GNU General Public License 19 | * along with datastructures. If not, see . 20 | * 21 | */ 22 | 23 | #ifndef DS_FIBONACCIHEAP 24 | #define DS_FIBONACCIHEAP 25 | 26 | #include 27 | #include "heap.hpp" 28 | 29 | using fibonacci_heap_s =heap; 30 | using fibonacci_heap_d = heap; 31 | using fibonacci_heap_i = heap; 32 | 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /inst/include/hashmap.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * datastructures: Implementation of core datastructures for R. 3 | *

    4 | * Copyright (C) Simon Dirmeier 5 | *

    6 | * This file is part of datastructures. 7 | *

    8 | * datastructures is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | *

    13 | * datastructures is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | *

    18 | * You should have received a copy of the GNU General Public License 19 | * along with datastructures. If not, see . 20 | * 21 | */ 22 | 23 | 24 | #ifndef DS_HASHMAP 25 | #define DS_HASHMAP 26 | 27 | 28 | #include 29 | #include 30 | #include "map.hpp" 31 | #include 32 | 33 | 34 | using hashmap_s = datastructures::map< 35 | std::unordered_map, std::string>; 36 | using hashmap_d = datastructures::map< 37 | std::unordered_map, double>; 38 | using hashmap_i = datastructures::map< 39 | std::unordered_map, int>; 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /inst/include/map.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * datastructures: Implementation of core datastructures for R. 3 | *

    4 | * Copyright (C) Simon Dirmeier 5 | *

    6 | * This file is part of datastructures. 7 | *

    8 | * datastructures is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | *

    13 | * datastructures is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | *

    18 | * You should have received a copy of the GNU General Public License 19 | * along with datastructures. If not, see . 20 | * 21 | */ 22 | 23 | 24 | #ifndef DS_MAP 25 | #define DS_MAP 26 | 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | 34 | namespace datastructures 35 | { 36 | template class H, typename T> 37 | class map 38 | { 39 | public: 40 | map() = default; 41 | 42 | size_t size() 43 | { 44 | return map_.size(); 45 | } 46 | 47 | void insert(std::vector& t, Rcpp::RObject u) 48 | { 49 | if(!Rf_isNewList(u)) 50 | { 51 | Rcpp::stop( 52 | "Rcpp::RObject needs to be a NewList\n"); 53 | } 54 | 55 | const unsigned int sexp_size = static_cast(Rf_length(u)); 56 | if (t.size() != sexp_size) 57 | { 58 | Rcpp::stop("keys.size() != values.size()"); 59 | } 60 | for (typename std::vector::size_type i = 0; i < t.size(); ++i) 61 | { 62 | Rcpp::RObject s = Rf_duplicate(VECTOR_ELT(u, i)); 63 | map_.insert(std::pair(t[i], s)); 64 | } 65 | } 66 | 67 | void clear() 68 | { 69 | for (auto it = map_.begin(); it != map_.end(); ++it) 70 | R_ReleaseObject(it->second); 71 | map_.clear(); 72 | } 73 | 74 | void remove_with_value(std::vector& t, Rcpp::RObject u) 75 | { 76 | for (typename std::vector::size_type i = 0; i < t.size(); ++i) 77 | { 78 | auto iter = map_.equal_range(t[i]); 79 | for (auto it = iter.first; it != iter.second; ++it) 80 | { 81 | if (R_compute_identical(VECTOR_ELT(u, i), it->second, 0)) 82 | { 83 | map_.erase(it); 84 | break; 85 | } 86 | } 87 | } 88 | } 89 | 90 | void remove(std::vector& t) 91 | { 92 | for (typename std::vector::size_type i = 0; i < t.size(); ++i) 93 | map_.erase(t[i]); 94 | } 95 | 96 | bool is_empty() 97 | { 98 | return map_.empty(); 99 | } 100 | 101 | std::vector keys() 102 | { 103 | std::vector keys; 104 | keys.reserve(map_.size()); 105 | for (const auto& pair : map_) 106 | { 107 | keys.push_back(pair.first); 108 | } 109 | 110 | return keys; 111 | } 112 | 113 | Rcpp::List values() 114 | { 115 | std::vector< Rcpp::RObject > values; 116 | values.reserve(map_.size()); 117 | 118 | int prt = 0; 119 | for (const auto& pair : map_) 120 | { 121 | Rcpp::RObject s = PROTECT(pair.second); 122 | values.push_back(s); 123 | ++prt; 124 | } 125 | UNPROTECT(prt); 126 | 127 | return Rcpp::wrap(values); 128 | } 129 | 130 | Rcpp::List head() 131 | { 132 | unsigned int i = 0; 133 | std::map heads; 134 | for (const auto& pair : map_) 135 | { 136 | if (i++ == 5) break; 137 | heads.insert(pair); 138 | } 139 | 140 | return Rcpp::wrap(heads); 141 | } 142 | 143 | Rcpp::List get(std::vector& t) 144 | { 145 | std::vector< Rcpp::RObject > values; 146 | int prt = 0; 147 | 148 | for (typename std::vector::size_type i = 0; i < t.size(); ++i) 149 | { 150 | T key = t[i]; 151 | if (map_.find(key) != map_.end()) 152 | { 153 | auto range = map_.equal_range(key); 154 | for (auto it = range.first; it != range.second; ++it) 155 | { 156 | Rcpp::RObject s = PROTECT(it->second); 157 | ++prt; 158 | values.push_back(s); 159 | } 160 | } 161 | else 162 | { 163 | std::ostringstream ss; 164 | ss << key; 165 | UNPROTECT(prt); 166 | Rcpp::stop( 167 | std::string("Could not find key: ").append(ss.str())); 168 | } 169 | } 170 | UNPROTECT(prt); 171 | 172 | return Rcpp::wrap(values); 173 | } 174 | 175 | private: 176 | H map_; 177 | }; 178 | } 179 | #endif 180 | -------------------------------------------------------------------------------- /inst/include/multimap.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * datastructures: Implementation of core datastructures for R. 3 | *

    4 | * Copyright (C) Simon Dirmeier 5 | *

    6 | * This file is part of datastructures. 7 | *

    8 | * datastructures is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | *

    13 | * datastructures is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | *

    18 | * You should have received a copy of the GNU General Public License 19 | * along with datastructures. If not, see . 20 | * 21 | */ 22 | 23 | 24 | #ifndef DS_MULTIMAP 25 | #define DS_MULTIMAP 26 | 27 | 28 | #include 29 | #include 30 | #include "map.hpp" 31 | #include 32 | 33 | 34 | using multimap_d = datastructures::map< 35 | std::unordered_multimap, double>; 36 | using multimap_i = datastructures::map< 37 | std::unordered_multimap, int>; 38 | using multimap_s = datastructures::map< 39 | std::unordered_multimap, std::string>; 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /inst/include/queue.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * datastructures: Implementation of core datastructures for R. 3 | *

    4 | * Copyright (C) Simon Dirmeier 5 | *

    6 | * This file is part of datastructures. 7 | *

    8 | * datastructures is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | *

    13 | * datastructures is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | *

    18 | * You should have received a copy of the GNU General Public License 19 | * along with datastructures. If not, see . 20 | * 21 | */ 22 | 23 | 24 | #ifndef DS_QUEUE 25 | #define DS_QUEUE 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | 32 | class queue 33 | { 34 | public: 35 | queue(): queue_() 36 | {} 37 | 38 | size_t size() 39 | { 40 | return queue_.size(); 41 | } 42 | 43 | void insert(Rcpp::RObject t) 44 | { 45 | if(!Rf_isNewList(t)) 46 | Rcpp::stop("Rcpp::RObject needs to be a NewList\n"); 47 | for (int i = 0; i < Rf_length(t); ++i) 48 | queue_.push(VECTOR_ELT(t, i)); 49 | } 50 | 51 | void clear() 52 | { 53 | std::queue().swap(queue_); 54 | } 55 | 56 | Rcpp::RObject peek() 57 | { 58 | return queue_.front(); 59 | } 60 | 61 | Rcpp::RObject pop() 62 | { 63 | Rcpp::RObject t = peek(); 64 | queue_.pop(); 65 | return t; 66 | } 67 | 68 | private: 69 | std::queue< Rcpp::RObject > queue_; 70 | }; 71 | 72 | using queue_sexp = queue; 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /inst/include/stack.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * datastructures: Implementation of core datastructures for R. 3 | *

    4 | * Copyright (C) Simon Dirmeier 5 | *

    6 | * This file is part of datastructures. 7 | *

    8 | * datastructures is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | *

    13 | * datastructures is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | *

    18 | * You should have received a copy of the GNU General Public License 19 | * along with datastructures. If not, see . 20 | * 21 | */ 22 | 23 | 24 | #ifndef DS_STACK 25 | #define DS_STACK 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | 32 | class stack 33 | { 34 | public: 35 | stack(): stack_() 36 | {} 37 | 38 | size_t size() 39 | { 40 | return stack_.size(); 41 | } 42 | 43 | void insert(Rcpp::RObject t) 44 | { 45 | if(!Rf_isNewList(t)) 46 | Rcpp::stop("Rcpp::Robject needs to be a NewList\n"); 47 | for (int i = 0; i < Rf_length(t); ++i) 48 | stack_.push(VECTOR_ELT(t, i)); 49 | } 50 | 51 | Rcpp::RObject peek() 52 | { 53 | return stack_.top(); 54 | } 55 | 56 | void clear() 57 | { 58 | std::stack().swap(stack_); 59 | } 60 | 61 | Rcpp::RObject pop() 62 | { 63 | Rcpp::RObject t = peek(); 64 | stack_.pop(); 65 | return t; 66 | } 67 | 68 | private: 69 | std::stack< Rcpp::RObject > stack_; 70 | }; 71 | 72 | using stack_sexp = stack; 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /joss/paper.bib: -------------------------------------------------------------------------------- 1 | @Manual{rlang, 2 | title = {R: A Language and Environment for Statistical Computing}, 3 | author = {{R Core Team}}, 4 | organization = {R Foundation for Statistical Computing}, 5 | address = {Vienna, Austria}, 6 | year = {2018}, 7 | url = {https://www.R-project.org/}, 8 | } 9 | 10 | @Article{rcpp, 11 | title = {{Rcpp}: Seamless {R} and {C++} Integration}, 12 | author = {Dirk Eddelbuettel and Romain Fran\c{c}ois}, 13 | journal = {Journal of Statistical Software}, 14 | year = {2011}, 15 | volume = {40}, 16 | number = {8}, 17 | pages = {1--18}, 18 | url = {http://www.jstatsoft.org/v40/i08/}, 19 | doi = {10.18637/jss.v040.i08}, 20 | } 21 | 22 | @Manual{dequerpackage, 23 | title = {{dequer}: Stacks, Queues, and Deques for R}, 24 | author = {Drew Schmidt}, 25 | year = {2017}, 26 | note = {{R} package version 2.0-1}, 27 | url = {https://cran.r-project.org/package=dequer}, 28 | } 29 | 30 | @Manual{hashmappackage, 31 | title = {hashmap: The Faster Hash Map}, 32 | author = {Nathan Russell}, 33 | year = {2017}, 34 | note = {R package version 0.2.2}, 35 | url = {https://CRAN.R-project.org/package=hashmap}, 36 | } 37 | -------------------------------------------------------------------------------- /joss/paper.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: '`datastructures`: An R package for organisation and storage of data' 3 | tags: 4 | - R 5 | - datastructures 6 | - Boost 7 | authors: 8 | - name: Simon Dirmeier 9 | orcid: 0000-0001-9795-3550 10 | affiliation: "1" 11 | affiliations: 12 | - name: Department of Biosystems Science and Engineering, ETH Zurich 13 | index: 1 14 | date: 20 August 2018 15 | bibliography: paper.bib 16 | --- 17 | 18 | # Summary 19 | 20 | In computer science, data structures are objects for organisation and storage of data. They are often a key for efficient computation. So far the statistical computing language R [@rlang] only supports simple data structures such as lists, matrices and data tables, whereas other data structures such as priority queues, stacks or hashmaps are not supported. These data structures are essential in many computer science and statistics problems, like graph algorithms, string analysis or scheduling. Single packages, for example `hashmap` [@hashmappackage] or `dequer` [@dequerpackage], have already been proposed, although none of them support a wider range of data structures with a unified interface. 21 | 22 | In order to access a richer repertoire of data types, and thus enabling efficient computation of various problems, we propose the R package [datastructures](https://github.com/dirmeier/datastructures). The package uses Boost and STL data types in a C++ backend and exports these to R using the Rcpp [@rcpp] module system. 23 | 24 | So far `datastructures` supports three different groups of data types. **Heaps** Fibonacci and binomial heaps can be used for priority queue operations, such as in (a version of) Dijkstra's algorithm for finding shortest paths. **Maps** Hash-, bi- and multimaps are associative arrays that establish key-value relationships. 25 | **Deques** Stacks and queues are linked lists supporting the LIFO or FIFO principle, respectively. 26 | 27 | We used an object-oriented approach where every data structure is exported using `S4` classes. For easier maintainance and extensibility we heavily rely ond polymorphism and inheritance of the implemented class system. 28 | 29 | The package `datastructures` aims to bridge the gap between statistical and *classical* programming languages, where usage of these data structures are fairly common. In addition, in the near future we will add support of formats such as *suffix arrays* and *suffix trees*, *red-black*- and *B*-trees. 30 | 31 | # Examples 32 | 33 | The following example shows a use case of a `hashmap` for an artificial data set. 34 | 35 | ```{r} 36 | > library(datastructures) 37 | 38 | 39 | > hm <- hashmap("integer") 40 | > keys <- 1:2 41 | > values <- list( 42 | environment(), 43 | data.frame(A=rbeta(3, .5, .5), B=rgamma(3, 1))) 44 | > hm[keys] <- values 45 | 46 | > hm[2L] 47 | [[1]] 48 | A B 49 | 1 0.5649580 1.8091666 50 | 2 0.2313472 0.4522518 51 | 3 0.8533336 3.8463516 52 | ``` 53 | 54 | # References 55 | -------------------------------------------------------------------------------- /man/at-methods.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods_at.R, R/ds_map_bimap.R, 3 | % R/ds_map_unordered.R 4 | \docType{methods} 5 | \name{at} 6 | \alias{at} 7 | \alias{at,bimap,vector,character-method} 8 | \alias{at,bimap,vector,missing-method} 9 | \alias{at,unordered_map,vector,missing-method} 10 | \title{Access elements from an object} 11 | \usage{ 12 | at(obj, x, which = c("values", "keys"), ...) 13 | 14 | \S4method{at}{bimap,vector,character}(obj, x, which = c("values", "keys")) 15 | 16 | \S4method{at}{bimap,vector,missing}(obj, x) 17 | 18 | \S4method{at}{unordered_map,vector,missing}(obj, x) 19 | } 20 | \arguments{ 21 | \item{obj}{object to extract values from} 22 | 23 | \item{x}{the set of keys to match the values} 24 | 25 | \item{which}{choose either \code{values} if the values should get returned} 26 | 27 | \item{...}{other arguments 28 | or \code{keys} if the keys should get returned} 29 | } 30 | \value{ 31 | returns extracted keys or values from \code{obj} 32 | } 33 | \description{ 34 | Extracts a set of pairs. For \code{hashmaps} 35 | mappings from 36 | \deqn{f: keys -> values,} 37 | exist so argument \code{which} is per default \code{values} (since these 38 | are going to be retrieved). For \code{bimaps} also 39 | \deqn{f: values -> keys,} 40 | mappings exist, such that \code{which} can also be \code{keys} if the keys 41 | from the object should be retrieved. 42 | } 43 | \details{ 44 | # datastructures: Implementation of core datastructures for R. 45 | # 46 | # Copyright (C) Simon Dirmeier 47 | # 48 | # This file is part of datastructures. 49 | # 50 | # datastructures is free software: you can redistribute it and/or modify 51 | # it under the terms of the GNU General Public License as published by 52 | # the Free Software Foundation, either version 3 of the License, or 53 | # (at your option) any later version. 54 | # 55 | # datastructures is distributed in the hope that it will be useful, 56 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 57 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 58 | # GNU General Public License for more details. 59 | # 60 | # You should have received a copy of the GNU General Public License 61 | # along with datastructures. If not, see . 62 | } 63 | \examples{ 64 | 65 | # access values from a hashmap 66 | h_map <- hashmap("integer") 67 | h_map[seq(2)] <- list(data.frame(a=rexp(3), b=rnorm(3)), environment()) 68 | h_map[1L] 69 | 70 | # access values or keys from a bimap 71 | b_map <- bimap("integer", "character") 72 | b_map[seq(5)] <- letters[seq(5)] 73 | at(b_map, c(1L, 3L)) 74 | at(b_map, c(1L, 3L), which="values") 75 | at(b_map, c("a", "c"), which="keys") 76 | 77 | # access values from a multimap 78 | m_map <- multimap("integer") 79 | m_map[c(seq(5), seq(5))] <- letters[seq(10)] 80 | at(m_map, 1L) 81 | 82 | } 83 | -------------------------------------------------------------------------------- /man/bimap-class.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ds_map_bimap.R 3 | \docType{class} 4 | \name{bimap-class} 5 | \alias{bimap-class} 6 | \title{Bimap class} 7 | \description{ 8 | Implementation of a bimap data structure, i.e. an unordered 9 | collection of key-value pairs. The notable difference to 10 | \code{\linkS4class{hashmap}} is that the mapping is not only 11 | \deqn{f: keys -> values,} 12 | but also 13 | \deqn{f: values -> keys.} 14 | Inserting and accessing is amortized in \emph{O(1)}. 15 | \code{bimap} wraps a \code{boost::bimap} using Rcpp modules. 16 | } 17 | \section{Slots}{ 18 | 19 | \describe{ 20 | \item{\code{.map}}{\code{C++} object representing a mapping} 21 | 22 | \item{\code{.key.class}}{the class of the keys} 23 | 24 | \item{\code{.value.class}}{the class of the values} 25 | }} 26 | 27 | \seealso{ 28 | \code{\link{bimap}} for creating a new \code{bimap} object 29 | } 30 | -------------------------------------------------------------------------------- /man/bimap.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ds_map_bimap.R 3 | \name{bimap} 4 | \alias{bimap} 5 | \title{Create a new \code{bimap}} 6 | \usage{ 7 | bimap(key.class = c("character", "numeric", "integer"), 8 | value.class = c("character", "numeric", "integer")) 9 | } 10 | \arguments{ 11 | \item{key.class}{the primitive class type of the keys} 12 | 13 | \item{value.class}{the primitive class type of the values} 14 | } 15 | \value{ 16 | returns a new \code{bimap} object 17 | } 18 | \description{ 19 | Instantiates a new \code{\linkS4class{bimap}} object, 20 | i.e. an unordered collection of key-value pairs with mappings 21 | \deqn{f: keys -> values,} 22 | and 23 | \deqn{f: values -> keys.} 24 | } 25 | \examples{ 26 | # create a bimap with character <-> character bi-mapping 27 | b <- bimap() 28 | 29 | # create a bimap with character <-> integer bi-mapping 30 | b <- bimap("character", "integer") 31 | 32 | # create a bimap with integer <-> integer bi-mapping 33 | b <- bimap("integer", "numeric") 34 | 35 | } 36 | -------------------------------------------------------------------------------- /man/binomial_heap-class.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ds_heap_binomial.R 3 | \docType{class} 4 | \name{binomial_heap-class} 5 | \alias{binomial_heap-class} 6 | \title{Binomial heap class} 7 | \description{ 8 | Implementation of a binomial heap data structure, i.e. a 9 | priority datastructure with \code{push} and \code{pop} in amortized 10 | \emph{O(log n)}. \code{binomial_heap} wraps a \code{boost::binomial_heap} 11 | using Rcpp modules. The heap consists of nodes with 12 | keys and values where the key determines the priority in the heap. 13 | Also see the \code{\linkS4class{binomial_heap}} class. 14 | } 15 | \section{Slots}{ 16 | 17 | \describe{ 18 | \item{\code{.heap}}{\code{C++} object representing a heap} 19 | 20 | \item{\code{.key.class}}{the class of the keys} 21 | }} 22 | 23 | \seealso{ 24 | \code{\link{binomial_heap}} for creating a new \code{binomial_heap} object 25 | } 26 | -------------------------------------------------------------------------------- /man/binomial_heap.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ds_heap_binomial.R 3 | \name{binomial_heap} 4 | \alias{binomial_heap} 5 | \title{Create a new \code{binomial_heap}} 6 | \usage{ 7 | binomial_heap(key.class = c("character", "numeric", "integer")) 8 | } 9 | \arguments{ 10 | \item{key.class}{the primitive class type of the keys} 11 | } 12 | \value{ 13 | returns a new \code{binomial_heap} object 14 | } 15 | \description{ 16 | Instantiates a new \code{\linkS4class{binomial_heap}} object, 17 | i.e. a tree-like data structure satisfying the \emph{min-heap} property. 18 | } 19 | \examples{ 20 | # creates a binomial_heap 21 | b_heap <- binomial_heap() 22 | 23 | # creates a binomial_heap 24 | b_heap <- binomial_heap("numeric") 25 | 26 | # creates a binomial_heap 27 | b_heap <- binomial_heap("character") 28 | 29 | } 30 | -------------------------------------------------------------------------------- /man/clear-methods.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods_clear.R, R/ds_deque.R, R/ds_heap.R, 3 | % R/ds_map.R 4 | \docType{methods} 5 | \name{clear} 6 | \alias{clear} 7 | \alias{clear,deque-method} 8 | \alias{clear,heap-method} 9 | \alias{clear,map-method} 10 | \title{Remove all elements from a datastructure} 11 | \usage{ 12 | clear(obj) 13 | 14 | \S4method{clear}{deque}(obj) 15 | 16 | \S4method{clear}{heap}(obj) 17 | 18 | \S4method{clear}{map}(obj) 19 | } 20 | \arguments{ 21 | \item{obj}{the object to clear} 22 | } 23 | \description{ 24 | Removes every element that is stored in a data structure and 25 | resets everything. 26 | } 27 | \examples{ 28 | 29 | # clears a multimap 30 | m_map <- multimap() 31 | m_map <- insert(m_map, c("a", "b"), 1:2) 32 | m_map <- insert(m_map, c("a", "b"), list(1, list(a=1))) 33 | m_map <- clear(m_map) 34 | 35 | 36 | # clears a heap 37 | f_heap <- fibonacci_heap("integer") 38 | f_heap <- insert(f_heap, 1:2, 1:2) 39 | f_heap[3:4] <- list(1, list(a=1)) 40 | f_heap <- clear(f_heap) 41 | 42 | # clears a \\code{deque} 43 | s <- stack() 44 | s <- insert(s, list(1, vector(), list(3), data.frame(rnorm(3)))) 45 | s <- clear(s) 46 | 47 | } 48 | -------------------------------------------------------------------------------- /man/datastructures-package.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/datastructures-package.R 3 | \docType{package} 4 | \name{datastructures-package} 5 | \alias{datastructures-package} 6 | \title{datastructures} 7 | \description{ 8 | Implementation of advanced data structures such as hashmaps, 9 | heaps, or queues. Advanced data structures are essential 10 | in many computer science and statistics problems, for example graph 11 | algorithms or string analysis. The package uses 'Boost' and 'STL' data 12 | types and extends these to R with 'Rcpp' modules. 13 | } 14 | \author{ 15 | Simon Dirmeier 16 | } 17 | \keyword{package} 18 | -------------------------------------------------------------------------------- /man/decrease_key-methods.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods_decrease.R, R/ds_heap.R 3 | \docType{methods} 4 | \name{decrease_key} 5 | \alias{decrease_key} 6 | \alias{decrease_key,heap,vector,vector,character-method} 7 | \alias{decrease_key,heap,vector,vector,missing-method} 8 | \title{Decreases the key of a node in a heap} 9 | \usage{ 10 | decrease_key(obj, from, to, handle) 11 | 12 | \S4method{decrease_key}{heap,vector,vector,character}(obj, from, to, handle) 13 | 14 | \S4method{decrease_key}{heap,vector,vector,missing}(obj, from, to) 15 | } 16 | \arguments{ 17 | \item{obj}{a heap object} 18 | 19 | \item{from}{a key in the heap for which the node should be decreased} 20 | 21 | \item{to}{the new value of the heap} 22 | 23 | \item{handle}{the handle of the specific node that is decreased} 24 | } 25 | \value{ 26 | returns extracted handles and values from \code{obj} 27 | } 28 | \description{ 29 | Decreases the key of a node in a heap and updates the complete 30 | heap. The key is decreases \code{from} a value \code{to} a value by that moving the 31 | node's position in the heap. If a node cannot uniquely be identified using 32 | the \code{to} key, a \code{\link{handle}} needs to be given in addition. 33 | } 34 | \examples{ 35 | 36 | # decreases the key of a heap 37 | f_heap <- fibonacci_heap("integer") 38 | f_heap <- insert(f_heap, 1:5, letters[1:5]) 39 | peek(f_heap) 40 | 41 | decrease_key(f_heap, 5L, -1L) 42 | peek(f_heap) 43 | 44 | hand <- handle(f_heap, value=letters[3]) 45 | decrease_key(f_heap, hand[[1]]$key, -2L) 46 | peek(f_heap) 47 | 48 | } 49 | -------------------------------------------------------------------------------- /man/deque-class.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ds_deque.R 3 | \docType{class} 4 | \name{deque-class} 5 | \alias{deque-class} 6 | \title{Deque class} 7 | \description{ 8 | Abstract deque class 9 | } 10 | \section{Slots}{ 11 | 12 | \describe{ 13 | \item{\code{.deque}}{\code{C++} object representing a deque} 14 | }} 15 | 16 | -------------------------------------------------------------------------------- /man/erase-methods.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods_erase.R, R/ds_map.R, R/ds_map_bimap.R, 3 | % R/ds_map_multimap.R 4 | \docType{methods} 5 | \name{erase} 6 | \alias{erase} 7 | \alias{erase,map,vector,missing-method} 8 | \alias{erase,bimap,missing,vector-method} 9 | \alias{erase,multimap,vector,vector-method} 10 | \alias{erase,multimap,vector,list-method} 11 | \alias{erase,multimap,vector,ANY-method} 12 | \title{Erase an entry from a map} 13 | \usage{ 14 | erase(obj, key, value) 15 | 16 | \S4method{erase}{map,vector,missing}(obj, key) 17 | 18 | \S4method{erase}{bimap,missing,vector}(obj, value) 19 | 20 | \S4method{erase}{multimap,vector,vector}(obj, key, value) 21 | 22 | \S4method{erase}{multimap,vector,list}(obj, key, value) 23 | 24 | \S4method{erase}{multimap,vector,ANY}(obj, key, value) 25 | } 26 | \arguments{ 27 | \item{obj}{the object to pop an element from} 28 | 29 | \item{key}{a vector of keys that should be removed} 30 | 31 | \item{value}{optionally a list of values needs to be supplied for some data 32 | structures such as \code{multimap}s if a single key-value pair should removed. If 33 | not provided removes all key-value pairs with a specific key.} 34 | } 35 | \value{ 36 | returns \code{obj} with removed values 37 | } 38 | \description{ 39 | Erase a vector of key-value pair from a \code{map} object. 40 | } 41 | \examples{ 42 | 43 | # erases keys from a hashmap or bimap 44 | h_map <- hashmap() 45 | h_map[letters] <- rnorm(length(letters)) 46 | h_map <- erase(h_map, "a") 47 | h_map <- erase(h_map, letters[2:5]) 48 | 49 | # erases keys from a multimap 50 | m_map <- multimap() 51 | m_map[c("a", "a", "a", "b", "b", "c")] <- rep(1:2, 3) 52 | m_map <- erase(m_map, "a") 53 | m_map <- erase(m_map, "b", 1) 54 | 55 | } 56 | -------------------------------------------------------------------------------- /man/fibonacci_heap-class.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ds_heap_fibonacci.R 3 | \docType{class} 4 | \name{fibonacci_heap-class} 5 | \alias{fibonacci_heap-class} 6 | \title{Fibonacci heap class} 7 | \description{ 8 | Implementation of a Fibonacci heap data structure, i.e. a 9 | priority datastructure with \code{push} in amortized \emph{O(1)} and 10 | \code{pop} in \emph{O(log n)}. \code{fibonacci_heap} wraps a 11 | \code{boost::fibonacci_heap} using Rcpp modules. The heap consists of 12 | nodes with keys and values where the key determines the priority in the 13 | heap. Also see the \code{\linkS4class{binomial_heap}} class. 14 | } 15 | \section{Slots}{ 16 | 17 | \describe{ 18 | \item{\code{.heap}}{\code{C++} object representing a heap} 19 | 20 | \item{\code{.key.class}}{the class of the keys} 21 | }} 22 | 23 | \seealso{ 24 | \code{\link{fibonacci_heap}} for creating a new \code{fibonacci_heap} object 25 | } 26 | -------------------------------------------------------------------------------- /man/fibonacci_heap.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ds_heap_fibonacci.R 3 | \name{fibonacci_heap} 4 | \alias{fibonacci_heap} 5 | \title{Create a new \code{fibonacci_heap}} 6 | \usage{ 7 | fibonacci_heap(key.class = c("character", "numeric", "integer")) 8 | } 9 | \arguments{ 10 | \item{key.class}{the primitive class type of the keys} 11 | } 12 | \value{ 13 | returns a new \code{fibonacci_heap} object 14 | } 15 | \description{ 16 | Instantiates a new \code{\linkS4class{fibonacci_heap}} object, 17 | i.e. a tree-like data structure satisfying the \emph{min-heap} property. 18 | } 19 | \examples{ 20 | # creates a fibonacci_heap 21 | f_heap <- fibonacci_heap() 22 | 23 | # creates a fibonacci_heap 24 | f_heap <- fibonacci_heap("numeric") 25 | 26 | # creates a fibonacci_heap 27 | f_heap <- fibonacci_heap("character") 28 | 29 | } 30 | -------------------------------------------------------------------------------- /man/handle-methods.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods_handle.R, R/ds_heap.R 3 | \docType{methods} 4 | \name{handle} 5 | \alias{handle} 6 | \alias{handle,heap,vector,missing-method} 7 | \alias{handle,heap,missing,list-method} 8 | \alias{handle,heap,missing,vector-method} 9 | \alias{handle,heap,missing,matrix-method} 10 | \title{Get the handles and values for nodes of a specific key in a heap.} 11 | \usage{ 12 | handle(obj, key, value) 13 | 14 | \S4method{handle}{heap,vector,missing}(obj, key) 15 | 16 | \S4method{handle}{heap,missing,list}(obj, value) 17 | 18 | \S4method{handle}{heap,missing,vector}(obj, value) 19 | 20 | \S4method{handle}{heap,missing,matrix}(obj, value) 21 | } 22 | \arguments{ 23 | \item{obj}{a heap object} 24 | 25 | \item{key}{a key in the heap} 26 | 27 | \item{value}{a value in the heap} 28 | } 29 | \value{ 30 | returns extracted handles and values from \code{obj} 31 | } 32 | \description{ 33 | Returns a list of handles and values for node elements that have 34 | a specific key. That means for a given key, the reference to the node 35 | (the handle) as well as the value of the node are returned. If one key fits 36 | fits multiple nodes, all of the values and handles are returned. This is 37 | needed in order to uniquely identify a node if, for example, 38 | \code{decrease_key} on a specific node is going to be called. 39 | } 40 | \examples{ 41 | # returns the handle of a heap 42 | f_heap <- fibonacci_heap("integer") 43 | f_heap <- insert(f_heap, 1:5, letters[1:5]) 44 | 45 | handle(f_heap, key=3L) 46 | 47 | handle(f_heap, value=letters[3]) 48 | 49 | } 50 | -------------------------------------------------------------------------------- /man/hashmap-class.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ds_map_hashmap.R 3 | \docType{class} 4 | \name{hashmap-class} 5 | \alias{hashmap-class} 6 | \title{Hashmap class} 7 | \description{ 8 | Implementation of a hashmap data structure, i.e. an unordered 9 | collection of key-value pairs: 10 | \deqn{f: keys -> values.} 11 | Hashmaps only to store unique keys-value pairs. For a data structure 12 | where multiple identical keys can be stores see \code{\link{multimap}}. 13 | Inserting and accessing is amortized in \emph{O(1)}. 14 | \code{hashmap} wraps a C++ \code{unordered_map} using Rcpp modules. 15 | Also see \code{\linkS4class{bimap}} for mappings in both ways. 16 | } 17 | \section{Slots}{ 18 | 19 | \describe{ 20 | \item{\code{.map}}{\code{C++} object representing a mapping} 21 | 22 | \item{\code{.key.class}}{the class of the keys} 23 | }} 24 | 25 | \seealso{ 26 | \code{\link{hashmap}} for creating a new \code{hashmap} object 27 | } 28 | -------------------------------------------------------------------------------- /man/hashmap.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ds_map_hashmap.R 3 | \name{hashmap} 4 | \alias{hashmap} 5 | \title{Create a new \code{hashmap}} 6 | \usage{ 7 | hashmap(key.class = c("character", "numeric", "integer")) 8 | } 9 | \arguments{ 10 | \item{key.class}{the primitive class type of the keys} 11 | } 12 | \value{ 13 | returns a new \code{hashmap} object 14 | } 15 | \description{ 16 | Instantiates a new \code{\linkS4class{hashmap}} object, 17 | i.e. an unordered collection of key-value pairs with mapping 18 | \deqn{f: keys -> values}, where only unique key-value pairs 19 | can be stored. 20 | } 21 | \examples{ 22 | # creates a hashmap 23 | h <- hashmap() 24 | 25 | # creates a hashmap 26 | h <- hashmap("integer") 27 | 28 | # creates a hashmap 29 | h <- hashmap("numeric") 30 | 31 | } 32 | -------------------------------------------------------------------------------- /man/heap-class.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ds_heap.R 3 | \docType{class} 4 | \name{heap-class} 5 | \alias{heap-class} 6 | \title{Abstract heap class} 7 | \description{ 8 | Abstract heap class 9 | } 10 | \section{Slots}{ 11 | 12 | \describe{ 13 | \item{\code{.heap}}{\code{C++} object representing a heap} 14 | 15 | \item{\code{.key.class}}{the class of the keys} 16 | }} 17 | 18 | -------------------------------------------------------------------------------- /man/insert-methods.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods_insert.R, R/ds_deque.R, R/ds_heap.R, 3 | % R/ds_map_bimap.R, R/ds_map_unordered.R 4 | \docType{methods} 5 | \name{insert} 6 | \alias{insert} 7 | \alias{insert,deque,ANY,missing-method} 8 | \alias{insert,deque,list,missing-method} 9 | \alias{insert,heap,vector,vector-method} 10 | \alias{insert,heap,vector,matrix-method} 11 | \alias{insert,heap,vector,list-method} 12 | \alias{insert,heap,vector,ANY-method} 13 | \alias{insert,bimap,vector,vector-method} 14 | \alias{insert,unordered_map,vector,vector-method} 15 | \alias{insert,unordered_map,vector,list-method} 16 | \alias{insert,unordered_map,vector,ANY-method} 17 | \title{Add elements to an object} 18 | \usage{ 19 | insert(obj, x, y) 20 | 21 | \S4method{insert}{deque,ANY,missing}(obj, x) 22 | 23 | \S4method{insert}{deque,list,missing}(obj, x) 24 | 25 | \S4method{insert}{heap,vector,vector}(obj, x, y) 26 | 27 | \S4method{insert}{heap,vector,matrix}(obj, x, y) 28 | 29 | \S4method{insert}{heap,vector,list}(obj, x, y) 30 | 31 | \S4method{insert}{heap,vector,ANY}(obj, x, y) 32 | 33 | \S4method{insert}{bimap,vector,vector}(obj, x, y) 34 | 35 | \S4method{insert}{unordered_map,vector,vector}(obj, x, y) 36 | 37 | \S4method{insert}{unordered_map,vector,list}(obj, x, y) 38 | 39 | \S4method{insert}{unordered_map,vector,ANY}(obj, x, y) 40 | } 41 | \arguments{ 42 | \item{obj}{object to insert into} 43 | 44 | \item{x}{the values/keys to insert into} 45 | 46 | \item{y}{values to be inserted which are required for some datastructures} 47 | } 48 | \value{ 49 | returns \code{obj} with inserted values 50 | } 51 | \description{ 52 | Adds keys or pairs to an object and returns the 53 | object. Depending on the datastructure used, either only keys are required 54 | or pairs of . Insertion of elements with vectors, i.e. giving 55 | multiple arguments at the same time is faster than inserting elements 56 | iteratively. 57 | } 58 | \examples{ 59 | 60 | # inserts values into a multimap 61 | m_map <- multimap() 62 | m_map <- insert(m_map, c("a", "b"), 1:2) 63 | m_map <- insert(m_map, c("a", "b"), list(1, list(a=1))) 64 | m_map["a"] <- rnorm(length(letters)) 65 | m_map[c("a", "b", "c")] <- list(1, data.frame(a=2), environment()) 66 | 67 | # inserts values into a fibonacci_heap 68 | f_heap <- fibonacci_heap("integer") 69 | f_heap <- insert(f_heap, 1:2, 1:2) 70 | f_heap[3:4] <- list(1, list(a=1)) 71 | f_heap <- insert(f_heap, 5:6, list(data.frame(a=rnorm(3)), diag(2))) 72 | 73 | # inserts elements into a queue or stack 74 | s <- stack() 75 | s <- insert(s, list(1, vector(), list(3), data.frame(rnorm(3)))) 76 | 77 | } 78 | -------------------------------------------------------------------------------- /man/keys-methods.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods_keys.R, R/ds_map_bimap.R, 3 | % R/ds_map_unordered.R 4 | \docType{methods} 5 | \name{keys} 6 | \alias{keys} 7 | \alias{keys,bimap-method} 8 | \alias{keys,unordered_map-method} 9 | \title{Get keys from an object} 10 | \usage{ 11 | keys(obj) 12 | 13 | \S4method{keys}{bimap}(obj) 14 | 15 | \S4method{keys}{unordered_map}(obj) 16 | } 17 | \arguments{ 18 | \item{obj}{object to extract keys from} 19 | } 20 | \value{ 21 | returns the extracted keys as vector 22 | } 23 | \description{ 24 | Extracts the keys from a \code{map} object. 25 | } 26 | \examples{ 27 | 28 | # returns the keys of a hashmap 29 | h_map <- hashmap("numeric") 30 | h_map[rnorm(3)] <- list(1, 2, 3) 31 | keys(h_map) 32 | 33 | # returns the keys of a multimap 34 | m_map <- multimap("numeric") 35 | m_map[c(1, 2, 1)] <- list(rnorm(1), rgamma(1, 1), rexp(1)) 36 | keys(m_map) 37 | 38 | } 39 | -------------------------------------------------------------------------------- /man/map-class.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ds_map.R 3 | \docType{class} 4 | \name{map-class} 5 | \alias{map-class} 6 | \title{Map class} 7 | \description{ 8 | Abstract map class 9 | } 10 | \section{Slots}{ 11 | 12 | \describe{ 13 | \item{\code{.map}}{\code{C++} object representing a mapping} 14 | 15 | \item{\code{.key.class}}{the class of the keys} 16 | }} 17 | 18 | -------------------------------------------------------------------------------- /man/multimap-class.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ds_map_multimap.R 3 | \docType{class} 4 | \name{multimap-class} 5 | \alias{multimap-class} 6 | \title{Multimap class} 7 | \description{ 8 | Implementation of a multimap data structure, i.e. an unordered 9 | collection of key-value pairs: 10 | \deqn{f: keys -> values.} 11 | Multimaps are able to store several identical keys. For a data structure 12 | which unique keys, see \code{\link{hashmap}}. 13 | Inserting and accessing is amortized in \emph{O(1)}. 14 | \code{hashmap} wraps a C++ \code{unordered_multimap} using Rcpp modules. 15 | Also see \code{\linkS4class{bimap}} for mappings in both ways. 16 | } 17 | \section{Slots}{ 18 | 19 | \describe{ 20 | \item{\code{.map}}{\code{C++} object representing a mapping} 21 | 22 | \item{\code{.key.class}}{the class of the keys} 23 | }} 24 | 25 | \seealso{ 26 | \code{\link{multimap}} for creating a new \code{multimap} object 27 | } 28 | -------------------------------------------------------------------------------- /man/multimap.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ds_map_multimap.R 3 | \name{multimap} 4 | \alias{multimap} 5 | \title{Create a new \code{multimap}} 6 | \usage{ 7 | multimap(key.class = c("character", "numeric", "integer")) 8 | } 9 | \arguments{ 10 | \item{key.class}{the primitive class type of the keys} 11 | } 12 | \value{ 13 | returns a new \code{multimap} object 14 | } 15 | \description{ 16 | Instantiates a new \code{\linkS4class{multimap}} object, 17 | i.e. an unordered collection of key-value pairs with mapping 18 | \deqn{f: keys -> values}, where multiple identical key-value paors 19 | can be stored. 20 | } 21 | \examples{ 22 | # creates a new multimap 23 | m <- multimap() 24 | 25 | # creates a new multimap 26 | m <- multimap("numeric") 27 | 28 | # creates a new multimap 29 | m <- multimap("integer") 30 | 31 | } 32 | -------------------------------------------------------------------------------- /man/peek-methods.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods_peek.R, R/ds_deque.R, R/ds_heap.R, 3 | % R/ds_map.R 4 | \docType{methods} 5 | \name{peek} 6 | \alias{peek} 7 | \alias{peek,deque-method} 8 | \alias{peek,heap-method} 9 | \alias{peek,map-method} 10 | \title{Have a look at the first element from an object without removing it} 11 | \usage{ 12 | peek(obj) 13 | 14 | \S4method{peek}{deque}(obj) 15 | 16 | \S4method{peek}{heap}(obj) 17 | 18 | \S4method{peek}{map}(obj) 19 | } 20 | \arguments{ 21 | \item{obj}{the object to peek} 22 | } 23 | \value{ 24 | returns the first element from \code{obj} as list 25 | } 26 | \description{ 27 | Peeks into an object, i.e. takes the first element and returns 28 | it without removing it from the object. The data structure that has a peek 29 | method usually uses some sort of priority of its elements. 30 | } 31 | \examples{ 32 | 33 | # peeks into a queue 34 | q <- queue() 35 | q <- insert(q, list(environment(), data.frame(a=1))) 36 | peek(q) 37 | 38 | # peeks into a fibonacci heap 39 | b_heap <- binomial_heap() 40 | b_heap <- insert(b_heap, letters[seq(3)], list(1, diag(3), rnorm(2))) 41 | peek(b_heap) 42 | 43 | # peeks into a \\code{hashmap} 44 | h_map <- hashmap() 45 | h_map[letters] <- rnorm(length(letters)) 46 | peek(h_map) 47 | 48 | # peeks into a \\code{bimap} 49 | b_map <- bimap("integer", "integer") 50 | b_map[seq(10)] <- seq(10, 1) 51 | peek(b_map) 52 | 53 | } 54 | -------------------------------------------------------------------------------- /man/pop-methods.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods_pop.R, R/ds_deque.R, R/ds_heap.R 3 | \docType{methods} 4 | \name{pop} 5 | \alias{pop} 6 | \alias{pop,deque-method} 7 | \alias{pop,heap-method} 8 | \title{Pop a single element from an object} 9 | \usage{ 10 | pop(obj) 11 | 12 | \S4method{pop}{deque}(obj) 13 | 14 | \S4method{pop}{heap}(obj) 15 | } 16 | \arguments{ 17 | \item{obj}{the object to pop an element from} 18 | } 19 | \value{ 20 | returns the first element from \code{obj} as list 21 | } 22 | \description{ 23 | Remove and return the first element from a data structure that 24 | has a priority, such as a \code{heap} or \code{deque}. 25 | } 26 | \examples{ 27 | 28 | # pops from a queue 29 | q <- queue() 30 | q <- insert(q, list(environment(), data.frame(a=1))) 31 | pop(q) 32 | 33 | # pops from a stack 34 | s <- stack() 35 | s <- insert(s, list(environment(), data.frame(a=1))) 36 | pop(s) 37 | 38 | # pops from a fibonacci heap 39 | b_heap <- binomial_heap() 40 | b_heap <- insert(b_heap, letters[seq(3)], list(1, diag(3), rnorm(2))) 41 | pop(b_heap) 42 | 43 | } 44 | -------------------------------------------------------------------------------- /man/queue-class.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ds_deque_queue.R 3 | \docType{class} 4 | \name{queue-class} 5 | \alias{queue-class} 6 | \title{Queue class} 7 | \description{ 8 | Implementation of a queue data structure, i.e. a list 9 | implementation with FIFO principle. \code{queue} uses a \code{std::deque} 10 | as default container, so inserting, peeking and popping functions require 11 | constant \emph{O(1)}. See \code{\linkS4class{stack}} for a class using 12 | the LIFO principle. 13 | } 14 | \section{Slots}{ 15 | 16 | \describe{ 17 | \item{\code{.deque}}{\code{C++} object representing a deque} 18 | }} 19 | 20 | \seealso{ 21 | \code{\link{queue}} for creating a new \code{queue} object. 22 | } 23 | -------------------------------------------------------------------------------- /man/queue.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ds_deque_queue.R 3 | \name{queue} 4 | \alias{queue} 5 | \title{Create a new \code{queue}} 6 | \usage{ 7 | queue() 8 | } 9 | \value{ 10 | returns a new \code{queue} object 11 | } 12 | \description{ 13 | Instantiates a new \code{\linkS4class{queue}} object, 14 | i.e. a list implementation with FIFO principle. 15 | } 16 | \examples{ 17 | # returns a new queue 18 | q <- queue() 19 | 20 | } 21 | -------------------------------------------------------------------------------- /man/size-methods.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods_size.R, R/ds_deque.R, R/ds_heap.R, 3 | % R/ds_map.R 4 | \docType{methods} 5 | \name{size} 6 | \alias{size} 7 | \alias{size,deque-method} 8 | \alias{size,heap-method} 9 | \alias{size,map-method} 10 | \title{Get the size of an object} 11 | \usage{ 12 | size(obj) 13 | 14 | \S4method{size}{deque}(obj) 15 | 16 | \S4method{size}{heap}(obj) 17 | 18 | \S4method{size}{map}(obj) 19 | } 20 | \arguments{ 21 | \item{obj}{the object to get the size from} 22 | } 23 | \value{ 24 | returns the size of \code{obj} 25 | } 26 | \description{ 27 | Computes the size of an object, i.e. the number of keys or 28 | pairs depending on the object. 29 | } 30 | \examples{ 31 | 32 | # get the size of a hashmap 33 | h_map <- hashmap() 34 | h_map[letters] <- rnorm(length(letters)) 35 | size(h_map) 36 | 37 | # get the size of a fibonacci heap 38 | f_heap <- fibonacci_heap() 39 | f_heap <- insert(f_heap, letters[seq(3)], list(1, diag(3), rnorm(2))) 40 | size(f_heap) 41 | 42 | # get the size of a stack 43 | s <- stack() 44 | s <- insert(s, list(1)) 45 | size(s) 46 | 47 | } 48 | -------------------------------------------------------------------------------- /man/stack-class.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ds_deque_stack.R 3 | \docType{class} 4 | \name{stack-class} 5 | \alias{stack-class} 6 | \title{Stack class} 7 | \description{ 8 | Implementation of a stack data structure, i.e. a list 9 | implementation with LIFO principle. \code{stack} uses a \code{std::deque} 10 | as default container, so inserting, peeking and popping functions require 11 | constant \emph{O(1)}. See \code{\linkS4class{queue}} for a class using 12 | the FIFO principle. 13 | } 14 | \section{Slots}{ 15 | 16 | \describe{ 17 | \item{\code{.deque}}{\code{C++} object representing a deque} 18 | }} 19 | 20 | \seealso{ 21 | \code{\link{stack}} for creating a new \code{stack} object. 22 | } 23 | -------------------------------------------------------------------------------- /man/stack.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ds_deque_stack.R 3 | \name{stack} 4 | \alias{stack} 5 | \title{Create a new \code{stack}} 6 | \usage{ 7 | stack(...) 8 | } 9 | \arguments{ 10 | \item{...}{parameters that are only needed if \code{utils::stack} should be 11 | called} 12 | } 13 | \value{ 14 | returns a new \code{stack} object 15 | } 16 | \description{ 17 | Instantiates a new \code{\linkS4class{stack}} object, 18 | i.e. a list implementation with LIFO principle. 19 | } 20 | \examples{ 21 | # creates a new stack 22 | s <- stack() 23 | 24 | } 25 | -------------------------------------------------------------------------------- /man/sub-unordered_map-vector-missing-missing-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ds_map_unordered.R 3 | \docType{methods} 4 | \name{[,unordered_map,vector,missing,missing-method} 5 | \alias{[,unordered_map,vector,missing,missing-method} 6 | \title{Extract elements from an object} 7 | \usage{ 8 | \S4method{[}{unordered_map,vector,missing,missing}(x, i) 9 | } 10 | \arguments{ 11 | \item{x}{an unorderd map object, such as a \code{\link{hashmap}} or 12 | \code{\link{multimap}}} 13 | 14 | \item{i}{a vector of keys} 15 | } 16 | \description{ 17 | Access pairs of an unordered map using a 18 | set of keys. 19 | } 20 | -------------------------------------------------------------------------------- /man/subset-bimap-vector-missing-vector-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ds_map_bimap.R 3 | \docType{methods} 4 | \name{[<-,bimap,vector,missing,vector-method} 5 | \alias{[<-,bimap,vector,missing,vector-method} 6 | \title{Insert parts to an object} 7 | \usage{ 8 | \S4method{[}{bimap,vector,missing,vector}(x, i) <- value 9 | } 10 | \arguments{ 11 | \item{x}{a \code{map} object} 12 | 13 | \item{i}{a vector of keys} 14 | 15 | \item{value}{a vector of values for the keys} 16 | } 17 | \description{ 18 | Inserts pairs to a bimap. 19 | } 20 | -------------------------------------------------------------------------------- /man/subset-heap-vector-missing-list-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ds_heap.R 3 | \docType{methods} 4 | \name{[<-,heap,vector,missing,list-method} 5 | \alias{[<-,heap,vector,missing,list-method} 6 | \title{Insert parts to an object} 7 | \usage{ 8 | \S4method{[}{heap,vector,missing,list}(x, i) <- value 9 | } 10 | \arguments{ 11 | \item{x}{a heap object, such as a \code{\link{fibonacci_heap}} or a 12 | \code{\link{binomial_heap}}} 13 | 14 | \item{i}{a vector of keys} 15 | 16 | \item{value}{a vector of values for the keys} 17 | } 18 | \description{ 19 | Inserts pairs to a heap. The keys are 20 | determine the ordering of the heap, while the value is the actual value to 21 | store. 22 | } 23 | -------------------------------------------------------------------------------- /man/subset-heap-vector-missing-matrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ds_heap.R 3 | \docType{methods} 4 | \name{[<-,heap,vector,missing,matrix-method} 5 | \alias{[<-,heap,vector,missing,matrix-method} 6 | \title{Insert parts to an object} 7 | \usage{ 8 | \S4method{[}{heap,vector,missing,matrix}(x, i) <- value 9 | } 10 | \arguments{ 11 | \item{x}{a heap object, such as a \code{\link{fibonacci_heap}} or a 12 | \code{\link{binomial_heap}}} 13 | 14 | \item{i}{a vector of keys} 15 | 16 | \item{value}{a vector of values for the keys} 17 | } 18 | \description{ 19 | Inserts pairs to a heap. The keys are 20 | determine the ordering of the heap, while the value is the actual value to 21 | store. 22 | } 23 | -------------------------------------------------------------------------------- /man/subset-heap-vector-missing-vector-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ds_heap.R 3 | \docType{methods} 4 | \name{[<-,heap,vector,missing,vector-method} 5 | \alias{[<-,heap,vector,missing,vector-method} 6 | \title{Insert parts to an object} 7 | \usage{ 8 | \S4method{[}{heap,vector,missing,vector}(x, i) <- value 9 | } 10 | \arguments{ 11 | \item{x}{a heap object, such as a \code{\link{fibonacci_heap}} or a 12 | \code{\link{binomial_heap}}} 13 | 14 | \item{i}{a vector of keys} 15 | 16 | \item{value}{a vector of values for the keys} 17 | } 18 | \description{ 19 | Inserts pairs to a heap. The keys are 20 | determine the ordering of the heap, while the value is the actual value to 21 | store. 22 | } 23 | -------------------------------------------------------------------------------- /man/subset-unordered_map-vector-missing-ANY-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ds_map_unordered.R 3 | \docType{methods} 4 | \name{[<-,unordered_map,vector,missing,ANY-method} 5 | \alias{[<-,unordered_map,vector,missing,ANY-method} 6 | \title{Insert parts to an object} 7 | \usage{ 8 | \S4method{[}{unordered_map,vector,missing,ANY}(x, i) <- value 9 | } 10 | \arguments{ 11 | \item{x}{x an unorderd map object, such as a \code{\link{hashmap}} or 12 | \code{\link{multimap}}} 13 | 14 | \item{i}{a vector of keys} 15 | 16 | \item{value}{a vector of values for the keys} 17 | } 18 | \description{ 19 | Inserts pairs to an unordered_map. 20 | } 21 | -------------------------------------------------------------------------------- /man/subset-unordered_map-vector-missing-list-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ds_map_unordered.R 3 | \docType{methods} 4 | \name{[<-,unordered_map,vector,missing,list-method} 5 | \alias{[<-,unordered_map,vector,missing,list-method} 6 | \title{Insert parts to an object} 7 | \usage{ 8 | \S4method{[}{unordered_map,vector,missing,list}(x, i) <- value 9 | } 10 | \arguments{ 11 | \item{x}{x an unorderd map object, such as a \code{\link{hashmap}} or 12 | \code{\link{multimap}}} 13 | 14 | \item{i}{a vector of keys} 15 | 16 | \item{value}{a vector of values for the keys} 17 | } 18 | \description{ 19 | Inserts pairs to an unordered_map. 20 | } 21 | -------------------------------------------------------------------------------- /man/subset-unordered_map-vector-missing-vector-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ds_map_unordered.R 3 | \docType{methods} 4 | \name{[<-,unordered_map,vector,missing,vector-method} 5 | \alias{[<-,unordered_map,vector,missing,vector-method} 6 | \title{Insert parts to an object} 7 | \usage{ 8 | \S4method{[}{unordered_map,vector,missing,vector}(x, i) <- value 9 | } 10 | \arguments{ 11 | \item{x}{x an unorderd map object, such as a \code{\link{hashmap}} or 12 | \code{\link{multimap}}} 13 | 14 | \item{i}{a vector of keys} 15 | 16 | \item{value}{a vector of values for the keys} 17 | } 18 | \description{ 19 | Inserts pairs to an unordered_map. 20 | } 21 | -------------------------------------------------------------------------------- /man/unordered_map-class.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ds_map_unordered.R 3 | \docType{class} 4 | \name{unordered_map-class} 5 | \alias{unordered_map-class} 6 | \title{Abstract unordered map class} 7 | \description{ 8 | Abstract unordered map class 9 | } 10 | \section{Slots}{ 11 | 12 | \describe{ 13 | \item{\code{.map}}{\code{C++} object representing a mapping} 14 | 15 | \item{\code{.key.class}}{the class of the keys} 16 | }} 17 | 18 | -------------------------------------------------------------------------------- /man/values-methods.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods_values.R, R/ds_heap.R, 3 | % R/ds_map_bimap.R, R/ds_map_unordered.R 4 | \docType{methods} 5 | \name{values} 6 | \alias{values} 7 | \alias{values,heap-method} 8 | \alias{values,bimap-method} 9 | \alias{values,unordered_map-method} 10 | \title{Get values from an object} 11 | \usage{ 12 | values(obj) 13 | 14 | \S4method{values}{heap}(obj) 15 | 16 | \S4method{values}{bimap}(obj) 17 | 18 | \S4method{values}{unordered_map}(obj) 19 | } 20 | \arguments{ 21 | \item{obj}{object to extract values from} 22 | } 23 | \value{ 24 | returns the extracted values as a \code{list} or, when primitive, as 25 | a \code{vector}. In case of a \code{heap} also returns \code{key} and \code{handle} 26 | of the heap node. 27 | } 28 | \description{ 29 | Extracts the values from a data structure such as a \code{map} 30 | or \code{heap} object. 31 | } 32 | \examples{ 33 | 34 | # shows the values of a hashmap 35 | h_map <- hashmap("integer") 36 | h_map <- insert(h_map, seq(2), list(data.frame(a=1), 3)) 37 | values(h_map) 38 | 39 | # shows the values of a multimap 40 | m_map <- multimap("integer") 41 | m_map[seq(2)] <- list(diag(2), rnorm(3)) 42 | values(m_map) 43 | 44 | # shows the values of a heap 45 | f_heap <- fibonacci_heap("integer") 46 | f_heap <- insert(f_heap, 1:2, list(diag(2), rnorm(3))) 47 | values(f_heap) 48 | 49 | } 50 | -------------------------------------------------------------------------------- /pkgdown/_pkgdown.yml: -------------------------------------------------------------------------------- 1 | authors: 2 | Simon Dirmeier: 3 | href: http://www.simon-dirmeier.net 4 | 5 | navbar: 6 | title: "datastructures" 7 | type: inverse 8 | left: 9 | - text: "Vignettes" 10 | menu: 11 | - text: "Tutorial" 12 | href: articles/datastructures.html 13 | - text: "Reference" 14 | href: reference/index.html 15 | - text: "News" 16 | href: news/index.html 17 | right: 18 | - icon: fa-home fa-2x 19 | href: http://simon-dirmeier.net 20 | - icon: fa-github fa-2x 21 | href: https://github.com/dirmeier/datastructures 22 | - icon: fa fa-twitter fa-2x 23 | href: http://twitter.com/simon_dirmeier/ 24 | 25 | template: 26 | params: 27 | bootswatch: lumen 28 | -------------------------------------------------------------------------------- /pkgdown/extra.css: -------------------------------------------------------------------------------- 1 | /* 2 | # datastructures: Implementation of Core Data Structures 3 | # 4 | # Copyright (C) 2018 Simon Dirmeier 5 | # 6 | # This file is part of datastructures 7 | # 8 | # datastructures is free software: you can redistribute it and/or modify 9 | # it under the terms of the GNU General Public License as published by 10 | # the Free Software Foundation, either version 3 of the License, or 11 | # (at your option) any later version. 12 | # 13 | # datastructures is distributed in the hope that it will be useful, 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | # GNU General Public License for more details. 17 | # 18 | # You should have received a copy of the GNU General Public License 19 | # along with datastructures. If not, see . 20 | # 21 | */ 22 | 23 | @import url('https://fonts.googleapis.com/css?family=Roboto|Hind|Inconsolata'); 24 | 25 | .section { 26 | margin-top: 30px; 27 | margin-bottom: 30px; 28 | } 29 | 30 | code,pre { 31 | font-family: "Inconsolata"; 32 | font-size: 1.4rem; 33 | } 34 | 35 | body { 36 | font-size: 1.4em; 37 | line-height: 1.6; 38 | font-family: "Roboto", "Helvetica Neue", Helvetica, Arial, sans-serif; 39 | color: #222; 40 | } 41 | 42 | h1, h2, h3, h4, h5, h6 { 43 | margin-top: 0; 44 | margin-bottom: 2rem; 45 | font-weight: 300; 46 | } 47 | 48 | h1 { 49 | font-size: 4.0rem; 50 | font-style: italic; 51 | 52 | } 53 | 54 | h2 { 55 | font-size: 3.0rem; 56 | line-height: 1.25; 57 | letter-spacing: -.1rem; 58 | } 59 | 60 | h3 { 61 | font-size: 2.5rem; 62 | line-height: 1.3; 63 | letter-spacing: -.1rem; 64 | } 65 | 66 | h4 { 67 | font-size: 2.0rem; 68 | line-height: 1.35; 69 | letter-spacing: -.08rem; 70 | } 71 | 72 | h5 { 73 | font-size: 1.8rem; 74 | line-height: 1.5; 75 | letter-spacing: -.05rem; 76 | } 77 | 78 | h6 { 79 | font-size: 1.5rem; 80 | line-height: 1.6; 81 | letter-spacing: 0; 82 | } 83 | 84 | .navbar { 85 | font-family: "Roboto", "Helvetica Neue", Helvetica, Arial, sans-serif; 86 | border-width: 0 1px 2px 1px; 87 | position: fixed; 88 | max-height: 20px; 89 | top: 0px; 90 | } 91 | 92 | .navbar .navbar-nav > li > a > .fa { 93 | font-size: 21px; 94 | } 95 | 96 | .navbar .navbar-nav > li > a { 97 | text-transform: uppercase; 98 | font-size: 11px; 99 | letter-spacing: .2rem; 100 | margin-right: 5px; 101 | color: #222; 102 | } 103 | 104 | .navbar .navbar-brand { 105 | text-transform: uppercase; 106 | font-weight: normal; 107 | font-size: 11px; 108 | font-weight: 300; 109 | letter-spacing: .2rem; 110 | font-style: italic; 111 | margin-right: 35px; 112 | text-decoration: none; 113 | color: #222; 114 | } 115 | 116 | .navbar .navbar-brand:hover { 117 | color: #1EAEDB; 118 | } 119 | 120 | .navbar .navbar-nav > li > a:hover, 121 | .navbar .navbar-brand > a:hover { 122 | color: #1EAEDB; 123 | } 124 | -------------------------------------------------------------------------------- /src/Makevars: -------------------------------------------------------------------------------- 1 | CXX_STD = CXX11 2 | PKG_CPPFLAGS = -I../inst/include/ 3 | -------------------------------------------------------------------------------- /src/Makevars.win: -------------------------------------------------------------------------------- 1 | CXX_STD = CXX11 2 | PKG_CPPFLAGS = -I../inst/include/ 3 | -------------------------------------------------------------------------------- /src/binomial_heap_module.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * datastructures: Implementation of core datastructures for R. 3 | *

    4 | * Copyright (C) Simon Dirmeier 5 | *

    6 | * This file is part of datastructures. 7 | *

    8 | * datastructures is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | *

    13 | * datastructures is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | *

    18 | * You should have received a copy of the GNU General Public License 19 | * along with datastructures. If not, see . 20 | * 21 | */ 22 | 23 | 24 | #include 25 | #include "binomial_heap.hpp" 26 | 27 | 28 | RCPP_MODULE(binomial_heap_module) { 29 | Rcpp::class_< binomial_heap_s >( "binomial_heap_s" ) 30 | .constructor() 31 | .method("peek", &binomial_heap_s::peek) 32 | .method("handles", &binomial_heap_s::handles) 33 | .method("handle_values", &binomial_heap_s::handles_value) 34 | .method("values", &binomial_heap_s::values) 35 | .method("decrease_key", &binomial_heap_s::decrease_key) 36 | .method("pop", &binomial_heap_s::pop) 37 | .method("size", &binomial_heap_s::size) 38 | .method("insert", &binomial_heap_s::insert) 39 | .method("clear", &binomial_heap_s::clear); 40 | Rcpp::class_< binomial_heap_d >( "binomial_heap_d" ) 41 | .constructor() 42 | .method("peek", &binomial_heap_d::peek) 43 | .method("handles", &binomial_heap_d::handles) 44 | .method("handle_values", &binomial_heap_d::handles_value) 45 | .method("values", &binomial_heap_d::values) 46 | .method("decrease_key", &binomial_heap_d::decrease_key) 47 | .method("pop", &binomial_heap_d::pop) 48 | .method("size", &binomial_heap_d::size) 49 | .method("insert", &binomial_heap_d::insert) 50 | .method("clear", &binomial_heap_d::clear); 51 | Rcpp::class_< binomial_heap_i >( "binomial_heap_i" ) 52 | .constructor() 53 | .method("peek", &binomial_heap_i::peek) 54 | .method("handles", &binomial_heap_i::handles) 55 | .method("handle_values", &binomial_heap_i::handles_value) 56 | .method("values", &binomial_heap_i::values) 57 | .method("decrease_key", &binomial_heap_i::decrease_key) 58 | .method("pop", &binomial_heap_i::pop) 59 | .method("size", &binomial_heap_i::size) 60 | .method("insert", &binomial_heap_i::insert) 61 | .method("clear", &binomial_heap_i::clear); 62 | } 63 | -------------------------------------------------------------------------------- /src/fibonacci_heap_module.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * datastructures: Implementation of core datastructures for R. 3 | *

    4 | * Copyright (C) Simon Dirmeier 5 | *

    6 | * This file is part of datastructures. 7 | *

    8 | * datastructures is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | *

    13 | * datastructures is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | *

    18 | * You should have received a copy of the GNU General Public License 19 | * along with datastructures. If not, see . 20 | * 21 | */ 22 | 23 | 24 | #include 25 | #include "fibonacci_heap.hpp" 26 | 27 | 28 | RCPP_MODULE(fibonacci_heap_module) { 29 | Rcpp::class_< fibonacci_heap_s >( "fibonacci_heap_s" ) 30 | .constructor() 31 | .method("peek", &fibonacci_heap_s::peek) 32 | .method("handles", &fibonacci_heap_s::handles) 33 | .method("handle_values", &fibonacci_heap_s::handles_value) 34 | .method("values", &fibonacci_heap_s::values) 35 | .method("decrease_key", &fibonacci_heap_s::decrease_key) 36 | .method("pop", &fibonacci_heap_s::pop) 37 | .method("size", &fibonacci_heap_s::size) 38 | .method("insert", &fibonacci_heap_s::insert) 39 | .method("clear", &fibonacci_heap_s::clear); 40 | Rcpp::class_< fibonacci_heap_d >( "fibonacci_heap_d" ) 41 | .constructor() 42 | .method("peek", &fibonacci_heap_d::peek) 43 | .method("handles", &fibonacci_heap_d::handles) 44 | .method("handle_values", &fibonacci_heap_d::handles_value) 45 | .method("values", &fibonacci_heap_d::values) 46 | .method("decrease_key", &fibonacci_heap_d::decrease_key) 47 | .method("pop", &fibonacci_heap_d::pop) 48 | .method("size", &fibonacci_heap_d::size) 49 | .method("insert", &fibonacci_heap_d::insert) 50 | .method("clear", &fibonacci_heap_d::clear); 51 | Rcpp::class_< fibonacci_heap_i >( "fibonacci_heap_i" ) 52 | .constructor() 53 | .method("peek", &fibonacci_heap_i::peek) 54 | .method("handles", &fibonacci_heap_i::handles) 55 | .method("handle_values", &fibonacci_heap_i::handles_value) 56 | .method("values", &fibonacci_heap_i::values) 57 | .method("decrease_key", &fibonacci_heap_i::decrease_key) 58 | .method("pop", &fibonacci_heap_i::pop) 59 | .method("size", &fibonacci_heap_i::size) 60 | .method("insert", &fibonacci_heap_i::insert) 61 | .method("clear", &fibonacci_heap_i::clear); 62 | } 63 | -------------------------------------------------------------------------------- /src/hashmap_module.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * datastructures: Implementation of core datastructures for R. 3 | *

    4 | * Copyright (C) Simon Dirmeier 5 | *

    6 | * This file is part of datastructures. 7 | *

    8 | * datastructures is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | *

    13 | * datastructures is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | *

    18 | * You should have received a copy of the GNU General Public License 19 | * along with datastructures. If not, see . 20 | * 21 | */ 22 | 23 | #include 24 | #include "hashmap.hpp" 25 | 26 | RCPP_MODULE(hashmap_module) { 27 | Rcpp::class_< hashmap_s >( "hashmap_s" ) 28 | .constructor() 29 | .method("keys", &hashmap_s::keys) 30 | .method("values", &hashmap_s::values) 31 | .method("clear", &hashmap_s::clear) 32 | .method("remove", &hashmap_s::remove) 33 | .method("remove_with_value", &hashmap_s::remove_with_value) 34 | .method("head", &hashmap_s::head) 35 | .method("size", &hashmap_s::size) 36 | .method("insert", &hashmap_s::insert) 37 | .method("get", &hashmap_s::get); 38 | Rcpp::class_< hashmap_d >( "hashmap_d" ) 39 | .constructor() 40 | .method("keys", &hashmap_d::keys) 41 | .method("values", &hashmap_d::values) 42 | .method("clear", &hashmap_d::clear) 43 | .method("remove", &hashmap_d::remove) 44 | .method("remove_with_value", &hashmap_d::remove_with_value) 45 | .method("head", &hashmap_d::head) 46 | .method("size", &hashmap_d::size) 47 | .method("insert", &hashmap_d::insert) 48 | .method("get", &hashmap_d::get); 49 | Rcpp::class_< hashmap_i >( "hashmap_i" ) 50 | .constructor() 51 | .method("keys", &hashmap_i::keys) 52 | .method("values", &hashmap_i::values) 53 | .method("clear", &hashmap_i::clear) 54 | .method("remove", &hashmap_i::remove) 55 | .method("remove_with_value", &hashmap_i::remove_with_value) 56 | .method("head", &hashmap_i::head) 57 | .method("size", &hashmap_i::size) 58 | .method("insert", &hashmap_i::insert) 59 | .method("get", &hashmap_i::get); 60 | } 61 | -------------------------------------------------------------------------------- /src/init.c: -------------------------------------------------------------------------------- 1 | /** 2 | * datastructures: Implementation of core datastructures for R. 3 | *

    4 | * Copyright (C) Simon Dirmeier 5 | *

    6 | * This file is part of datastructures. 7 | *

    8 | * datastructures is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | *

    13 | * datastructures is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | *

    18 | * You should have received a copy of the GNU General Public License 19 | * along with datastructures. If not, see . 20 | * 21 | */ 22 | 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | extern SEXP _rcpp_module_boot_bimap_module(); 30 | extern SEXP _rcpp_module_boot_hashmap_module(); 31 | extern SEXP _rcpp_module_boot_multimap_module(); 32 | 33 | extern SEXP _rcpp_module_boot_binomial_heap_module(); 34 | extern SEXP _rcpp_module_boot_fibonacci_heap_module(); 35 | 36 | extern SEXP _rcpp_module_boot_queue_module(); 37 | extern SEXP _rcpp_module_boot_stack_module(); 38 | 39 | static const R_CallMethodDef CallEntries[] = { 40 | {"_rcpp_module_boot_hashmap_module", 41 | (DL_FUNC) &_rcpp_module_boot_hashmap_module, 0}, 42 | {"_rcpp_module_boot_bimap_module", 43 | (DL_FUNC) &_rcpp_module_boot_bimap_module, 0}, 44 | {"_rcpp_module_boot_multimap_module", 45 | (DL_FUNC) &_rcpp_module_boot_multimap_module, 0}, 46 | 47 | {"_rcpp_module_boot_binomial_heap_module", 48 | (DL_FUNC) &_rcpp_module_boot_binomial_heap_module, 0}, 49 | {"_rcpp_module_boot_fibonacci_heap_module", 50 | (DL_FUNC) &_rcpp_module_boot_fibonacci_heap_module, 0}, 51 | 52 | {"_rcpp_module_boot_queue_module", 53 | (DL_FUNC) &_rcpp_module_boot_queue_module, 0}, 54 | {"_rcpp_module_boot_stack_module", 55 | (DL_FUNC) &_rcpp_module_boot_stack_module, 0}, 56 | {NULL, NULL, 0} 57 | }; 58 | 59 | void R_init_datastructures(DllInfo *dll) { 60 | R_registerRoutines(dll, NULL, CallEntries, NULL, NULL); 61 | R_useDynamicSymbols(dll, FALSE); 62 | } 63 | -------------------------------------------------------------------------------- /src/multimap_module.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * datastructures: Implementation of core datastructures for R. 3 | *

    4 | * Copyright (C) Simon Dirmeier 5 | *

    6 | * This file is part of datastructures. 7 | *

    8 | * datastructures is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | *

    13 | * datastructures is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | *

    18 | * You should have received a copy of the GNU General Public License 19 | * along with datastructures. If not, see . 20 | * 21 | */ 22 | 23 | #include 24 | #include "multimap.hpp" 25 | 26 | 27 | RCPP_MODULE(multimap_module) { 28 | Rcpp::class_< multimap_s >( "multimap_s" ) 29 | .constructor() 30 | .method("keys", &multimap_s::keys) 31 | .method("values", &multimap_s::values) 32 | .method("clear", &multimap_s::clear) 33 | .method("remove", &multimap_s::remove) 34 | .method("remove_with_value", &multimap_s::remove_with_value) 35 | .method("head", &multimap_s::head) 36 | .method("size", &multimap_s::size) 37 | .method("insert", &multimap_s::insert) 38 | .method("get", &multimap_s::get); 39 | Rcpp::class_< multimap_d >( "multimap_d" ) 40 | .constructor() 41 | .method("keys", &multimap_d::keys) 42 | .method("values", &multimap_d::values) 43 | .method("clear", &multimap_d::clear) 44 | .method("remove", &multimap_d::remove) 45 | .method("remove_with_value", &multimap_d::remove_with_value) 46 | .method("head", &multimap_d::head) 47 | .method("size", &multimap_d::size) 48 | .method("insert", &multimap_d::insert) 49 | .method("get", &multimap_d::get); 50 | Rcpp::class_< multimap_i >( "multimap_i" ) 51 | .constructor() 52 | .method("keys", &multimap_i::keys) 53 | .method("values", &multimap_i::values) 54 | .method("clear", &multimap_i::clear) 55 | .method("remove", &multimap_i::remove) 56 | .method("remove_with_value", &multimap_i::remove_with_value) 57 | .method("head", &multimap_i::head) 58 | .method("size", &multimap_i::size) 59 | .method("insert", &multimap_i::insert) 60 | .method("get", &multimap_i::get); 61 | 62 | } 63 | -------------------------------------------------------------------------------- /src/queue_module.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * datastructures: Implementation of core datastructures for R. 3 | *

    4 | * Copyright (C) Simon Dirmeier 5 | *

    6 | * This file is part of datastructures. 7 | *

    8 | * datastructures is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | *

    13 | * datastructures is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | *

    18 | * You should have received a copy of the GNU General Public License 19 | * along with datastructures. If not, see . 20 | * 21 | */ 22 | 23 | #include 24 | #include "queue.hpp" 25 | 26 | RCPP_MODULE(queue_module) { 27 | Rcpp::class_< queue_sexp >( "queue_sexp" ) 28 | .constructor() 29 | .method("peek", &queue_sexp::peek) 30 | .method("clear", &queue_sexp::clear) 31 | .method("pop", &queue_sexp::pop) 32 | .method("size", &queue_sexp::size) 33 | .method("insert", &queue_sexp::insert); 34 | } 35 | -------------------------------------------------------------------------------- /src/stack_module.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * datastructures: Implementation of core datastructures for R. 3 | *

    4 | * Copyright (C) Simon Dirmeier 5 | *

    6 | * This file is part of datastructures. 7 | *

    8 | * datastructures is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | *

    13 | * datastructures is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | *

    18 | * You should have received a copy of the GNU General Public License 19 | * along with datastructures. If not, see . 20 | * 21 | */ 22 | 23 | #include 24 | #include "stack.hpp" 25 | 26 | RCPP_MODULE(stack_module) { 27 | Rcpp::class_< stack_sexp >( "stack_sexp" ) 28 | .constructor() 29 | .method("peek", &stack_sexp::peek) 30 | .method("clear", &stack_sexp::clear) 31 | .method("pop", &stack_sexp::pop) 32 | .method("size", &stack_sexp::size) 33 | .method("insert", &stack_sexp::insert); 34 | } 35 | -------------------------------------------------------------------------------- /tests/testthat.R: -------------------------------------------------------------------------------- 1 | # datastructures: Implementation of core datastructures for R. 2 | # 3 | # Copyright (C) Simon Dirmeier 4 | # 5 | # This file is part of datastructures. 6 | # 7 | # datastructures is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # datastructures is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with datastructures. If not, see . 19 | 20 | 21 | library(datastructures) 22 | library(testthat) 23 | 24 | test_check("datastructures") 25 | -------------------------------------------------------------------------------- /tests/testthat/tests_deque.R: -------------------------------------------------------------------------------- 1 | # datastructures: Implementation of core datastructures for R. 2 | # 3 | # Copyright (C) Simon Dirmeier 4 | # 5 | # This file is part of datastructures. 6 | # 7 | # datastructures is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # datastructures is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with datastructures. If not, see . 19 | 20 | 21 | context("deque") 22 | 23 | 24 | ds <- c(queue, stack) 25 | 26 | 27 | test_that("abstract class cannot get instantiated", { 28 | expect_error(methods::new("deque")) 29 | }) 30 | 31 | 32 | test_that("deque clears correctly", { 33 | for (d in ds) 34 | { 35 | deq <- d() 36 | deq <- insert(deq, rnorm(10)) 37 | expect_equal(size(deq), 1) 38 | deq <- clear(deq) 39 | expect_equal(size(deq), 0) 40 | } 41 | }) 42 | -------------------------------------------------------------------------------- /tests/testthat/tests_deque_queue.R: -------------------------------------------------------------------------------- 1 | # datastructures: Implementation of core datastructures for R. 2 | # 3 | # Copyright (C) Simon Dirmeier 4 | # 5 | # This file is part of datastructures. 6 | # 7 | # datastructures is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # datastructures is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with datastructures. If not, see . 19 | 20 | 21 | context("queue") 22 | 23 | 24 | test_that("queue pops first element as list", { 25 | q <- queue() 26 | r <- as.list(stats::rnorm(5)) 27 | queue <- insert(q, r) 28 | expect_equal(pop(q), r[[1]], tolerance = 0.1) 29 | }) 30 | 31 | 32 | test_that("queue peeks first element as list", { 33 | q <- queue() 34 | r <- as.list(stats::rnorm(5)) 35 | q <- insert(q, r) 36 | expect_equal(peek(q), r[[1]], tolerance = 0.1) 37 | }) 38 | 39 | 40 | test_that("queue peeks first element vectorial", { 41 | q <- queue() 42 | r <- stats::rnorm(5) 43 | q <- insert(q, r) 44 | expect_equal(peek(q), r, tolerance = 0.1) 45 | }) 46 | 47 | 48 | test_that("queue pops first element vectorial", { 49 | q <- queue() 50 | r <- stats::rnorm(5) 51 | q <- insert(q, r) 52 | expect_equal(pop(q), r, tolerance = 0.1) 53 | }) 54 | 55 | 56 | test_that("queue peeks first element multiple elements in list", { 57 | q <- queue() 58 | r <- stats::rnorm(5) 59 | q <- insert(q, list(r, 1)) 60 | expect_equal(peek(q), r, tolerance = 0.1) 61 | }) 62 | 63 | 64 | test_that("queue pop first element multiple elements in list", { 65 | q <- queue() 66 | r <- stats::rnorm(5) 67 | q <- insert(q, list(r, 1)) 68 | expect_equal(pop(q), r, tolerance = 0.1) 69 | }) 70 | 71 | 72 | test_that("queue can handle different values", { 73 | q <- queue() 74 | q <- insert(q, list(2, 1)) 75 | q <- insert(q, data.frame(A = 1)) 76 | q <- insert(q, rnorm(10)) 77 | expect_equal(pop(q), 2) 78 | expect_equal(pop(q), 1) 79 | expect_true(is.data.frame(pop(q))) 80 | expect_equal(length(pop(q)), 10) 81 | }) 82 | -------------------------------------------------------------------------------- /tests/testthat/tests_deque_stack.R: -------------------------------------------------------------------------------- 1 | # datastructures: Implementation of core datastructures for R. 2 | # 3 | # Copyright (C) Simon Dirmeier 4 | # 5 | # This file is part of datastructures. 6 | # 7 | # datastructures is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # datastructures is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with datastructures. If not, see . 19 | 20 | 21 | context("stack") 22 | 23 | 24 | test_that("stack pops first element as list", { 25 | s <- stack() 26 | r <- as.list(stats::rnorm(5)) 27 | s <- insert(s, r) 28 | expect_equal(pop(s), r[[5]], tolerance = 0.1) 29 | }) 30 | 31 | 32 | test_that("stack peeks first elemen as list", { 33 | s <- stack() 34 | r <- as.list(stats::rnorm(5)) 35 | s <- insert(s, r) 36 | expect_equal(peek(s), r[[5]], tolerance = 0.1) 37 | }) 38 | 39 | test_that("stack peeks first element vectorial", { 40 | q <- stack() 41 | r <- stats::rnorm(5) 42 | q <- insert(q, r) 43 | expect_equal(peek(q), r, tolerance = 0.1) 44 | }) 45 | 46 | 47 | test_that("stack pops first element vectorial", { 48 | q <- stack() 49 | r <- stats::rnorm(5) 50 | q <- insert(q, r) 51 | expect_equal(pop(q), r, tolerance = 0.1) 52 | }) 53 | 54 | 55 | test_that("stack peeks first element multiple elements in list", { 56 | q <- stack() 57 | r <- stats::rnorm(5) 58 | q <- insert(q, list(r, 1)) 59 | expect_equal(peek(q), 1, tolerance = 0.1) 60 | }) 61 | 62 | 63 | test_that("stack pop first element multiple elements in list", { 64 | q <- stack() 65 | r <- stats::rnorm(5) 66 | q <- insert(q, list(r, 1)) 67 | expect_equal(pop(q), 1, tolerance = 0.1) 68 | }) 69 | 70 | 71 | test_that("stack pop first element multiple elements in list", { 72 | q <- stack() 73 | r <- stats::rnorm(5) 74 | q <- insert(q, list(r, 1)) 75 | expect_equal(pop(q), 1, tolerance = 0.1) 76 | }) 77 | 78 | 79 | test_that("stack can handle different values", { 80 | q <- stack() 81 | q <- insert(q, list(2, 1)) 82 | q <- insert(q, data.frame(A = 1)) 83 | q <- insert(q, rnorm(10)) 84 | expect_equal(length(pop(q)), 10) 85 | expect_true(is.data.frame(pop(q))) 86 | expect_equal(pop(q), 1) 87 | expect_equal(pop(q), 2) 88 | }) 89 | -------------------------------------------------------------------------------- /tests/testthat/tests_map.R: -------------------------------------------------------------------------------- 1 | # datastructures: Implementation of core datastructures for R. 2 | # 3 | # Copyright (C) Simon Dirmeier 4 | # 5 | # This file is part of datastructures. 6 | # 7 | # datastructures is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # datastructures is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with datastructures. If not, see . 19 | 20 | 21 | context("map") 22 | 23 | 24 | maps <- c(multimap, bimap, hashmap) 25 | 26 | test_that("abstract class cannot get instantiated", { 27 | expect_error(methods::new("map")) 28 | }) 29 | 30 | test_that("maps clear correctly", { 31 | for (m in maps) { 32 | h <- m("character") 33 | h <- insert(h, letters[1], letters[1]) 34 | h <- insert(h, letters[2], letters[2]) 35 | h <- insert(h, letters[3], letters[3]) 36 | h <- clear(h) 37 | expect_equal(size(h), 0) 38 | } 39 | }) 40 | 41 | test_that("maps removes", { 42 | for (m in maps) { 43 | h <- m("character") 44 | h <- insert(h, letters[1], letters[1]) 45 | h <- insert(h, letters[2], letters[2]) 46 | h <- insert(h, letters[3], letters[3]) 47 | h <- erase(h, letters[1]) 48 | expect_error(h[letters[1]]) 49 | } 50 | }) 51 | -------------------------------------------------------------------------------- /tests/testthat/tests_map_bimap.R: -------------------------------------------------------------------------------- 1 | # datastructures: Implementation of core datastructures for R. 2 | # 3 | # Copyright (C) Simon Dirmeier 4 | # 5 | # This file is part of datastructures. 6 | # 7 | # datastructures is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # datastructures is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with datastructures. If not, see . 19 | 20 | 21 | context("bimap") 22 | 23 | 24 | test_that("creates correct class", { 25 | b <- bimap("numeric", "numeric") 26 | b <- insert(b, c(1, 2), c(4, 5)) 27 | expect_equal("Rcpp_bimap_dd", class(b@.map)[1]) 28 | }) 29 | 30 | 31 | test_that("map shows", { 32 | expect_output(show(bimap("integer", "numeric"))) 33 | }) 34 | 35 | 36 | test_that("bimap insert throws when inserting false values", { 37 | b <- bimap("numeric", "numeric") 38 | b <- insert(b, c(1, 2), c(4, 5)) 39 | expect_error(insert(b, c("s", "s"), c(4, 5))) 40 | }) 41 | 42 | 43 | test_that("bimap get throws when getting false values", { 44 | b <- bimap("integer", "character") 45 | expect_error(at(b, "s")) 46 | }) 47 | 48 | 49 | test_that("bimap insert/get methods work", { 50 | b <- bimap("numeric", "numeric") 51 | b <- insert(b, c(1, 2), c(4, 5)) 52 | expect_equal(at(b, 1), 4) 53 | }) 54 | 55 | 56 | test_that("bimap insert/get methods work with which argument", { 57 | b <- bimap("numeric", "numeric") 58 | b <- insert(b, c(1, 2), c(4, 5)) 59 | expect_equal(at(b, 1, "values"), 4) 60 | }) 61 | 62 | 63 | test_that("bimap insert/get methods work with which argument", { 64 | b <- bimap("numeric", "numeric") 65 | b <- insert(b, c(1, 2), c(4, 5)) 66 | expect_equal(at(b, 4, "keys"), 1) 67 | }) 68 | 69 | 70 | test_that("bimap peek works", { 71 | h <- bimap("integer", "character") 72 | m <- letters[1:2] 73 | r <- 1:2 74 | h <- insert(h, r, m) 75 | expect_output(show(peek(h))) 76 | }) 77 | 78 | 79 | test_that("bimap size works", { 80 | h <- bimap("character", "character") 81 | h <- insert(h, letters[1:2], letters[1:2]) 82 | expect_true(size(h) == 2) 83 | }) 84 | 85 | 86 | test_that("bimap retrieves correct values", { 87 | h <- bimap("integer", "numeric") 88 | h <- insert(h, 1:2, c(4, 5)) 89 | expect_true(all(sort(values(h)) %in% c(4, 5))) 90 | }) 91 | 92 | 93 | test_that("bimap replace works", { 94 | b <- bimap("integer", "numeric") 95 | b <- insert(b, 1:2, c(4, 2)) 96 | expect_equal(at(b, 1L), 4) 97 | expect_equal(at(b, 2L, "values"), 2) 98 | b <- insert(b, 1:2, c(1, 2)) 99 | expect_equal(at(b, 1L), 1) 100 | expect_equal(at(b, 2L), 2) 101 | }) 102 | 103 | test_that("erase values work", { 104 | b <- bimap("integer", "numeric") 105 | b <- insert(b, 1:2, c(4, 2)) 106 | b <- erase(b, value = 4) 107 | expect_error(at(g, 1L)) 108 | }) 109 | -------------------------------------------------------------------------------- /tests/testthat/tests_map_hashmap.R: -------------------------------------------------------------------------------- 1 | # datastructures: Implementation of core datastructures for R. 2 | # 3 | # Copyright (C) Simon Dirmeier 4 | # 5 | # This file is part of datastructures. 6 | # 7 | # datastructures is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # datastructures is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with datastructures. If not, see . 19 | 20 | 21 | context("hashmap") 22 | 23 | 24 | test_that("hashmap replace works", { 25 | h <- hashmap("integer") 26 | h <- insert(h, 1L, 3) 27 | expect_equal(h[1L][[1]], 3) 28 | h <- insert(h, 2L, list(a = 1)) 29 | h <- insert(h, 1L, 2) 30 | expect_equal(h[1L][[1]], 2) 31 | }) 32 | -------------------------------------------------------------------------------- /tests/testthat/tests_map_multimap.R: -------------------------------------------------------------------------------- 1 | # datastructures: Implementation of core datastructures for R. 2 | # 3 | # Copyright (C) Simon Dirmeier 4 | # 5 | # This file is part of datastructures. 6 | # 7 | # datastructures is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # datastructures is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with datastructures. If not, see . 19 | 20 | 21 | context("multimap") 22 | 23 | 24 | test_that("multimap can store same keys", { 25 | h <- multimap("character") 26 | h <- insert(h, letters, seq(letters)) 27 | h <- insert(h, letters, seq(letters)) 28 | expect_equal(length(at(h, "a")), 2) 29 | expect_equal(unlist(at(h, "a")), c(1L, 1L)) 30 | }) 31 | 32 | test_that("multimap can store same keys", { 33 | h <- multimap("character") 34 | h <- insert(h, letters, seq(letters)) 35 | h <- insert(h, letters, seq(letters)) 36 | expect_equal(length(at(h, "a")), 2) 37 | expect_equal(unlist(at(h, "a")), c(1L, 1L)) 38 | }) 39 | 40 | test_that("multimap erase works", { 41 | h <- multimap() 42 | h <- insert(h, letters, letters) 43 | h <- erase(h, letters[1]) 44 | expect_true(size(h) == 25) 45 | expect_error(h[letters[1]]) 46 | }) 47 | 48 | test_that("multimap does not overwrite", { 49 | h <- multimap() 50 | h <- insert(h, letters[1], "a") 51 | h <- insert(h, letters[1], "b") 52 | h <- insert(h, letters[1], "a") 53 | expect_equal(size(h), 3) 54 | expect_equal(length(h[letters[1]]), 3) 55 | }) 56 | -------------------------------------------------------------------------------- /tests/testthat/tests_map_unordered_map.R: -------------------------------------------------------------------------------- 1 | # datastructures: Implementation of core datastructures for R. 2 | # 3 | # Copyright (C) Simon Dirmeier 4 | # 5 | # This file is part of datastructures. 6 | # 7 | # datastructures is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # datastructures is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with datastructures. If not, see . 19 | 20 | 21 | context("unordered map") 22 | 23 | 24 | maps <- c(hashmap, multimap) 25 | 26 | 27 | test_that("map shows", { 28 | expect_output(show(hashmap("numeric"))) 29 | expect_output(show(multimap("character"))) 30 | }) 31 | 32 | 33 | test_that("unordered map insert throws when inserting false values", { 34 | for (cm in maps) 35 | { 36 | h <- cm("numeric") 37 | h <- insert(h, c(1, 2), c(4, 5)) 38 | expect_error(insert(h, c("s", "s"), c(4, 5))) 39 | } 40 | }) 41 | 42 | 43 | test_that("unordered map retrieves correct values", { 44 | for (cm in maps) 45 | { 46 | h <- cm("numeric") 47 | h <- insert(h, c(1, 2), c(4, 5)) 48 | expect_true(all(sort(unlist(values(h))) %in% c(4, 5))) 49 | } 50 | }) 51 | 52 | 53 | test_that("unordered map get throws when getting false values", { 54 | for (m in maps) 55 | { 56 | h <- m("numeric") 57 | h <- insert(h, c(1, 2), c(4, 5)) 58 | expect_error(at(h, "s")) 59 | } 60 | }) 61 | 62 | 63 | test_that("unordered map insert/get methods work", { 64 | for (m in maps) 65 | { 66 | h <- m("numeric") 67 | h <- insert(h, c(1, 2), c(4, 5)) 68 | expect_equal(at(h, 1)[[1]], 4) 69 | } 70 | }) 71 | 72 | 73 | test_that("unordered map insert list works", { 74 | for (cm in maps) 75 | { 76 | h <- cm("numeric") 77 | m <- as.list(letters[1:2]) 78 | r <- c(1, 2) 79 | h <- insert(h, r, m) 80 | expect_equal(at(h, 2)[[1]], m[[2]]) 81 | } 82 | }) 83 | 84 | 85 | test_that("unordered map insert vector works", { 86 | for (cm in maps) 87 | { 88 | h <- cm("numeric") 89 | m <- letters[1:2] 90 | r <- c(1, 2) 91 | h <- insert(h, r, m) 92 | expect_equal(at(h, 2)[[1]], m[2]) 93 | } 94 | }) 95 | 96 | test_that("unordered map can insert multiple values", { 97 | for (cm in maps) 98 | { 99 | h <- cm("character") 100 | 101 | h <- insert(h, "a", data.frame(A = rnorm(10))) 102 | h[letters[2:3]] <- rnorm(2) 103 | h <- insert(h, c("hallo", "hello"), list(a = "A", b = list(a = 3))) 104 | 105 | expect_true(is.data.frame(at(h, "a")[[1]])) 106 | expect_true(is.numeric(at(h, "b")[[1]])) 107 | expect_true(at(h, "hallo")[[1]] == "A") 108 | expect_true(is.list(at(h, "hello")[[1]])) 109 | } 110 | }) 111 | 112 | 113 | test_that("unordered map peek works", { 114 | for (cm in maps) 115 | { 116 | h <- cm("numeric") 117 | m <- as.list(letters[1:2]) 118 | r <- c(1, 2) 119 | h <- insert(h, r, m) 120 | expect_output(show(peek(h))) 121 | } 122 | }) 123 | 124 | 125 | test_that("unordered map size works", { 126 | for (cm in maps) 127 | { 128 | h <- cm("numeric") 129 | m <- as.list(letters[1:2]) 130 | r <- c(1, 2) 131 | h <- insert(h, r, m) 132 | expect_equal(size(h), 2) 133 | } 134 | }) 135 | 136 | 137 | test_that("unordered map erases", { 138 | for (m in maps) { 139 | h <- m("integer") 140 | h <- insert(h, 1L, environment()) 141 | h <- insert(h, 2L, list(a = 1)) 142 | h <- insert(h, 3L, 2) 143 | h <- erase(h, 1L) 144 | expect_error(h[letters[1]]) 145 | } 146 | }) 147 | --------------------------------------------------------------------------------