<commit_before>'use strict';

class Bag {
  constructor(first, N) {
    this.first = first || null;
    this.N = N || 0;
  }
  
  isEmpty() {
    return this.first === null;
  }
  
  size() {
    return this.N;
  }
  
  add(item) {
    var oldfirst = this.first;
    this.first = new Node();
    this.first.item = item;
    this.first.next = oldfirst;
    this.N++;
  }
  
}

class Node {
  constructor(item, next) {
    this.item = item;
    this.next = next;
  }
}

//测试
const main = (argv) => {
  var bag = new bag()
  console.log(bag.size());
  console.log(bag.isEmpty());
  bag.add(11);
  console.log(bag.size());
  console.log(bag.isEmpty());

  bag.add(22);
  console.log(bag.size());
  console.log(bag.isEmpty());

  bag.add(33);
  console.log(bag.size());
  console.log(bag.isEmpty());

  console.log(bag.first.item);
  console.log(bag.first.next.item);
  console.log(bag.first.next.next.item);
}

if (require.main === module) {
  main();
}

exports.Bag = Bag;<commit_msg>update bag data structure, add iterator<commit_after>'use strict';

class Bag {
  constructor(first, N) {
    this.first = first || null;
    this.N = N || 0;
  }

  isEmpty() {
    return this.first === null;
  }
  
  size() {
    return this.N;
  }
  
  add(item) {
    var oldfirst = this.first;
    this.first = new Node();
    this.first.item = item;
    this.first.next = oldfirst;
    this.N++;
  }
  
  //遍历器对象
  [Symbol.iterator]() { return this; }
  
  next() {
    if(this.first !== null) {
      var value = this.first.item;
      this.first = this.first.next;
      return {
        done: false,
        value: value
      }
    } else {
      return {
        done: true
      }
    }
  }
  
}

class Node {
  constructor(item, next) {
    this.item = item;
    this.next = next;
  }
}

//测试
const main = (argv) => {
  var bag = new Bag();
  bag.add(11);
  bag.add([9,8,7]);
  bag.add(22);
  bag.add({a:213});
  bag.add(33);
  for (var item of bag){
    console.log(item);
  }
}

if (require.main === module) {
  main();
}

exports.Bag = Bag;
{'fraction_non_alphanumeric': 0.18114874815905743, 'fraction_numerical': 0.009818360333824251, 'mean_word_length': 2.2712680577849116, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1407946', 'n_tokens_mistral': 798, 'n_tokens_neox': 786, 'n_words': 156}