Latest News
Đẩy log exception của spring boot lên elasticsearch
  • About
  • EmEditor
  • Register Google Adsense

Love Coding

Note anything I want

  • Home
  • Web Development
    • HTML
    • Javascript
    • jQuery
    • CSS
    • PHP
    • ASP
    • JSP
    • Fix Bug
  • Other Development
    • Desktop Java
    • Mobile J2ME
    • VBS
  • Operating System
    • Windows
    • Linux
  • Database
    • MySQL
    • Oracle
  • Software
Home » HTML » Love Coding: HashSet implementation in Javascript

HashSet implementation in Javascript

HuyPV
Add Comment
HTML
Thursday, April 8, 2010
<script>
function HashSet() {
    this._arr = new Array();
}

HashSet.prototype.add = function(e) {
    var arr = this._arr;
    var i = arr.indexOf(e);
    if (i == -1) arr.push(e);
}

HashSet.prototype.get = function(i) {
    return this._arr[i];
}

HashSet.prototype.size = function(i) {
    return this._arr.length;
}

HashSet.prototype.remove = function(e) {
    var arr =this._arr;
    var i = arr.indexOf(e);
    if (i != -1) arr.splice(i, 1);
}

HashSet.prototype.toString = function() {
    return this._arr.join(',');
}

/*
var startTime = new Date();
var set = new HashSet();
for(i = 0; i < 10004; i++) { //it took 3 seconds to do; with 100000 elements, my browser hangs
    set.add('Index ' + i); // value also effect to time complete task
}
set.add('One');
set.add('Two');
set.add('Three');
set.add('Four');
set.add('Five');

set.add('One');//will be ignored
set.add('One');//will be ignored

set.remove('Five');
var finishTime = new Date();
document.write(startTime + '<hr />' + set.size()  + ' distinct eles<hr />' + finishTime);
document.close();
*/


var a = [1, 2, 3, 4, 5];

function MyIntSet(n) {
    this.N = n;
    this._partitions = new Array();
    var i;
    for (i = 0; i < n; i++) {
        this._partitions[i] = new HashSet();
    }
}

MyIntSet.prototype.add = function(e) {
    // e Object - y = f(e) - y Int
    // assume that e is alreay Int
    var iP = e % this.N;
   
    var set = this._partitions[iP];
    set.add(e);
}

MyIntSet.prototype.get = function(i) {
    return -1;
}

MyIntSet.prototype.size = function(i) {
    var r = 0;
    for (i = 0; i < this.N; i++) {
        r += this._partitions[i].size();
    }
    return r;
}

MyIntSet.prototype.remove = function(e) {
    var iP = e % this.N;
   
    var set = this._partitions[iP];
    set.remove(e);
}

MyIntSet.prototype.toString = function() {
    var r = '';
    for (i = 0; i < this.N; i++) {
        var pi = this._partitions[i].toString();
        if (pi != '') r = r + pi + ',';
    }
    if (r != '') r = r.substring(0, r.length - 1);
    return r;
}


var startTime = new Date();
var xset = new MyIntSet(5000);
for(i = 0; i < 1000000; i++) {
    xset.add(i);
    xset.add(i); // will be ignored
}
document.write(startTime + '<hr />' + xset.size() + ' elements<hr />' +  new Date());
document.close();


/*
var arr = new Array();
for (i = 0; i < 5000000; i++) {
    arr.push(i);
}
document.write(startTime + '<hr />' + arr.length + ' elements<hr />' +  new Date());
document.close();
*/
</script>
Tweet
HashSet implementation in Javascript Title: HashSet implementation in Javascript
Description: <script> function HashSet() {     this._arr = new Array(); } HashSet.prototype.add = function(e) {     var arr = this._arr;    ... ...
Rating: 4

3 comments :

  1. AnonymousAugust 22, 2012 at 3:29 AM

    DO NOT USE THIS. This is NOT a HashSet. A HashSet needs to use a Hashtable to store it's key-value pairs (thus the name). This is a wrapper around an array and will have poor performance (linear as opposed to constant) as the number of items stored in it increase.

    ReplyDelete
    Replies
      Reply
  2. Jeremy SFebruary 21, 2013 at 12:58 PM

    Isn't a hashset also supposed to ensure unique values? You could do this with an internal key-value object instead of an Array, and just use blank values.

    ReplyDelete
    Replies
      Reply
  3. Jeremy SFebruary 21, 2013 at 1:00 PM

    Check out http://www.mojavelinux.com/articles/javascript_hashes.html instead

    ReplyDelete
    Replies
      Reply
Add comment
Load more...

Newer Post Older Post Home
Subscribe to: Post Comments ( Atom )
Quảng cáo

Popular Posts

  • VBS - Upload file HTTP Post
    Source: http://www.ericphelps.com/scripting/samples/Reference/Web/HTTP_POST.txt   Sub Upload(strUploadUrl, strFilePath, strFileField, strD...
  • Add other collapse div to your forum
    Collapse <div style="height: 16px; padding-right: 4px; font-weight: bold;" class="blockhead"> <span style=...
  • Use the YouTube API with PHP
    Process and integrate data from YouTube into your PHP application with PHP's SimpleXML extension Summary:   The YouTube vide...
  • Check Laravel version
    Đối tác bảo đang code và dùng Laravel, giờ muốn biết version Laravel tương ứng là bao nhiêu để xem mà code theo. Làm sao check? Cách 1: Sử...
  • Trong laravel thì queue:work với queue:listen khác nhau thế nào?
    Trong laravel thì queue:work với queue:listen đều chạy jobs trong queue. Vậy 2 cái này cũng phải có gì đó khác nhau chứ, nếu ko thì nó là ...
  • Hàm chuyển từ ngày dương lịch sang âm lịch (PHP)
    <?php function INT($d) {     return floor($d); } function jdFromDate($dd, $mm, $yy) {     $a = INT((14 - $mm) / 12);     $y = $yy + 4800 ...
  • Character Set và Collation trong MySQL là gì? Tạo DB thì để UTF8_GENERAL_CI hay UTF8_BIN
    Character Set là một tập các ký tự và dạng số hóa của các ký tự đó Collation là một tập các luật để so sánh các xâu được sinh ra từ các ký ...
  • git checkout file from other branch
    Đang làm trên nhánh dev. Muốn lấy 1 file, ví dụ: xxx/helper.php ở trên nhánh master về dev thì làm thế nào? Cách 1: nông dân nhiều bước nh...
  • mysql_error: Undeclared variable: INF
    mysql_error: Undeclared variable: INF PHP Code: $limit = 10; $page = isset($_GET['p']) ? $_GET['p'] : 1; $offset = ($pa...
  • Download media files (video, audio) from VnExpress.Net
    Download media files (video, audio) from VnExpress.Net Example: http://vnexpress.net/GL/Vi-tinh/Giai-tri/2010/02/3BA18A0E/page_4.asp htt...
Back to top!
Copyright 2010 Love Coding - All Rights Reserved Design by Newbie_PC - Powered by Blogger