From 60dda3987b83ab556d35bef61c9ca2267c1d120a Mon Sep 17 00:00:00 2001 From: Bishop92 Date: Thu, 24 Apr 2014 17:58:02 +0200 Subject: [PATCH 01/18] Updated readme. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3e522e4..14eab0d 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ History - Persistence for data structures like array and lists. ### Current state of the art -- v 1.0.0 First release of the library. +- v 1.0 First release of the library. Support ------- From 1a1ebb290172cceba755f40d394468d034ea5522 Mon Sep 17 00:00:00 2001 From: Battistella Stefano Date: Sat, 31 Jan 2015 18:55:19 +0100 Subject: [PATCH 02/18] Update README.md --- README.md | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/README.md b/README.md index 14eab0d..e76f301 100644 --- a/README.md +++ b/README.md @@ -47,16 +47,6 @@ Documentation Follow this [link](https://github.com/Bishop92/JavaScript-Data-Structures/wiki) to read the full documentation. Follow this [link](https://rawgit.com/Bishop92/JavaScript-Data-Structures/master/doc/index.html) to read the JSDoc. -History -------- - -### Future implementations - -- Persistence for data structures like array and lists. - -### Current state of the art -- v 1.0 First release of the library. - Support ------- -Battistella Stefano, [stefano.battistella.92@gmail.com](mailto:stefano.battistella.92@gmail.com) \ No newline at end of file +Battistella Stefano, [stefano.battistella.92@gmail.com](mailto:stefano.battistella.92@gmail.com) From ff208ed5c5e04ac16dd256b314b73f6b4321534c Mon Sep 17 00:00:00 2001 From: Battistella Stefano Date: Sun, 1 Feb 2015 03:28:33 +0100 Subject: [PATCH 03/18] Added license --- LICENSE.md | 201 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100644 LICENSE.md diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..5c304d1 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,201 @@ +Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright {yyyy} {name of copyright owner} + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. From 48c8aa9d28b810ab98f6d7f194d59b22f6e5926b Mon Sep 17 00:00:00 2001 From: Magnus Pedersen Date: Sun, 1 Feb 2015 13:01:14 +0100 Subject: [PATCH 04/18] Update CircularBuffer.html --- doc/symbols/CircularBuffer.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/symbols/CircularBuffer.html b/doc/symbols/CircularBuffer.html index beed725..3e8651b 100644 --- a/doc/symbols/CircularBuffer.html +++ b/doc/symbols/CircularBuffer.html @@ -308,7 +308,7 @@

