From d107b36684945f56589a13466f44be78d74d497d Mon Sep 17 00:00:00 2001 From: Philip Sterne Date: Wed, 20 Dec 2017 15:43:08 +0200 Subject: [PATCH 1/3] Suggestions? --- README | 70 ------------------------------------ README.md | 10 +++--- segment_tree/operations.py | 4 +-- segment_tree/segment_tree.py | 2 +- setup.py | 2 +- 5 files changed, 9 insertions(+), 79 deletions(-) delete mode 100644 README diff --git a/README b/README deleted file mode 100644 index abb2557..0000000 --- a/README +++ /dev/null @@ -1,70 +0,0 @@ -Segment Tree with range operations -================================== - -.. figure:: https://img.shields.io/badge/license-MIT-blue.svg - :alt: LicenseLink - -This is a general implementation of a segment tree for Python 3. - -- Semigroup range operations in O(logN) time -- Built-in support for ``max``, ``min``, ``sum`` operations -- Extensible to support more semigroup operations -- Limited support for multidimensional trees -- Python 2 is not currently supported - -Installation -============ - -``pip3 install segment-tree`` - -Segment Tree (one-dimensional) -============================== - -Basic usage ------------ - -.. code:: python - - - from segment_tree import * - array = [3, 1, 5, 3, 13, 7, 2, 7, 2] - tree = SegmentTree(array) - - t.query(1, 3, "sum") # 9 - t.update(0, 10) # [10, 1, 5, 3, 13, 7, 2, 7, 2] - t.query(0, 8, "min") # 0 - t.update(2, -1) # [10, 1, -1, 3, 13, 7, 2, 0, 2] - t.query(0, 2, "min") # -1 - -Updates on ranges ------------------ - -.. code:: python - - from segment_tree import * - array = [1, 2, 3, 4, 5] - t = SegmentTree(array) - t.update_range(0, 2, 6) # 6 6 6 4 5 - t.update_range(1, 4, 2) # 6 2 2 2 2 - t.query(0, 3, "min") # 2 - -Multidimensional Segment Tree (alpha version, might have bugs) -============================================================== - -Basic usage ------------ - -.. code:: python - - from segment_tree import * - a = [[[1, 2], [1, 3]], [[-1, -2], [-1, -3]]] - tree = MultidimensionalSegmentTree(a) - - tree.query([(0, 1), (0, 0), (0, 0)], max) # 1 - tree.query([(1, 1), (0, 1), (0, 1)], sum) # -7 - tree.query([(0, 1), (1, 1), (0, 1)], min) # -3 - -Tests -===== - -Execute ``python3 setup.py test`` to run tests. diff --git a/README.md b/README.md index 072d1e6..9a6d4c6 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Segment Tree (one-dimensional) from segment_tree import * array = [3, 1, 5, 3, 13, 7, 2, 7, 2] - tree = SegmentTree(array) + t = SegmentTree(array) t.query(1, 3, "sum") # 9 t.update(0, 10) # [10, 1, 5, 3, 13, 7, 2, 7, 2] @@ -56,11 +56,11 @@ Multidimensional Segment Tree (alpha version, might have bugs) ```python from segment_tree import * a = [[[1, 2], [1, 3]], [[-1, -2], [-1, -3]]] - tree = MultidimensionalSegmentTree(a) + t = MultidimensionalSegmentTree(a) - tree.query([(0, 1), (0, 0), (0, 0)], max) # 1 - tree.query([(1, 1), (0, 1), (0, 1)], sum) # -7 - tree.query([(0, 1), (1, 1), (0, 1)], min) # -3 + t.query([(0, 1), (0, 0), (0, 0)], max) # 1 + t.query([(1, 1), (0, 1), (0, 1)], sum) # -7 + t.query([(0, 1), (1, 1), (0, 1)], min) # -3 ``` ## Operations and their complexity diff --git a/segment_tree/operations.py b/segment_tree/operations.py index c176406..0882829 100644 --- a/segment_tree/operations.py +++ b/segment_tree/operations.py @@ -18,5 +18,5 @@ def max_multiple(x, count): sum_operation = Operation("sum", sum, add_multiple, 0) -min_operation = Operation("min", min, min_multiple, 1e9) -max_operation = Operation("max", max, max_multiple, -1e9) +min_operation = Operation("min", min, min_multiple, +float('inf')) +max_operation = Operation("max", max, max_multiple, -float('inf')) diff --git a/segment_tree/segment_tree.py b/segment_tree/segment_tree.py index 8b6c3a1..f15c2e0 100644 --- a/segment_tree/segment_tree.py +++ b/segment_tree/segment_tree.py @@ -1,4 +1,4 @@ -from segment_tree.operations import * +from operations import * class SegmentTree: diff --git a/setup.py b/setup.py index c63e029..aa584cd 100644 --- a/setup.py +++ b/setup.py @@ -16,4 +16,4 @@ license='MIT', packages=find_packages(exclude=['tests*']), keywords='segment, tree, range, rmq, multidimensional', - python_requires='>=3') + python_requires='>=2.7') From f0eda2c64bfccecd48de20282c692ef7300157f9 Mon Sep 17 00:00:00 2001 From: Philip Sterne Date: Wed, 20 Dec 2017 15:48:04 +0200 Subject: [PATCH 2/3] Figured out README - oops --- README | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 README diff --git a/README b/README new file mode 100644 index 0000000..abb2557 --- /dev/null +++ b/README @@ -0,0 +1,70 @@ +Segment Tree with range operations +================================== + +.. figure:: https://img.shields.io/badge/license-MIT-blue.svg + :alt: LicenseLink + +This is a general implementation of a segment tree for Python 3. + +- Semigroup range operations in O(logN) time +- Built-in support for ``max``, ``min``, ``sum`` operations +- Extensible to support more semigroup operations +- Limited support for multidimensional trees +- Python 2 is not currently supported + +Installation +============ + +``pip3 install segment-tree`` + +Segment Tree (one-dimensional) +============================== + +Basic usage +----------- + +.. code:: python + + + from segment_tree import * + array = [3, 1, 5, 3, 13, 7, 2, 7, 2] + tree = SegmentTree(array) + + t.query(1, 3, "sum") # 9 + t.update(0, 10) # [10, 1, 5, 3, 13, 7, 2, 7, 2] + t.query(0, 8, "min") # 0 + t.update(2, -1) # [10, 1, -1, 3, 13, 7, 2, 0, 2] + t.query(0, 2, "min") # -1 + +Updates on ranges +----------------- + +.. code:: python + + from segment_tree import * + array = [1, 2, 3, 4, 5] + t = SegmentTree(array) + t.update_range(0, 2, 6) # 6 6 6 4 5 + t.update_range(1, 4, 2) # 6 2 2 2 2 + t.query(0, 3, "min") # 2 + +Multidimensional Segment Tree (alpha version, might have bugs) +============================================================== + +Basic usage +----------- + +.. code:: python + + from segment_tree import * + a = [[[1, 2], [1, 3]], [[-1, -2], [-1, -3]]] + tree = MultidimensionalSegmentTree(a) + + tree.query([(0, 1), (0, 0), (0, 0)], max) # 1 + tree.query([(1, 1), (0, 1), (0, 1)], sum) # -7 + tree.query([(0, 1), (1, 1), (0, 1)], min) # -3 + +Tests +===== + +Execute ``python3 setup.py test`` to run tests. From 921d973e2ad1621fc7680f4ffad83d3342ec7eb5 Mon Sep 17 00:00:00 2001 From: Philip Sterne Date: Wed, 20 Dec 2017 15:51:36 +0200 Subject: [PATCH 3/3] Correct imports for python 2 and 3 --- segment_tree/segment_tree.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/segment_tree/segment_tree.py b/segment_tree/segment_tree.py index f15c2e0..598192f 100644 --- a/segment_tree/segment_tree.py +++ b/segment_tree/segment_tree.py @@ -1,4 +1,4 @@ -from operations import * +from .operations import * class SegmentTree: