<commit_before>/* -*- mode: c++; indent-tabs-mode: nil -*-
 *
 *  $Id: list.h,v 1.1 2006/10/09 07:07:57 <NAME> Exp $
 *
 *  Copyright 2006-2007 FLWOR Foundation.  All Rights Reserved.
 *	Author: <NAME>
 *
 */

#ifndef XQP_LIST_H
#define XQP_LIST_H

#include "xqp_exception.h"

namespace xqp {

template <class T>
class list_node
{
public:
  T data;
  list_node* next;
  list_node* prev;
  list_node() : data(T()), next(0), prev(0) {}
  list_node(T d, list_node* n, list_node* p) : data(d), next(n), prev(p) {}
	~list_node() {}
};


template <class T>
class list_iterator
{
protected:
  list_node<T>* curr;
public:
  list_iterator& operator++() { curr = curr->next; return *this; }
  T& operator*() { return curr->data; }
	list_node<T>* get_curr() { return curr; }
  list_iterator(list_node<T>* n) : curr(n) {}
	~list_iterator() {}
	bool operator!=(const list_iterator& a) { return curr!=a.curr; }
	bool operator==(const list_iterator& a) { return curr==a.curr; }
};


template <class T>
class list_reverse_iterator
{
protected:
  list_node<T>* curr;
public:
  list_reverse_iterator& operator++() { curr = curr->prev; return *this; }
  T& operator*() { return curr->data; }
	list_node<T>* get_curr() { return curr; }
  list_reverse_iterator(list_node<T>* n) : curr(n) {}
	~list_reverse_iterator() {}
	bool operator!=(const list_reverse_iterator& a) { return curr!=a.curr; }
	bool operator==(const list_reverse_iterator& a) { return curr==a.curr; }
};


template<class T> class list;
template<class T> list<T> operator*(const list<T>&, const list<T>&);	// intersection
template<class T> list<T> operator+(const list<T>&, const list<T>&);	// union


template <class T>
class list
{ 
protected:
  list_node<T>* head;	// dummy node points to first element
  list_node<T>* tail;	// dummy node	pointed to by last element
	uint32_t sz;

/*....................................................................
:                                                                
:         _______                                      _______
:   .--->|___0___|<--head                      tail-->|___0___|<-.
:   | .--|_______|                                    |_/_/_/_|  |
:   | |  |_/_/_/_|                                    |___.___|  |
:   | |next                                               |prev  |
:   | |                                                   |      |next
:   | |   _______        _______        _______        ___v___   |
:   | `->|___T___| next |___T___| next |___T___| next |___T___|  |
:   |    |_______|----->|_______|----->|_______|----->|_______|--'
:   `----|_______|<-----|_______|<-----|_______|<-----|_______| 
:    prev          prev           prev           prev
:
:......................................................................*/


public:
  list();
	~list();

public:	// list value interface
  void push_back(T const&);
  void push_front(T const&);
  T pop_front();

public: // listnode interface
	void insert_after(list_node<T>* oldlist_node, list_node<T>* newlist_node);
	void remove(list_node<T>* node);
	list_node<T>* get_head() const { return head->next; }
	list_node<T>* get_tail() const { return tail->prev; }
	uint32_t size() const { return sz; }

public:
  list_iterator<T> begin() const
    { return list_iterator<T>(head->next); }
  list_iterator<T> end() const
    { return list_iterator<T>(tail); } 
    
  list_reverse_iterator<T> rbegin() const
    { return list_reverse_iterator<T>(tail->prev); }
  list_reverse_iterator<T> rend() const
    { return list_reverse_iterator<T>(head); }
    