-
Is false if buffer is full, false otherwise.
+
Is true if buffer is full, false otherwise.
From 4bff47df953ef68d44f111507dffb16c5516054f Mon Sep 17 00:00:00 2001 From: Battistella Stefano Date: Sun, 1 Feb 2015 18:42:06 +0100 Subject: [PATCH 05/18] Added Range for build arrays --- .gitignore | 222 ++++++++++++++++++ Tests/DoubleLinkedList_Test.js | 9 + Tests/LinkedList_Test.js | 7 + Tests/Queue_Test.js | 7 + Tests/Range_Test.js | 31 +++ Tests/Stack_Test.js | 273 ++++++++++++----------- Tests/tester.html | 7 +- lib/DoubleLinkedList/DoubleLinkedList.js | 10 +- lib/LinkedList/LinkedList.js | 10 +- lib/PriorityQueue/PriorityQueue.js | 3 +- lib/Queue/Queue.js | 9 +- lib/Range/Range.js | 21 ++ lib/Stack/Stack.js | 9 +- 13 files changed, 473 insertions(+), 145 deletions(-) create mode 100644 .gitignore create mode 100644 Tests/Range_Test.js create mode 100644 lib/Range/Range.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0053193 --- /dev/null +++ b/.gitignore @@ -0,0 +1,222 @@ +################# +## IntelliJIdea +################# + +.idea + +################# +## Eclipse +################# + +*.pydevproject +.project +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.classpath +.settings/ +.loadpath + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# CDT-specific +.cproject + +# PDT-specific +.buildpath + + +################# +## Visual Studio +################# + +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.sln.docstates + +# Build results + +[Dd]ebug/ +[Rr]elease/ +x64/ +build/ +[Bb]in/ +[Oo]bj/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +*_i.c +*_p.c +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.log +*.scc + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf +*.cachefile + +# Visual Studio profiler +*.psess +*.vsp +*.vspx + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +*.ncrunch* +.*crunch*.local.xml + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.Publish.xml +*.pubxml +*.publishproj + +# NuGet Packages Directory +## TODO: If you have NuGet Package Restore enabled, uncomment the next line +#packages/ + +# Windows Azure Build Output +csx +*.build.csdef + +# Windows Store app package directory +AppPackages/ + +# Others +sql/ +*.Cache +ClientBin/ +[Ss]tyle[Cc]op.* +~$* +*~ +*.dbmdl +*.[Pp]ublish.xml +*.pfx +*.publishsettings + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file to a newer +# Visual Studio version. Backup files are not needed, because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +App_Data/*.mdf +App_Data/*.ldf + +############# +## Windows detritus +############# + +# Windows image file caches +Thumbs.db +ehthumbs.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Mac crap +.DS_Store + + +############# +## Python +############# + +*.py[cod] + +# Packages +*.egg +*.egg-info +dist/ +build/ +eggs/ +parts/ +var/ +sdist/ +develop-eggs/ +.installed.cfg + +# Installer logs +pip-log.txt + +# Unit test / coverage reports +.coverage +.tox + +#Translations +*.mo + +#Mr Developer +.mr.developer.cfg diff --git a/Tests/DoubleLinkedList_Test.js b/Tests/DoubleLinkedList_Test.js index 98d92e7..7e7fb45 100644 --- a/Tests/DoubleLinkedList_Test.js +++ b/Tests/DoubleLinkedList_Test.js @@ -5,6 +5,15 @@ test("DoubleLinkedList - Init test", function () { var list = new DoubleLinkedList(0, 1, 2); deepEqual(list.toArray(), [0, 1, 2], "Init list"); + list = new DoubleLinkedList(2); + deepEqual(list.toArray(), [2], "Init list"); +}); + +test("DoubleLinkedList - Init range test", function () { + var list = new DoubleLinkedList(Range(0, 2)); + deepEqual(list.toArray(), [0, 1, 2], "Init list"); + list = new DoubleLinkedList(Range(2, -2, -0.5)); + deepEqual(list.toArray(), [2, 1.5, 1, 0.5, 0, -0.5, -1, -1.5, -2], "Init list"); }); test("DoubleLinkedList - Push test", function () { diff --git a/Tests/LinkedList_Test.js b/Tests/LinkedList_Test.js index f75a0d8..dfcd8fb 100644 --- a/Tests/LinkedList_Test.js +++ b/Tests/LinkedList_Test.js @@ -5,6 +5,13 @@ test("LinkedList - Init test", function () { var list = new LinkedList(0, 1, 2); deepEqual(list.toArray(), [0, 1, 2], "Init list"); + list = new LinkedList(0); + deepEqual(list.toArray(), [0], "Init list"); +}); + +test("LinkedList - Init test with range", function () { + var list = new LinkedList(new Range(0, 5)); + deepEqual(list.toArray(), [0, 1, 2, 3, 4, 5], "Init list with range"); }); test("LinkedList - Push test", function () { diff --git a/Tests/Queue_Test.js b/Tests/Queue_Test.js index 32b875d..1081ded 100644 --- a/Tests/Queue_Test.js +++ b/Tests/Queue_Test.js @@ -5,6 +5,13 @@ test("Queue - Init test", function () { var queue = new Queue(0, 2, 4, 6); deepEqual(queue.multiDequeue(4), [0, 2, 4, 6], "Initializing"); + queue = new Queue(0); + deepEqual(queue.multiDequeue(4), [0], "Initializing"); +}); + +test("Queue - Init range test", function () { + var queue = new Queue(Range(0, 6, 2)); + deepEqual(queue.multiDequeue(4), [0, 2, 4, 6], "Initializing"); }); test("Queue - Enqueue test", function () { diff --git a/Tests/Range_Test.js b/Tests/Range_Test.js new file mode 100644 index 0000000..fc201ff --- /dev/null +++ b/Tests/Range_Test.js @@ -0,0 +1,31 @@ +/** + * Created by Stefano on 06/04/14. + */ + +test("Range - Create linear test", function () { + var range = Range(0, 5); + for (var j = 0; j < range.length; j++) { + deepEqual(range[j], j, "Check range values"); + } +}); + +test("Range - Create inverse test", function () { + var range = Range(5, 0, -1); + for (var j = 0; j < range.length; j++) { + deepEqual(range[j], 5 - j, "Check range values"); + } +}); + +test("Range - Create step test", function () { + var range = Range(0, 15, 4); + for (var j = 0; j < range.length; j++) { + deepEqual(range[j], j * 4, "Check range values"); + } +}); + +test("Range - Create inverse step test", function () { + var range = Range(15, 4, -2); + for (var j = 0; j < range.length; j++) { + deepEqual(range[j], 15 - j * 2, "Check range values"); + } +}); \ No newline at end of file diff --git a/Tests/Stack_Test.js b/Tests/Stack_Test.js index bc2fcac..57cae5d 100644 --- a/Tests/Stack_Test.js +++ b/Tests/Stack_Test.js @@ -3,189 +3,196 @@ */ test("Stack - Init test", function () { - var stack = new Stack(0, 2, 4, 6); - deepEqual(stack.multiPop(4), [6, 4, 2, 0], "Initializing"); + var stack = new Stack(0, 2, 4, 6); + deepEqual(stack.multiPop(4), [6, 4, 2, 0], "Initializing"); + stack = new Stack(0); + deepEqual(stack.multiPop(4), [0], "Initializing"); +}); + +test("Stack - Init range test", function () { + var stack = new Stack(Range(0, 6, 2)); + deepEqual(stack.multiPop(4), [6, 4, 2, 0], "Initializing"); }); test("Stack - Push test", function () { - var stack = new Stack(); - stack.push(0); - deepEqual(stack.getItem(0), 0, "Push 0"); - stack.push(2); - deepEqual(stack.getItem(0), 2, "Push 2"); - deepEqual(stack.getItem(1), 0, "Push 2"); + var stack = new Stack(); + stack.push(0); + deepEqual(stack.getItem(0), 0, "Push 0"); + stack.push(2); + deepEqual(stack.getItem(0), 2, "Push 2"); + deepEqual(stack.getItem(1), 0, "Push 2"); }); test("Stack - Pop test", function () { - var stack = new Stack(); - stack.push(0); - stack.push(2); - deepEqual(stack.pop(), 2, "Pop"); - deepEqual(stack.pop(), 0, "Check length"); - deepEqual(stack.pop(), undefined, "Check length if too much pop"); + var stack = new Stack(); + stack.push(0); + stack.push(2); + deepEqual(stack.pop(), 2, "Pop"); + deepEqual(stack.pop(), 0, "Check length"); + deepEqual(stack.pop(), undefined, "Check length if too much pop"); }); test("Stack - MultiPop test", function () { - var stack = new Stack(); - stack.push(0); - stack.push(2); - deepEqual(stack.multiPop(1), [2], "MultiPop 1 time"); - stack.push(4); - stack.push(5); - stack.push(8); - deepEqual(stack.multiPop(7), [8, 5, 4, 0], "MultiPop 7 times"); - deepEqual(stack.multiPop(1), [], "MultiPop 1 time with stack empty"); + var stack = new Stack(); + stack.push(0); + stack.push(2); + deepEqual(stack.multiPop(1), [2], "MultiPop 1 time"); + stack.push(4); + stack.push(5); + stack.push(8); + deepEqual(stack.multiPop(7), [8, 5, 4, 0], "MultiPop 7 times"); + deepEqual(stack.multiPop(1), [], "MultiPop 1 time with stack empty"); }); test("Stack - Iterator test", function () { - var stack = new Stack(); - for (var i = 0; i < 10; i++) - stack.push(i); - var j = 9; - var it = stack.getIterator(); - for (it.first(); !it.isDone(); it.next(), j--) - deepEqual(it.getItem(), j, "Get next item " + (10 - j)); - j++; - for (it.last(); !it.isDone(); it.previous(), j++) - deepEqual(it.getItem(), j, "Get previous item " + (10 - j)); + var stack = new Stack(); + for (var i = 0; i < 10; i++) + stack.push(i); + var j = 9; + var it = stack.getIterator(); + for (it.first(); !it.isDone(); it.next(), j--) + deepEqual(it.getItem(), j, "Get next item " + (10 - j)); + j++; + for (it.last(); !it.isDone(); it.previous(), j++) + deepEqual(it.getItem(), j, "Get previous item " + (10 - j)); }); test("Stack - Get length test", function () { - var stack = new Stack(); - deepEqual(stack.getLength(), 0, "Length 0"); - stack.push(0); - deepEqual(stack.getLength(), 1, "Length 1"); - stack.push(2); - deepEqual(stack.getLength(), 2, "Length 2"); - stack.pop(); - stack.pop(); - deepEqual(stack.getLength(), 0, "Length 0"); + var stack = new Stack(); + deepEqual(stack.getLength(), 0, "Length 0"); + stack.push(0); + deepEqual(stack.getLength(), 1, "Length 1"); + stack.push(2); + deepEqual(stack.getLength(), 2, "Length 2"); + stack.pop(); + stack.pop(); + deepEqual(stack.getLength(), 0, "Length 0"); }); test("Stack - Peek test", function () { - var stack = new Stack(); - stack.push(0); - deepEqual(stack.peek(), 0, "Peek 0"); - stack.push(2); - deepEqual(stack.peek(), 2, "Peek 2"); + var stack = new Stack(); + stack.push(0); + deepEqual(stack.peek(), 0, "Peek 0"); + stack.push(2); + deepEqual(stack.peek(), 2, "Peek 2"); }); test("Stack - Clear test", function () { - var stack = new Stack(); - stack.push(0); - stack.push(2); - stack.clear(); - deepEqual(stack.isEmpty(), true, "Clear stack"); + var stack = new Stack(); + stack.push(0); + stack.push(2); + stack.clear(); + deepEqual(stack.isEmpty(), true, "Clear stack"); }); test("Stack - Is empty test", function () { - var stack = new Stack(); - stack.push(0); - stack.push(2); - deepEqual(stack.isEmpty(), false, "Is empty"); - stack.clear(); - deepEqual(stack.isEmpty(), true, "Is empty"); + var stack = new Stack(); + stack.push(0); + stack.push(2); + deepEqual(stack.isEmpty(), false, "Is empty"); + stack.clear(); + deepEqual(stack.isEmpty(), true, "Is empty"); }); test("Stack - contains", function () { - var stack = new Stack(); - stack.push(0); - stack.push(2); - deepEqual(stack.contains(0), true, "Contains 0"); - deepEqual(stack.contains(2), true, "Contains 2"); - deepEqual(stack.contains(1), false, "Not contains 1"); - var callback = function (item) { - return item > 0; - }; - deepEqual(stack.contains(null, callback), true, "Contains a value > 0"); - callback = function (item) { - return item < 0; - }; - deepEqual(stack.contains(null, callback), false, "Contains a value < 0"); + var stack = new Stack(); + stack.push(0); + stack.push(2); + deepEqual(stack.contains(0), true, "Contains 0"); + deepEqual(stack.contains(2), true, "Contains 2"); + deepEqual(stack.contains(1), false, "Not contains 1"); + var callback = function (item) { + return item > 0; + }; + deepEqual(stack.contains(null, callback), true, "Contains a value > 0"); + callback = function (item) { + return item < 0; + }; + deepEqual(stack.contains(null, callback), false, "Contains a value < 0"); }); test("Stack - execute", function () { - var stack = new Stack(); - stack.push(0); - stack.push(2); - var callback = function (item) { - return item * 2; - }; - stack.execute(callback); - deepEqual(stack.getItem(0), 4, "Execute for item 1"); - deepEqual(stack.getItem(1), 0, "Execute for item 0"); + var stack = new Stack(); + stack.push(0); + stack.push(2); + var callback = function (item) { + return item * 2; + }; + stack.execute(callback); + deepEqual(stack.getItem(0), 4, "Execute for item 1"); + deepEqual(stack.getItem(1), 0, "Execute for item 0"); }); test("Stack - Index of test", function () { - var stack = new Stack(); - for (var i = 0; i < 10; i++) - stack.push(i); - var callback = function (item) { - return !(item % 2) && item > 5; - }; - deepEqual(stack.indexOf(0), 0, "Index of 0"); - deepEqual(stack.indexOf(15), -1, "Index of 15"); - deepEqual(stack.indexOf(5), 5, "Index of 5"); - deepEqual(stack.indexOf(null, callback), 8, "Index of the first even number greater than 5"); + var stack = new Stack(); + for (var i = 0; i < 10; i++) + stack.push(i); + var callback = function (item) { + return !(item % 2) && item > 5; + }; + deepEqual(stack.indexOf(0), 0, "Index of 0"); + deepEqual(stack.indexOf(15), -1, "Index of 15"); + deepEqual(stack.indexOf(5), 5, "Index of 5"); + deepEqual(stack.indexOf(null, callback), 8, "Index of the first even number greater than 5"); }); test("Stack - Last index of test", function () { - var stack = new Stack(); - for (var i = 0; i < 10; i++) - stack.push(i); - var callback = function (item) { - return !(item % 2) && item > 5; - }; - deepEqual(stack.lastIndexOf(0), 0, "Last index of 0"); - deepEqual(stack.lastIndexOf(15), -1, "Last index of 15"); - deepEqual(stack.lastIndexOf(5), 5, "Last index of 5"); - deepEqual(stack.lastIndexOf(null, callback), 6, "Index of the last even number greater than 5"); + var stack = new Stack(); + for (var i = 0; i < 10; i++) + stack.push(i); + var callback = function (item) { + return !(item % 2) && item > 5; + }; + deepEqual(stack.lastIndexOf(0), 0, "Last index of 0"); + deepEqual(stack.lastIndexOf(15), -1, "Last index of 15"); + deepEqual(stack.lastIndexOf(5), 5, "Last index of 5"); + deepEqual(stack.lastIndexOf(null, callback), 6, "Index of the last even number greater than 5"); }); test("Stack - Indexes of test", function () { - var stack = new Stack(); - for (var i = 0; i < 30; i++) - stack.push(i % 10); - var callback = function (item) { - return !(item % 2) && item > 5; - }; - deepEqual(stack.allIndexesOf(0), [20, 10, 0], "Indexes of 0"); - deepEqual(stack.allIndexesOf(15), [], "Indexes of 15"); - deepEqual(stack.allIndexesOf(5), [25, 15, 5], "Indexes of 5"); - deepEqual(stack.allIndexesOf(null, callback), [28, 26, 18, 16, 8, 6], "Indexes of the even numbers greater than 5"); + var stack = new Stack(); + for (var i = 0; i < 30; i++) + stack.push(i % 10); + var callback = function (item) { + return !(item % 2) && item > 5; + }; + deepEqual(stack.allIndexesOf(0), [20, 10, 0], "Indexes of 0"); + deepEqual(stack.allIndexesOf(15), [], "Indexes of 15"); + deepEqual(stack.allIndexesOf(5), [25, 15, 5], "Indexes of 5"); + deepEqual(stack.allIndexesOf(null, callback), [28, 26, 18, 16, 8, 6], "Indexes of the even numbers greater than 5"); }); test("Stack - Clone test", function () { - var stack = new Stack(); - for (var i = 0; i < 10; i++) - stack.push(i); - var clone = stack.clone(); - var it = clone.getIterator(); - var j = 9; - for (it.first(); !it.isDone(); it.next(), j--) - deepEqual(it.getItem(), j, "Clone of the list"); + var stack = new Stack(); + for (var i = 0; i < 10; i++) + stack.push(i); + var clone = stack.clone(); + var it = clone.getIterator(); + var j = 9; + for (it.first(); !it.isDone(); it.next(), j--) + deepEqual(it.getItem(), j, "Clone of the list"); }); test("Stack - Clone distinct test", function () { - var stack = new Stack(); - for (var i = 0; i < 20; i++) - stack.push(i % 10); - var clone = stack.cloneDistinct(); - deepEqual(clone.allIndexesOf(2), [2], "Clone of the list"); + var stack = new Stack(); + for (var i = 0; i < 20; i++) + stack.push(i % 10); + var clone = stack.cloneDistinct(); + deepEqual(clone.allIndexesOf(2), [2], "Clone of the list"); }); test("Stack - Filter test", function () { - var stack = new Stack(); - const length = 100; + var stack = new Stack(); + const length = 100; - for (var i = 0; i < length; i++) - stack.push(i); + for (var i = 0; i < length; i++) + stack.push(i); - var result = stack.filter(function (item) { - return 1 - item % 2; - }); + var result = stack.filter(function (item) { + return 1 - item % 2; + }); - deepEqual(result[0], 98, "Filter of the even values"); - deepEqual(result[result.length - 1], 0, "Filter on the even values"); + deepEqual(result[0], 98, "Filter of the even values"); + deepEqual(result[result.length - 1], 0, "Filter on the even values"); }); \ No newline at end of file diff --git a/Tests/tester.html b/Tests/tester.html index bc1629f..56c4cb5 100644 --- a/Tests/tester.html +++ b/Tests/tester.html @@ -3,13 +3,14 @@ QUnit basic example - +
+ @@ -37,10 +38,10 @@ - - + + diff --git a/lib/DoubleLinkedList/DoubleLinkedList.js b/lib/DoubleLinkedList/DoubleLinkedList.js index 7e2b3fc..9c70a55 100644 --- a/lib/DoubleLinkedList/DoubleLinkedList.js +++ b/lib/DoubleLinkedList/DoubleLinkedList.js @@ -49,8 +49,14 @@ function DoubleLinkedList(args) { * @type {number} */ this.length = 0; - //builds the list from the parameters of the constructor - this.fromArray(arguments); + + if(args && args.length) { + //builds the list from the range passed from the constructor + this.fromArray(args); + } else { + //builds the list from the parameters of the constructor + this.fromArray(arguments); + } } /** diff --git a/lib/LinkedList/LinkedList.js b/lib/LinkedList/LinkedList.js index e2be3ab..cc874f5 100644 --- a/lib/LinkedList/LinkedList.js +++ b/lib/LinkedList/LinkedList.js @@ -44,8 +44,14 @@ function LinkedList(args) { * @type {number} */ this.length = 0; - //builds the list from the parameters of the constructor - this.fromArray(arguments); + + if(args && args.length) { + //builds the list from the range passed from the constructor + this.fromArray(args); + } else { + //builds the list from the parameters of the constructor + this.fromArray(arguments); + } } /** diff --git a/lib/PriorityQueue/PriorityQueue.js b/lib/PriorityQueue/PriorityQueue.js index 1d80f71..373c9b4 100644 --- a/lib/PriorityQueue/PriorityQueue.js +++ b/lib/PriorityQueue/PriorityQueue.js @@ -260,4 +260,5 @@ PriorityQueue.prototype.cloneDistinct = function () { queue.items = this.items.cloneDistinct(); queue.length = queue.items.getSize(); return queue; -}; \ No newline at end of file +}; + diff --git a/lib/Queue/Queue.js b/lib/Queue/Queue.js index d2ad13e..336d041 100644 --- a/lib/Queue/Queue.js +++ b/lib/Queue/Queue.js @@ -17,8 +17,13 @@ function Queue(args) { */ this.items = []; - //builds the queue from the parameters of the constructor - this.multiEnqueue(arguments); + if(args && args.length) { + //builds the list from the range passed from the constructor + this.multiEnqueue(args); + } else { + //builds the list from the parameters of the constructor + this.multiEnqueue(arguments); + } } /** diff --git a/lib/Range/Range.js b/lib/Range/Range.js new file mode 100644 index 0000000..80a0892 --- /dev/null +++ b/lib/Range/Range.js @@ -0,0 +1,21 @@ +/** + * Created by Stefano on 01/02/2015. + */ + +/** + * Create an array containing the value of the range + * @param from {number} The inclusive start value of the range. + * @param to {number} The inclusive end value of the range. + * @param [step=1] The step to sample the values + * @return {Array} The array containing the value of the range. + */ +function Range(from, to, step) { + var range = []; + step = step || 1; + var sign = (step > 0 ? 1 : -1); + + for (var i = 0; from * sign <= to * sign; from += step, ++i) + range[i] = from; + + return range; +} \ No newline at end of file diff --git a/lib/Stack/Stack.js b/lib/Stack/Stack.js index ac21b05..6798b14 100644 --- a/lib/Stack/Stack.js +++ b/lib/Stack/Stack.js @@ -17,8 +17,13 @@ function Stack(args) { */ this.items = []; - //builds the stack from the parameters of the constructor - this.multiPush(arguments); + if(args && args.length) { + //builds the stack from the range passed from the constructor + this.multiPush(args); + } else { + //builds the stack from the parameters of the constructor + this.multiPush(arguments); + } } /** From d34b12179f023fa00cc45ecf0546662bd880eb77 Mon Sep 17 00:00:00 2001 From: Battistella Stefano Date: Sun, 1 Feb 2015 18:45:37 +0100 Subject: [PATCH 06/18] Changed reference to the wiki for the data structures --- README.md | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index e76f301..6f65164 100644 --- a/README.md +++ b/README.md @@ -10,18 +10,18 @@ the data structure that work for storing your data. Supported data structures ------------------------- -- [Stack](https://rawgit.com/Bishop92/JavaScript-Data-Structures/master/doc/symbols/Stack.html) -- [Queue](https://rawgit.com/Bishop92/JavaScript-Data-Structures/master/doc/symbols/Queue.html) -- [Priority Queue](https://rawgit.com/Bishop92/JavaScript-Data-Structures/master/doc/symbols/PriorityQueue.html) -- [Circular Buffer](https://rawgit.com/Bishop92/JavaScript-Data-Structures/master/doc/symbols/CircularBuffer.html) -- [Hash Table](https://rawgit.com/Bishop92/JavaScript-Data-Structures/master/doc/symbols/HashTable.html) -- [Linked List](https://rawgit.com/Bishop92/JavaScript-Data-Structures/master/doc/symbols/LinkedList.html) -- [Double Linked List](https://rawgit.com/Bishop92/JavaScript-Data-Structures/master/doc/symbols/DoubleLinkedList.html) -- [Binary Search Tree](https://rawgit.com/Bishop92/JavaScript-Data-Structures/master/doc/symbols/BSTree.html) -- [Red-Black Tree](https://rawgit.com/Bishop92/JavaScript-Data-Structures/master/doc/symbols/RBTree.html) -- [Red-Black Tree List](https://rawgit.com/Bishop92/JavaScript-Data-Structures/master/doc/symbols/RBTreeList.html) -- [B-Tree](https://rawgit.com/Bishop92/JavaScript-Data-Structures/master/doc/symbols/BTree.html) -- [Set](https://rawgit.com/Bishop92/JavaScript-Data-Structures/master/doc/symbols/Set.html) +- [Stack](https://github.com/Bishop92/JavaScript-Data-Structures/wiki/Stack) +- [Queue](https://github.com/Bishop92/JavaScript-Data-Structures/wiki/Queue) +- [Priority Queue](https://github.com/Bishop92/JavaScript-Data-Structures/wiki/Priority-Queue) +- [Circular Buffer](https://github.com/Bishop92/JavaScript-Data-Structures/wiki/Circular-Buffer) +- [Hash Table](https://github.com/Bishop92/JavaScript-Data-Structures/wiki/Hash-Table) +- [Linked List](https://github.com/Bishop92/JavaScript-Data-Structures/wiki/Linked-List) +- [Double Linked List](https://github.com/Bishop92/JavaScript-Data-Structures/wiki/Double-Linked-List) +- [Binary search Tree](https://github.com/Bishop92/JavaScript-Data-Structures/wiki/Binary-search-tree) +- [Red-Black Tree](https://github.com/Bishop92/JavaScript-Data-Structures/wiki/Red-Black-Tree) +- [Red-Black Tree List](https://github.com/Bishop92/JavaScript-Data-Structures/wiki/Red-Black-Tree-List) +- [B-Tree](https://github.com/Bishop92/JavaScript-Data-Structures/wiki/B-Tree) +- [Set](https://github.com/Bishop92/JavaScript-Data-Structures/wiki/Set) How to use ---------- From 2f6f509a35a926e9230d7fa15ff704d6569a9647 Mon Sep 17 00:00:00 2001 From: Battistella Stefano Date: Tue, 3 Feb 2015 00:02:57 +0100 Subject: [PATCH 07/18] Added initial trie data structure --- Tests/Trie_Test.js | 28 ++++++++++++++++++++++++++++ Tests/tester.html | 3 +++ 2 files changed, 31 insertions(+) create mode 100644 Tests/Trie_Test.js diff --git a/Tests/Trie_Test.js b/Tests/Trie_Test.js new file mode 100644 index 0000000..740aab2 --- /dev/null +++ b/Tests/Trie_Test.js @@ -0,0 +1,28 @@ +/** + * Created by Stefano on 06/04/14. + */ + +test("Trie - Insert test", function () { + var trie = new Trie(); + trie.insert("Blue"); + trie.insert("Bleu"); + var strings = []; + trie.stringsToArray(strings) + deepEqual(strings, ["Bleu","Blue"], "Insert node"); +}); + +test("Trie - Suggest test", function () { + var trie = new Trie(); + trie.insert("Blue"); + trie.insert("Boing"); + trie.insert("Yellow"); + trie.insert("Hello"); + trie.insert("He"); + trie.insert("Hola"); + deepEqual(trie.suggest(""), ["Blue","Boing", "He", "Hello", "Hola", "Yellow"], "Suggest empty string"); + deepEqual(trie.suggest("B"), ["Blue","Boing"], "Suggest B string"); + deepEqual(trie.suggest("Bo"), ["Boing"], "Suggest Bo string"); + deepEqual(trie.suggest("Ho"), ["Hola"], "Suggest Ho string"); + deepEqual(trie.suggest("A"), [], "Suggest A string"); + deepEqual(trie.suggest("Ready"), [], "Suggest Ready string"); +}); \ No newline at end of file diff --git a/Tests/tester.html b/Tests/tester.html index 56c4cb5..e79e53f 100644 --- a/Tests/tester.html +++ b/Tests/tester.html @@ -35,6 +35,8 @@ + + @@ -54,6 +56,7 @@ +