  T& operator[](int);

public:
	friend list operator*<>(list const&, list const&);		// intersection
	friend list operator+<>(list const&, list const&);		// union
};


template<class T>
list<T>::list()
: head(new list_node<T>()),
	tail(new list_node<T>()),
	sz(0)
{
	head->next = tail;
	head->prev = NULL;
	tail->prev = head;
	tail->next = NULL;
}


template<class T>
list<T>::~list()
{
  list_node<T>* n_p;
  list_iterator<T> it = begin();
  for (; it!=end(); ++it,delete n_p) n_p = it.get_curr();<commit_msg>Fixed memory leak in list.h (dummy head and tail nodes weren't freed)<commit_after>/* -*- mode: c++; indent-tabs-mode: nil -*-
 *
 *  $Id: list.h,v 1.1 2006/10/09 07:07:57 <NAME> Exp $
 *
 *  Copyright 2006-2007 FLWOR Foundation.  All Rights Reserved.
 *	Author: <NAME>
 *
 */

#ifndef XQP_LIST_H
#define XQP_LIST_H

#include "xqp_exception.h"

namespace xqp {

template <class T>
class list_node
{
public:
  T data;
  list_node* next;
  list_node* prev;
  list_node() : data(T()), next(0), prev(0) {}
  list_node(T d, list_node* n, list_node* p) : data(d), next(n), prev(p) {}
	~list_node() {}
};


template <class T>
class list_iterator
{
protected:
  list_node<T>* curr;
public:
  list_iterator& operator++() { curr = curr->next; return *this; }
  T& operator*() { return curr->data; }
	list_node<T>* get_curr() { return curr; }
  list_iterator(list_node<T>* n) : curr(n) {}
	~list_iterator() {}
	bool operator!=(const list_iterator& a) { return curr!=a.curr; }
	bool operator==(const list_iterator& a) { return curr==a.curr; }
};


template <class T>
class list_reverse_iterator
{
protected:
  list_node<T>* curr;
public:
  list_reverse_iterator& operator++() { curr = curr->prev; return *this; }
  T& operator*() { return curr->data; }
	list_node<T>* get_curr() { return curr; }
  list_reverse_iterator(list_node<T>* n) : curr(n) {}
	~list_reverse_iterator() {}
	bool operator!=(const list_reverse_iterator& a) { return curr!=a.curr; }
	bool operator==(const list_reverse_iterator& a) { return curr==a.curr; }
};


template<class T> class list;
template<class T> list<T> operator*(const list<T>&, const list<T>&);	// intersection
template<class T> list<T> operator+(const list<T>&, const list<T>&);	// union


template <class T>
class list
{ 
protected:
  list_node<T>* head;	// dummy node points to first element
  list_node<T>* tail;	// dummy node	pointed to by last element
	uint32_t sz;

/*....................................................................
:                                                                
:         _______                                      _______
:   .--->|___0___|<--head                      tail-->|___0___|<-.
:   | .--|_______|                                    |_/_/_/_|  |
:   | |  |_/_/_/_|                                    |___.___|  |
:   | |next                                               |prev  |
:   | |                                                   |      |next
:   | |   _______        _______        _______        ___v___   |
:   | `->|___T___| next |___T___| next |___T___| next |___T___|  |
:   |    |_______|----->|_______|----->|_______|----->|_______|--'
:   `----|_______|<-----|_______|<-----|_______|<-----|_______| 
:    prev          prev           prev           prev
:
:......................................................................*/


public:
  list();
	~list();

public:	// list value interface
  void push_back(T const&);
  void push_front(T const&);
  T pop_front();

public: // listnode interface
	void insert_after(list_node<T>* oldlist_node, list_node<T>* newlist_node);
	void remove(list_node<T>* node);
	list_node<T>* get_head() const { return head->next; }
	list_node<T>* get_tail() const { return tail->prev; }
	uint32_t size() const { return sz; }

public:
  list_iterator<T> begin() const
    { return list_iterator<T>(head->next); }
  list_iterator<T> end() const
    { return list_iterator<T>(tail); } 
    
  list_reverse_iterator<T> rbegin() const
    { return list_reverse_iterator<T>(tail->prev); }
  list_reverse_iterator<T> rend() const
    { return list_reverse_iterator<T>(head); }
    
  T& operator[](int);

public:
	friend list operator*<>(list const&, list const&);		// intersection
	friend list operator+<>(list const&, list const&);		// union
};


template<class T>
list<T>::list()
: head(new list_node<T>()),
	tail(new list_node<T>()),
	sz(0)
{
	head->next = tail;
	head->prev = NULL;
	tail->prev = head;
	tail->next = NULL;
}


template<class T>
list<T>::~list()
{
  list_node<T>* n_p;
  list_iterator<T> it = begin();
  for (; it!=end(); ++it,delete n_p) n_p = it.get_curr();
  delete head; delete tail;
{'fraction_non_alphanumeric': 0.20019555120997312, 'fraction_numerical': 0.008066487411390858, 'mean_word_length': 2.3772183243912504, 'pattern_counts': {'":': 0, '<': 105, '<?xml version=': 0, '>': 131, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 28, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '593915', 'n_tokens_mistral': 2876, 'n_tokens_neox': 2811, 'n_words': 622}