code:
// -*- C++ -*-

// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the terms
// of the GNU General Public License as published by the Free Software
// Foundation; either version 2, or (at your option) any later
// version.

// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this library; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
// MA 02111-1307, USA.

// As a special exception, you may use this file as part of a free
// software library without restriction.  Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to
// produce an executable, this file does not by itself cause the
// resulting executable to be covered by the GNU General Public
// License.  This exception does not however invalidate any other
// reasons why the executable file might be covered by the GNU General
// Public License.

// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.

// Permission to use, copy, modify, sell, and distribute this software
// is hereby granted without fee, provided that the above copyright
// notice appears in all copies, and that both that copyright notice
// and this permission notice appear in supporting documentation. None
// of the above authors, nor IBM Haifa Research Laboratories, make any
// representation about the suitability of this software for any
// purpose. It is provided "as is" without express or implied
// warranty.

/**
 * @file node_metadata_selector.hpp
 * Contains an implementation class for trees.
 */

#ifndef PB_DS_TREE_NODE_METADATA_SELECTOR_HPP
#define PB_DS_TREE_NODE_METADATA_SELECTOR_HPP

#include <ext/pb_ds/detail/basic_tree_policy/null_node_metadata.hpp>
#include <ext/pb_ds/detail/types_traits.hpp>

namespace pb_ds
{
  namespace detail
  {

    template<typename Node_Update, bool Null>
    struct tree_metadata_helper
    {
      typedef typename Node_Update::metadata_type type;
    };

    template<typename Node_Update>
    struct tree_metadata_helper<
      Node_Update,
      true>
    {
      typedef null_node_metadata type;
    };

    template<typename Key,
	     typename Data,
	     class Cmp_Fn,
	     template<typename Const_Node_Iterator,
		      class Const_Iterator,
		      class Cmp_Fn_,
		      class Allocator_>
    class Node_Update,
	     class Allocator>
    struct tree_node_metadata_selector
    {
    private:
      typedef
      dumconst_node_iterator<
      Key,
      Data,
      Allocator>
      dumconst_node_it;

      enum
	{
	  null_update =
	  is_same<
	  Node_Update<
	  dumconst_node_it,
	  dumconst_node_it,
	  Cmp_Fn,
	  Allocator>,
	  null_tree_node_update<
	  dumconst_node_it,
	  dumconst_node_it,
	  Cmp_Fn,
	  Allocator> >::value
	};

    public:
      typedef
      typename tree_metadata_helper<
      Node_Update<
      dumconst_node_it,
      dumconst_node_it,
      Cmp_Fn,
      Allocator>,
      null_update>::type
      type;
    };

  } // namespace detail
} // namespace pb_ds

#endif // #ifndef PB_DS_TREE_NODE_METADATA_SELECTOR_HPP


code:
// -*- C++ -*-

// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the terms
// of the GNU General Public License as published by the Free Software
// Foundation; either version 2, or (at your option) any later
// version.

// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this library; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
// MA 02111-1307, USA.

// As a special exception, you may use this file as part of a free
// software library without restriction.  Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to
// produce an executable, this file does not by itself cause the
// resulting executable to be covered by the GNU General Public
// License.  This exception does not however invalidate any other
// reasons why the executable file might be covered by the GNU General
// Public License.

// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.

// Permission to use, copy, modify, sell, and distribute this software
// is hereby granted without fee, provided that the above copyright
// notice appears in all copies, and that both that copyright notice
// and this permission notice appear in supporting documentation. None
// of the above authors, nor IBM Haifa Research Laboratories, make any
// representation about the suitability of this software for any
// purpose. It is provided "as is" without express or implied
// warranty.

/**
 * @file null_node_update_imp.hpp
 * Contains an implementation of null_node_update.
 */

PB_DS_CLASS_T_DEC
template<typename Const_Node_Iterator_,
	 typename Node_Iterator_,
	 class    Cmp_Fn_,
	 typename Allocator_>
inline void
PB_DS_CLASS_C_DEC::
swap(null_tree_node_update<            Const_Node_Iterator_, Node_Iterator_, Cmp_Fn_, Allocator_>& /*other*/)
{ }

repo_name: veiledmerc/mira

license: mit


code:
// -*- C++ -*-

// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the terms
// of the GNU General Public License as published by the Free Software
// Foundation; either version 2, or (at your option) any later
// version.

// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this library; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
// MA 02111-1307, USA.

// As a special exception, you may use this file as part of a free
// software library without restriction.  Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to
// produce an executable, this file does not by itself cause the
// resulting executable to be covered by the GNU General Public
// License.  This exception does not however invalidate any other
// reasons why the executable file might be covered by the GNU General
// Public License.

// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.

// Permission to use, copy, modify, sell, and distribute this software
// is hereby granted without fee, provided that the above copyright
// notice appears in all copies, and that both that copyright notice
// and this permission notice appear in supporting documentation. None
// of the above authors, nor IBM Haifa Research Laboratories, make any
// representation about the suitability of this software for any
// purpose. It is provided "as is" without express or implied
// warranty.

/**
 * @file order_statistics_imp.hpp
 * Contains forward declarations for order_statistics_key
 */

PB_DS_CLASS_T_DEC
inline typename PB_DS_CLASS_C_DEC::iterator
PB_DS_CLASS_C_DEC::
find_by_order(size_type order)
{
  node_iterator it = node_begin();

  node_iterator end_it = node_end();

  while (it != end_it)
    {
      node_iterator l_it = it.get_l_child();

      const size_type o = (l_it == end_it)?
	0 :
	l_it.get_metadata();

      if (order == o)
	return (*it);
      else if (order < o)
	it = l_it;
      else
        {
	  order -= o + 1;

	  it = it.get_r_child();
        }
    }

  return (PB_DS_BASE_C_DEC::end_iterator());
}

PB_DS_CLASS_T_DEC
inline typename PB_DS_CLASS_C_DEC::const_iterator
PB_DS_CLASS_C_DEC::
find_by_order(size_type order) const
{
  return (const_cast<PB_DS_CLASS_C_DEC* >(this)->find_by_order(order));
}

PB_DS_CLASS_T_DEC
inline typename PB_DS_CLASS_C_DEC::size_type
PB_DS_CLASS_C_DEC::
order_of_key(const_key_reference r_key) const
{
  const_node_iterator it = node_begin();

  const_node_iterator end_it = node_end();

  const cmp_fn& r_cmp_fn =
    const_cast<PB_DS_CLASS_C_DEC* >(this)->get_cmp_fn();

  size_type ord = 0;

  while (it != end_it)
    {
      const_node_iterator l_it = it.get_l_child();

      if (r_cmp_fn(r_key, extract_key(*(*it))))
	it = l_it;
      else if (r_cmp_fn(extract_key(*(*it)), r_key))
        {

	  ord += (l_it == end_it)?
	    1 :
	    1 + l_it.get_metadata();

	  it = it.get_r_child();
        }
      else
        {
	  ord += (l_it == end_it)?
	    0 :
	    l_it.get_metadata();

	  it = end_it;
        }
    }

  return (ord);
}

PB_DS_CLASS_T_DEC
inline void
PB_DS_CLASS_C_DEC::
operator()(node_iterator node_it, const_node_iterator end_nd_it) const
{
  node_iterator l_child_it = node_it.get_l_child();
  const size_type l_rank =(l_child_it == end_nd_it)? 0 : l_child_it.get_metadata();

  node_iterator r_child_it = node_it.get_r_child();
  const size_type r_rank =(r_child_it == end_nd_it)? 0 : r_child_it.get_metadata();

  const_cast<metadata_reference>(node_it.get_metadata())=
    1 + l_rank + r_rank;
}

PB_DS_CLASS_T_DEC
PB_DS_CLASS_C_DEC::
~tree_order_statistics_node_update()
{ }


code:
// -*- C++ -*-

// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the terms
// of the GNU General Public License as published by the Free Software
// Foundation; either version 2, or (at your option) any later
// version.

// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this library; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
// MA 02111-1307, USA.

// As a special exception, you may use this file as part of a free
// software library without restriction.  Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to
// produce an executable, this file does not by itself cause the
// resulting executable to be covered by the GNU General Public
// License.  This exception does not however invalidate any other
// reasons why the executable file might be covered by the GNU General
// Public License.

// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.

// Permission to use, copy, modify, sell, and distribute this software
// is hereby granted without fee, provided that the above copyright
// notice appears in all copies, and that both that copyright notice
// and this permission notice appear in supporting documentation. None
// of the above authors, nor IBM Haifa Research Laboratories, make any
// representation about the suitability of this software for any
// purpose. It is provided "as is" without express or implied
// warranty.

/**
 * @file sample_tree_node_update.hpp
 * Contains a samle node update functor.
 */

#ifndef PB_DS_SAMPLE_TREE_NODE_UPDATOR_HPP
#define PB_DS_SAMPLE_TREE_NODE_UPDATOR_HPP

// A sample node updator.
template<typename Const_Node_Iterator,

	 class Node_Iterator,

	 class Cmp_Fn,

	 class Allocator
	 >
class sample_tree_node_update
{

public:

  // Metadata type.
  typedef size_t metadata_type;

protected:

  // Default constructor.
  sample_tree_node_update();

  // Updates the rank of a node through a node_iterator node_it; end_nd_it is the end node iterator.
  inline void
  operator()(node_iterator node_it, const_node_iterator end_nd_it) const;

};

#endif // #ifndef PB_DS_SAMPLE_TREE_NODE_UPDATOR_HPP


code:
// -*- C++ -*-

// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the terms
// of the GNU General Public License as published by the Free Software
// Foundation; either version 2, or (at your option) any later
// version.

// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this library; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
// MA 02111-1307, USA.

// As a special exception, you may use this file as part of a free
// software library without restriction.  Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to
// produce an executable, this file does not by itself cause the
// resulting executable to be covered by the GNU General Public
// License.  This exception does not however invalidate any other
// reasons why the executable file might be covered by the GNU General
// Public License.

// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.

// Permission to use, copy, modify, sell, and distribute this software
// is hereby granted without fee, provided that the above copyright
// notice appears in all copies, and that both that copyright notice
// and this permission notice appear in supporting documentation. None
// of the above authors, nor IBM Haifa Research Laboratories, make any
// representation about the suitability of this software for any
// purpose. It is provided "as is" without express or implied
// warranty.

/**
 * @file tree_trace_base.hpp
 * Contains tree-related policies.
 */

#ifndef PB_DS_TREE_TRACE_BASE_HPP
#define PB_DS_TREE_TRACE_BASE_HPP

#ifdef PB_DS_TREE_TRACE

#include <ext/pb_ds/detail/basic_tree_policy/basic_tree_policy_base.hpp>
#include <ext/pb_ds/detail/basic_tree_policy/null_node_metadata.hpp>

namespace pb_ds
{

  namespace detail
  {

#ifdef PB_DS_TREE_TRACE

#define PB_DS_CLASS_T_DEC						\
    template<								\
						class Const_Node_Iterator, \
						class Node_Iterator,	\
						class Cmp_Fn,		\
						bool Node_Based,	\
						class Allocator>

#define PB_DS_CLASS_C_DEC						\
    tree_trace_base<							\
						Const_Node_Iterator,	\
						Node_Iterator,		\
						Cmp_Fn,			\
						Node_Based,		\
						Allocator>

#define PB_DS_BASE_C_DEC						\
    basic_tree_policy_base<				\
								Const_Node_Iterator, \
								Node_Iterator, \
								Allocator>

    template<typename Const_Node_Iterator,
	     class Node_Iterator,
	     class Cmp_Fn,
	     bool Node_Based,
	     class Allocator>
    class tree_trace_base : private PB_DS_BASE_C_DEC
    {
    public:
      void
      trace() const;

    private:
      typedef PB_DS_BASE_C_DEC base_type;

      typedef Const_Node_Iterator const_node_iterator;

      typedef typename Allocator::size_type size_type;

    private:
      void
      trace_node(const_node_iterator nd_it, size_type level) const;

      virtual bool
      empty() const = 0;

      virtual const_node_iterator
      node_begin() const = 0;

      virtual const_node_iterator
      node_end() const = 0;

      static void
      print_node_pointer(Const_Node_Iterator nd_it, integral_constant<int,true>);

      static void
      print_node_pointer(Const_Node_Iterator nd_it, integral_constant<int,false>);

      template<typename Metadata_>
      static void
      trace_it_metadata(Const_Node_Iterator nd_it, type_to_type<Metadata_>);

      static void
      trace_it_metadata(Const_Node_Iterator, type_to_type<null_node_metadata>);
    };

    PB_DS_CLASS_T_DEC
    void
    PB_DS_CLASS_C_DEC::
    trace() const
    {
      if (empty())
        return;

      trace_node(node_begin(), 0);
    }

    PB_DS_CLASS_T_DEC
    void
    PB_DS_CLASS_C_DEC::
    trace_node(const_node_iterator nd_it, size_type level) const
    {
      if (nd_it.get_r_child() != node_end())
        trace_node(nd_it.get_r_child(), level + 1);

      for (size_type i = 0; i < level; ++i)
        std::cerr << ' ';

      print_node_pointer(nd_it, integral_constant<int,Node_Based>());
      std::cerr << base_type::extract_key(*(*nd_it));

      typedef
        type_to_type<
	typename const_node_iterator::metadata_type>
        m_type_ind_t;

      trace_it_metadata(nd_it, m_type_ind_t());

      std::cerr << std::endl;

      if (nd_it.get_l_child() != node_end())
        trace_node(nd_it.get_l_child(), level + 1);
    }

    PB_DS_CLASS_T_DEC
    template<typename Metadata_>
    void
    PB_DS_CLASS_C_DEC::
    trace_it_metadata(Const_Node_Iterator nd_it, type_to_type<Metadata_>)
    {
      std::cerr << " (" <<
        static_cast<unsigned long>(nd_it.get_metadata()) << ") ";
    }

    PB_DS_CLASS_T_DEC
    void
    PB_DS_CLASS_C_DEC::
    trace_it_metadata(Const_Node_Iterator, type_to_type<null_node_metadata>)
    { }

    PB_DS_CLASS_T_DEC
    void
    PB_DS_CLASS_C_DEC::
    print_node_pointer(Const_Node_Iterator nd_it, integral_constant<int,true>)
    {
      std::cerr << nd_it.m_p_nd << " ";
    }

    PB_DS_CLASS_T_DEC
    void
    PB_DS_CLASS_C_DEC::
    print_node_pointer(Const_Node_Iterator nd_it, integral_constant<int,false>)
    {
      std::cerr <<* nd_it << " ";
    }

#undef PB_DS_CLASS_T_DEC

#undef PB_DS_CLASS_C_DEC

#undef PB_DS_BASE_C_DEC

#endif // #ifdef    PB_DS_TREE_TRACE

  } // namespace detail

} // namespace pb_ds

#endif // #ifdef PB_DS_TREE_TRACE

#endif // #ifndef PB_DS_TREE_TRACE_BASE_HPP


code:
// -*- C++ -*-

// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the terms
// of the GNU General Public License as published by the Free Software
// Foundation; either version 2, or (at your option) any later
// version.

// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this library; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
// MA 02111-1307, USA.

// As a special exception, you may use this file as part of a free
// software library without restriction.  Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to
// produce an executable, this file does not by itself cause the
// resulting executable to be covered by the GNU General Public
// License.  This exception does not however invalidate any other
// reasons why the executable file might be covered by the GNU General
// Public License.

// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.

// Permission to use, copy, modify, sell, and distribute this software
// is hereby granted without fee, provided that the above copyright
// notice appears in all copies, and that both that copyright notice
// and this permission notice appear in supporting documentation. None
// of the above authors, nor IBM Haifa Research Laboratories, make any
// representation about the suitability of this software for any
// purpose. It is provided "as is" without express or implied
// warranty.

/**
 * @file node_metadata_selector.hpp
 * Contains an implementation class for tries.
 */

#ifndef PB_DS_TRIE_NODE_METADATA_SELECTOR_HPP
#define PB_DS_TRIE_NODE_METADATA_SELECTOR_HPP

#include <ext/pb_ds/detail/basic_tree_policy/null_node_metadata.hpp>
#include <ext/pb_ds/detail/types_traits.hpp>

namespace pb_ds
{
  namespace detail
  {

    template<typename Node_Update, bool Null>
    struct trie_metadata_helper
    {
      typedef typename Node_Update::metadata_type type;
    };

    template<typename Node_Update>
    struct trie_metadata_helper<
      Node_Update,
      true>
    {
      typedef null_node_metadata type;
    };

    template<typename Key,
	     typename Data,
	     class Cmp_Fn,
	     template<typename Const_Node_Iterator,
		      class Const_Iterator,
		      class Cmp_Fn_,
		      class Allocator_>
    class Node_Update,
	     class Allocator>
    struct trie_node_metadata_selector
    {
    private:
      typedef
      dumconst_node_iterator<
      Key,
      Data,
      Allocator>
      dumconst_node_it;

      enum
	{
	  null_update =
	  is_same<
	  Node_Update<
	  dumconst_node_it,
	  dumconst_node_it,
	  Cmp_Fn,
	  Allocator>,
	  null_trie_node_update<
	  dumconst_node_it,
	  dumconst_node_it,
	  Cmp_Fn,
	  Allocator> >::value
	};

    public:
      typedef
      typename trie_metadata_helper<
      Node_Update<
      dumconst_node_it,
      dumconst_node_it,
      Cmp_Fn,
      Allocator>,
      null_update>::type
      type;
    };

  } // namespace detail
} // namespace pb_ds

#endif // #ifndef PB_DS_TRIE_NODE_METADATA_SELECTOR_HPP


code:
// -*- C++ -*-

// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the terms
// of the GNU General Public License as published by the Free Software
// Foundation; either version 2, or (at your option) any later
// version.

// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this library; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
// MA 02111-1307, USA.

// As a special exception, you may use this file as part of a free
// software library without restriction.  Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to
// produce an executable, this file does not by itself cause the
// resulting executable to be covered by the GNU General Public
// License.  This exception does not however invalidate any other
// reasons why the executable file might be covered by the GNU General
// Public License.

// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.

// Permission to use, copy, modify, sell, and distribute this software
// is hereby granted without fee, provided that the above copyright
// notice appears in all copies, and that both that copyright notice
// and this permission notice appear in supporting documentation. None
// of the above authors, nor IBM Haifa Research Laboratories, make any
// representation about the suitability of this software for any
// purpose. It is provided "as is" without express or implied
// warranty.

/**
 * @file null_node_update_imp.hpp
 * Contains an implementation of null_node_update.
 */

PB_DS_CLASS_T_DEC
template<typename Const_Node_Iterator_,
	 typename Node_Iterator_,
	 class    E_Access_Traits_,
	 typename Allocator_>
inline void
PB_DS_CLASS_C_DEC::
swap(null_trie_node_update<            Const_Node_Iterator_, Node_Iterator_, E_Access_Traits_, Allocator_>& /*other*/)
{ }


code:
// -*- C++ -*-

// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the terms
// of the GNU General Public License as published by the Free Software
// Foundation; either version 2, or (at your option) any later
// version.

// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this library; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
// MA 02111-1307, USA.

// As a special exception, you may use this file as part of a free
// software library without restriction.  Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to
// produce an executable, this file does not by itself cause the
// resulting executable to be covered by the GNU General Public
// License.  This exception does not however invalidate any other
// reasons why the executable file might be covered by the GNU General
// Public License.

// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.

// Permission to use, copy, modify, sell, and distribute this software
// is hereby granted without fee, provided that the above copyright
// notice appears in all copies, and that both that copyright notice
// and this permission notice appear in supporting documentation. None
// of the above authors, nor IBM Haifa Research Laboratories, make any
// representation about the suitability of this software for any
// purpose. It is provided "as is" without express or implied
// warranty.

/**
 * @file order_statistics_imp.hpp
 * Contains forward declarations for order_statistics_key
 */

PB_DS_CLASS_T_DEC
inline typename PB_DS_CLASS_C_DEC::iterator
PB_DS_CLASS_C_DEC::
find_by_order(size_type order)
{
  if (empty())
    return (end());

  ++order;

  node_iterator nd_it = node_begin();

  node_iterator end_nd_it = node_end();

  while (true)
    {
      if (order > nd_it.get_metadata())
	return (++base_type::rightmost_it(nd_it));

      const size_type num_children = nd_it.num_children();

      if (num_children == 0)
	return (*nd_it);

      for (size_type i = 0; i < num_children; ++i)
        {
	  node_iterator child_nd_it = nd_it.get_child(i);

	  if (order <= child_nd_it.get_metadata())
            {
	      i = num_children;

	      nd_it = child_nd_it;
            }
	  else
	    order -= child_nd_it.get_metadata();
        }
    }
}

PB_DS_CLASS_T_DEC
inline typename PB_DS_CLASS_C_DEC::const_iterator
PB_DS_CLASS_C_DEC::
find_by_order(size_type order) const
{
  return (const_cast<PB_DS_CLASS_C_DEC* >(this)->find_by_order(order));
}

PB_DS_CLASS_T_DEC
inline typename PB_DS_CLASS_C_DEC::size_type
PB_DS_CLASS_C_DEC::
order_of_key(const_key_reference r_key) const
{
  const E_Access_Traits& r_traits =
    const_cast<PB_DS_CLASS_C_DEC* >(this)->get_e_access_traits();

  return (order_of_prefix(
			  r_traits.begin(r_key),
			  r_traits.end(r_key)));
}

PB_DS_CLASS_T_DEC
inline typename PB_DS_CLASS_C_DEC::size_type
PB_DS_CLASS_C_DEC::
order_of_prefix(typename e_access_traits::const_iterator b, typename e_access_traits::const_iterator e) const
{
  if (empty())
    return (0);

  const E_Access_Traits& r_traits =
    const_cast<PB_DS_CLASS_C_DEC* >(this)->get_e_access_traits();

  const_node_iterator nd_it = node_begin();

  const_node_iterator end_nd_it = node_end();

  size_type ord = 0;

  while (true)
    {
      const size_type num_children = nd_it.num_children();

      if (num_children == 0)
        {
	  const_key_reference r_key =
	    base_type::extract_key(*(*nd_it));

	  typename e_access_traits::const_iterator key_b =
	    r_traits.begin(r_key);

	  typename e_access_traits::const_iterator key_e =
	    r_traits.end(r_key);

	  return ((base_type::less(                    key_b, key_e,  b, e,  r_traits))?
		  ord + 1 :
		  ord);
        }

      const_node_iterator next_nd_it = end_nd_it;

      size_type i = num_children - 1;

      do
        {
	  const_node_iterator child_nd_it = nd_it.get_child(i);

	  if (next_nd_it != end_nd_it)
	    ord += child_nd_it.get_metadata();
	  else if (!base_type::less(
				    b, e,
				    child_nd_it.valid_prefix().first,
				    child_nd_it.valid_prefix().second,
				    r_traits))
	    next_nd_it = child_nd_it;
        }
      while (i-- > 0);

      if (next_nd_it == end_nd_it)
	return (ord);

      nd_it = next_nd_it;
    }
}

PB_DS_CLASS_T_DEC
inline void
PB_DS_CLASS_C_DEC::
operator()(node_iterator nd_it, const_node_iterator /*end_nd_it*/) const
{
  const size_type num_children = nd_it.num_children();

  size_type children_rank = 0;

  for (size_type i = 0; i < num_children; ++i)
    children_rank += nd_it.get_child(i).get_metadata();

  const_cast<size_type& >(nd_it.get_metadata()) =(num_children == 0)? 1 : children_rank;
}

PB_DS_CLASS_T_DEC
PB_DS_CLASS_C_DEC::
~trie_order_statistics_node_update()
{ }


code:
// -*- C++ -*-

// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the terms
// of the GNU General Public License as published by the Free Software
// Foundation; either version 2, or (at your option) any later
// version.

// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this library; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
// MA 02111-1307, USA.

// As a special exception, you may use this file as part of a free
// software library without restriction.  Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to
// produce an executable, this file does not by itself cause the
// resulting executable to be covered by the GNU General Public
// License.  This exception does not however invalidate any other
// reasons why the executable file might be covered by the GNU General
// Public License.

// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.

// Permission to use, copy, modify, sell, and distribute this software
// is hereby granted without fee, provided that the above copyright
// notice appears in all copies, and that both that copyright notice
// and this permission notice appear in supporting documentation. None
// of the above authors, nor IBM Haifa Research Laboratories, make any
// representation about the suitability of this software for any
// purpose. It is provided "as is" without express or implied
// warranty.

/**
 * @file prefix_search_node_update_imp.hpp
 * Contains an implementation of prefix_search_node_update.
 */

PB_DS_CLASS_T_DEC
std::pair<
  typename PB_DS_CLASS_C_DEC::const_iterator,
  typename PB_DS_CLASS_C_DEC::const_iterator>
PB_DS_CLASS_C_DEC::
prefix_range(const_key_reference r_key) const
{
  const e_access_traits& r_traits = get_e_access_traits();

  return (prefix_range(
		       r_traits.begin(r_key),
		       r_traits.end(r_key)));
}

PB_DS_CLASS_T_DEC
std::pair<
  typename PB_DS_CLASS_C_DEC::iterator,
  typename PB_DS_CLASS_C_DEC::iterator>
PB_DS_CLASS_C_DEC::
prefix_range(const_key_reference r_key)
{
  return (prefix_range(
		       get_e_access_traits().begin(r_key),
		       get_e_access_traits().end(r_key)));
}

PB_DS_CLASS_T_DEC
std::pair<
  typename PB_DS_CLASS_C_DEC::const_iterator,
  typename PB_DS_CLASS_C_DEC::const_iterator>
PB_DS_CLASS_C_DEC::
prefix_range(typename e_access_traits::const_iterator b, typename e_access_traits::const_iterator e) const
{
  const std::pair<iterator, iterator> non_const_ret =
    const_cast<PB_DS_CLASS_C_DEC* >(this)->prefix_range(b, e);

  return (std::make_pair(
			 const_iterator(non_const_ret.first),
			 const_iterator(non_const_ret.second)));
}

PB_DS_CLASS_T_DEC
std::pair<
  typename PB_DS_CLASS_C_DEC::iterator,
  typename PB_DS_CLASS_C_DEC::iterator>
PB_DS_CLASS_C_DEC::
prefix_range(typename e_access_traits::const_iterator b, typename e_access_traits::const_iterator e)
{
  Node_Iterator nd_it = node_begin();
  Node_Iterator end_nd_it = node_end();

  const e_access_traits& r_traits =
    get_e_access_traits();

  const size_type given_range_length = std::distance(b, e);

  while (true)
    {
      if (nd_it == end_nd_it)
	return (std::make_pair(end(), end()));

      const size_type common_range_length =
	PB_DS_BASE_C_DEC::common_prefix_len(nd_it, b, e, r_traits);

      if (common_range_length >= given_range_length)
        {
	  iterator ret_b = leftmost_it(nd_it);

	  iterator ret_e = rightmost_it(nd_it);

	  return (std::make_pair(ret_b, ++ret_e));
        }

      nd_it = next_child(nd_it, b, e, end_nd_it, r_traits);
    }
}

PB_DS_CLASS_T_DEC
typename PB_DS_CLASS_C_DEC::node_iterator
PB_DS_CLASS_C_DEC::
next_child(node_iterator nd_it, typename e_access_traits::const_iterator b, typename e_access_traits::const_iterator e, node_iterator end_nd_it, const e_access_traits& r_traits)
{
  const size_type num_children = nd_it.num_children();

  node_iterator ret = end_nd_it;

  size_type max_length = 0;

  for (size_type i = 0; i < num_children; ++i)
    {
      node_iterator pot = nd_it.get_child(i);

      const size_type common_range_length =
	PB_DS_BASE_C_DEC::common_prefix_len(            pot, b, e, r_traits);

      if (common_range_length > max_length)
        {
	  ret = pot;

	  max_length = common_range_length;
        }
    }

  return (ret);
}

PB_DS_CLASS_T_DEC
inline void
PB_DS_CLASS_C_DEC::
operator()(node_iterator /*nd_it*/, const_node_iterator /*end_nd_it*/) const
{ }


code:
// -*- C++ -*-

// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the terms
// of the GNU General Public License as published by the Free Software
// Foundation; either version 2, or (at your option) any later
// version.

// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this library; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
// MA 02111-1307, USA.

// As a special exception, you may use this file as part of a free
// software library without restriction.  Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to
// produce an executable, this file does not by itself cause the
// resulting executable to be covered by the GNU General Public
// License.  This exception does not however invalidate any other
// reasons why the executable file might be covered by the GNU General
// Public License.

// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.

// Permission to use, copy, modify, sell, and distribute this software
// is hereby granted without fee, provided that the above copyright
// notice appears in all copies, and that both that copyright notice
// and this permission notice appear in supporting documentation. None
// of the above authors, nor IBM Haifa Research Laboratories, make any
// representation about the suitability of this software for any
// purpose. It is provided "as is" without express or implied
// warranty.

/**
 * @file sample_trie_e_access_traits.hpp
 * Contains a sample probe policy.
 */

#ifndef PB_DS_SAMPLE_TRIE_E_ACCESS_TRAITS_HPP
#define PB_DS_SAMPLE_TRIE_E_ACCESS_TRAITS_HPP

// A sample trie element-access traits.
class sample_trie_e_access_traits
{

public:

  // Size type.
  typedef size_t size_type;

  // Key type.
  typedef std::string key_type;

  // Const key reference type.
  typedef
  typename Allocator::template rebind<
    key_type>::other::const_reference
  const_key_reference;

  // Element const iterator type.
  typedef std::string::const_iterator const_iterator;

  // Element type.
  typedef char e_type;

  enum
    {
      max_size = 4
    };

public:

  // Returns a const_iterator to the first element of r_key.
  inline static const_iterator
  begin(const_key_reference r_key);

  // Returns a const_iterator to the after-last element of r_key.
  inline static const_iterator
  end(const_key_reference r_key);

  // Maps an element to a position.
  inline static size_type
  e_pos(e_type e);

};

#endif // #ifndef PB_DS_SAMPLE_TRIE_E_ACCESS_TRAITS_HPP


code:
// -*- C++ -*-

// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the terms
// of the GNU General Public License as published by the Free Software
// Foundation; either version 2, or (at your option) any later
// version.

// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this library; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
// MA 02111-1307, USA.

// As a special exception, you may use this file as part of a free
// software library without restriction.  Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to
// produce an executable, this file does not by itself cause the
// resulting executable to be covered by the GNU General Public
// License.  This exception does not however invalidate any other
// reasons why the executable file might be covered by the GNU General
// Public License.

// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.

// Permission to use, copy, modify, sell, and distribute this software
// is hereby granted without fee, provided that the above copyright
// notice appears in all copies, and that both that copyright notice
// and this permission notice appear in supporting documentation. None
// of the above authors, nor IBM Haifa Research Laboratories, make any
// representation about the suitability of this software for any
// purpose. It is provided "as is" without express or implied
// warranty.

/**
 * @file sample_trie_node_update.hpp
 * Contains a samle node update functor.
 */

#ifndef PB_DS_SAMPLE_TRIE_NODE_UPDATOR_HPP
#define PB_DS_SAMPLE_TRIE_NODE_UPDATOR_HPP

// A sample node updator.
template<typename Const_Node_Iterator,

	 class Node_Iterator,

	 class E_Access_Traits,

	 class Allocator
	 >
class sample_trie_node_update
{

public:

  // Metadata type.
  typedef size_t metadata_type;

protected:

  // Default constructor.
  sample_trie_node_update();

  // Updates the rank of a node through a node_iterator node_it; end_nd_it is the end node iterator.
  inline void
  operator()(node_iterator node_it, const_node_iterator end_nd_it) const;

};

#endif // #ifndef PB_DS_SAMPLE_TRIE_NODE_UPDATOR_HPP


code:
// -*- C++ -*-

// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the terms
// of the GNU General Public License as published by the Free Software
// Foundation; either version 2, or (at your option) any later
// version.

// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this library; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
// MA 02111-1307, USA.

// As a special exception, you may use this file as part of a free
// software library without restriction.  Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to
// produce an executable, this file does not by itself cause the
// resulting executable to be covered by the GNU General Public
// License.  This exception does not however invalidate any other
// reasons why the executable file might be covered by the GNU General
// Public License.

// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.

// Permission to use, copy, modify, sell, and distribute this software
// is hereby granted without fee, provided that the above copyright
// notice appears in all copies, and that both that copyright notice
// and this permission notice appear in supporting documentation. None
// of the above authors, nor IBM Haifa Research Laboratories, make any
// representation about the suitability of this software for any
// purpose. It is provided "as is" without express or implied
// warranty.

/**
 * @file string_trie_e_access_traits_imp.hpp
 * Contains a policy for extracting character positions from
 *    a string for a vector-based PATRICIA tree
 */

PB_DS_CLASS_T_DEC
detail::integral_constant<int, Reverse> PB_DS_CLASS_C_DEC::s_rev_ind;

PB_DS_CLASS_T_DEC
inline typename PB_DS_CLASS_C_DEC::size_type
PB_DS_CLASS_C_DEC::
e_pos(e_type e)
{
  return (static_cast<size_type>(e - min_e_val));
}

PB_DS_CLASS_T_DEC
inline typename PB_DS_CLASS_C_DEC::const_iterator
PB_DS_CLASS_C_DEC::
begin(const_key_reference r_key)
{
  return (begin_imp(r_key, s_rev_ind));
}

PB_DS_CLASS_T_DEC
inline typename PB_DS_CLASS_C_DEC::const_iterator
PB_DS_CLASS_C_DEC::
end(const_key_reference r_key)
{
  return (end_imp(r_key, s_rev_ind));
}

PB_DS_CLASS_T_DEC
inline typename PB_DS_CLASS_C_DEC::const_iterator
PB_DS_CLASS_C_DEC::
begin_imp(const_key_reference r_key, detail::false_type)
{
  return (r_key.begin());
}

PB_DS_CLASS_T_DEC
inline typename PB_DS_CLASS_C_DEC::const_iterator
PB_DS_CLASS_C_DEC::
begin_imp(const_key_reference r_key, detail::true_type)
{
  return (r_key.rbegin());
}

PB_DS_CLASS_T_DEC
inline typename PB_DS_CLASS_C_DEC::const_iterator
PB_DS_CLASS_C_DEC::
end_imp(const_key_reference r_key, detail::false_type)
{
  return (r_key.end());
}

PB_DS_CLASS_T_DEC
inline typename PB_DS_CLASS_C_DEC::const_iterator
PB_DS_CLASS_C_DEC::
end_imp(const_key_reference r_key, detail::true_type)
{
  return (r_key.rend());
}


code:
// -*- C++ -*-

// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the terms
// of the GNU General Public License as published by the Free Software
// Foundation; either version 2, or (at your option) any later
// version.

// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this library; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
// MA 02111-1307, USA.

// As a special exception, you may use this file as part of a free
// software library without restriction.  Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to
// produce an executable, this file does not by itself cause the
// resulting executable to be covered by the GNU General Public
// License.  This exception does not however invalidate any other
// reasons why the executable file might be covered by the GNU General
// Public License.

// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.

// Permission to use, copy, modify, sell, and distribute this software
// is hereby granted without fee, provided that the above copyright
// notice appears in all copies, and that both that copyright notice
// and this permission notice appear in supporting documentation. None
// of the above authors, nor IBM Haifa Research Laboratories, make any
// representation about the suitability of this software for any
// purpose. It is provided "as is" without express or implied
// warranty.

/**
 * @file trie_policy_base.hpp
 * Contains an implementation of trie_policy_base.
 */

#ifndef PB_DS_TRIE_POLICY_BASE_HPP
#define PB_DS_TRIE_POLICY_BASE_HPP

#include <ext/pb_ds/detail/basic_tree_policy/basic_tree_policy_base.hpp>

namespace pb_ds
{
  namespace detail
  {

#define PB_DS_CLASS_T_DEC						\
    template<								\
						class Const_Node_Iterator, \
						class Node_Iterator,	\
						class E_Access_Traits,	\
						typename Allocator>

#define PB_DS_CLASS_C_DEC						\
    trie_policy_base<							\
						Const_Node_Iterator,	\
						Node_Iterator,		\
						E_Access_Traits,	\
						Allocator>

#define PB_DS_BASE_C_DEC						\
    basic_tree_policy_base<				\
								Const_Node_Iterator, \
								Node_Iterator, \
								Allocator>

    template<typename Const_Node_Iterator,
	     class Node_Iterator,
	     class E_Access_Traits,
	     class Allocator>
    class trie_policy_base : public PB_DS_BASE_C_DEC
    {

    public:

      typedef E_Access_Traits e_access_traits;

      typedef Allocator allocator;

      typedef typename allocator::size_type size_type;

      typedef null_node_metadata metadata_type;

      typedef Const_Node_Iterator const_node_iterator;

      typedef Node_Iterator node_iterator;

      typedef typename const_node_iterator::value_type const_iterator;

      typedef typename node_iterator::value_type iterator;

    public:

      typedef typename PB_DS_BASE_C_DEC::key_type key_type;

      typedef
      typename PB_DS_BASE_C_DEC::const_key_reference
      const_key_reference;

    protected:

      virtual const_iterator
      end() const = 0;

      virtual iterator
      end() = 0;

      virtual const_node_iterator
      node_begin() const = 0;

      virtual node_iterator
      node_begin() = 0;

      virtual const_node_iterator
      node_end() const = 0;

      virtual node_iterator
      node_end() = 0;

      virtual const e_access_traits&
      get_e_access_traits() const = 0;

    private:
      typedef
      std::pair<
      typename e_access_traits::const_iterator,
      typename e_access_traits::const_iterator>
      prefix_range_t;

      typedef PB_DS_BASE_C_DEC base_type;

    protected:
      static size_type
      common_prefix_len(node_iterator nd_it, typename e_access_traits::const_iterator b_r, typename e_access_traits::const_iterator e_r, const e_access_traits& r_traits);

      static iterator
      leftmost_it(node_iterator nd_it);

      static iterator
      rightmost_it(node_iterator nd_it);

      static bool
      less(typename e_access_traits::const_iterator b_l, typename e_access_traits::const_iterator e_l, typename e_access_traits::const_iterator b_r, typename e_access_traits::const_iterator e_r, const e_access_traits& r_traits);
    };

    PB_DS_CLASS_T_DEC
    typename PB_DS_CLASS_C_DEC::size_type
    PB_DS_CLASS_C_DEC::
    common_prefix_len(node_iterator nd_it, typename e_access_traits::const_iterator b_r, typename e_access_traits::const_iterator e_r, const e_access_traits& r_traits)
    {
      prefix_range_t pref_range = nd_it.valid_prefix();

      typename e_access_traits::const_iterator b_l = pref_range.first;
      typename e_access_traits::const_iterator e_l = pref_range.second;

      const size_type range_length_l =
	std::distance(b_l, e_l);

      const size_type range_length_r =
	std::distance(b_r, e_r);

      if (range_length_r < range_length_l)
	{
	  std::swap(b_l, b_r);

	  std::swap(e_l, e_r);
	}

      size_type ret = 0;

      while (b_l != e_l)
	{
	  if (r_traits.e_pos(*b_l) != r_traits.e_pos(*b_r))
	    return (ret);

	  ++ret;

	  ++b_l;

	  ++b_r;
	}

      return (ret);
    }

    PB_DS_CLASS_T_DEC
    typename PB_DS_CLASS_C_DEC::iterator
    PB_DS_CLASS_C_DEC::
    leftmost_it(node_iterator nd_it)
    {
      if (nd_it.num_children() == 0)
	return (*nd_it);

      return (leftmost_it(nd_it.get_child(0)));
    }

    PB_DS_CLASS_T_DEC
    typename PB_DS_CLASS_C_DEC::iterator
    PB_DS_CLASS_C_DEC::
    rightmost_it(node_iterator nd_it)
    {
      const size_type num_children = nd_it.num_children();

      if (num_children == 0)
	return (*nd_it);

      return (rightmost_it(nd_it.get_child(num_children - 1)));
    }

    PB_DS_CLASS_T_DEC
    bool
    PB_DS_CLASS_C_DEC::
    less(typename e_access_traits::const_iterator b_l, typename e_access_traits::const_iterator e_l, typename e_access_traits::const_iterator b_r, typename e_access_traits::const_iterator e_r, const e_access_traits& r_traits)
    {
      while (b_l != e_l)
	{
	  if (b_r == e_r)
	    return (false);

	  size_type l_pos =
	    r_traits.e_pos(*b_l);
	  size_type r_pos =
	    r_traits.e_pos(*b_r);

	  if (l_pos != r_pos)
	    return (l_pos < r_pos);

	  ++b_l;
	  ++b_r;
	}

      return (b_r != e_r);
    }

#undef PB_DS_CLASS_T_DEC

#undef PB_DS_CLASS_C_DEC

#undef PB_DS_BASE_C_DEC

  } // namespace detail
} // namespace pb_ds

#endif // #ifndef PB_DS_TRIE_POLICY_BASE_HPP


code:
// -*- C++ -*-

// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the terms
// of the GNU General Public License as published by the Free Software
// Foundation; either version 2, or (at your option) any later
// version.

// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this library; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
// MA 02111-1307, USA.

// As a special exception, you may use this file as part of a free
// software library without restriction.  Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to
// produce an executable, this file does not by itself cause the
// resulting executable to be covered by the GNU General Public
// License.  This exception does not however invalidate any other
// reasons why the executable file might be covered by the GNU General
// Public License.

// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.

// Permission to use, copy, modify, sell, and distribute this software
// is hereby granted without fee, provided that the above copyright
// notice appears in all copies, and that both that copyright notice
// and this permission notice appear in supporting documentation. None
// of the above authors, nor IBM Haifa Research Laboratories, make any
// representation about the suitability of this software for any
// purpose. It is provided "as is" without express or implied
// warranty.

/**
 * @file type_utils.hpp
 * Contains utilities for handnling types. All of these classes are based on
 *    "Modern C++" by Andrei Alxandrescu.
 */

#ifndef PB_DS_TYPE_UTILS_HPP
#define PB_DS_TYPE_UTILS_HPP

#include <cstddef>
#include <utility>
#include <tr1/type_traits>
#include <ext/type_traits.h>
#include <ext/numeric_traits.h>

namespace pb_ds
{
  namespace detail
  {
    using std::tr1::is_same;
    using std::tr1::is_const;
    using std::tr1::is_pointer;
    using std::tr1::is_reference;
    using std::tr1::is_fundamental;
    using std::tr1::is_member_object_pointer;
    using std::tr1::is_member_pointer;
    using std::tr1::is_base_of;
    using std::tr1::remove_const;
    using std::tr1::remove_reference;

    // Need integral_const<bool, true> <-> integral_const<int, 1>, so
    // because of this use the following typedefs instead of importing
    // std::tr1's.
    using std::tr1::integral_constant;
    typedef std::tr1::integral_constant<int, 1> true_type;
    typedef std::tr1::integral_constant<int, 0> false_type;

    using __gnu_cxx::__conditional_type;
    using __gnu_cxx::__numeric_traits;

    template<typename T>
    struct is_const_pointer
    {
      enum
	{
	  value = is_const<T>::value && is_pointer<T>::value
	};
    };

    template<typename T>
    struct is_const_reference
    {
      enum
	{
	  value = is_const<T>::value && is_reference<T>::value
	};
    };

    template<typename T>
    struct is_simple
    {
      enum
	{
	  value = is_fundamental<typename remove_const<T>::type>::value
	  || is_pointer<typename remove_const<T>::type>::value
	  || is_member_pointer<T>::value
	};
    };

    template<typename T>
    class is_pair
    {
    private:
      template<typename U>
      struct is_pair_imp
      {
	enum
	  {
	    value = 0
	  };
      };

      template<typename U, typename V>
      struct is_pair_imp<std::pair<U,V> >
      {
	enum
	  {
	    value = 1
	  };
      };

    public:
      enum
	{
	  value = is_pair_imp<T>::value
	};
    };

    template<bool>
    struct static_assert;

    template<>
    struct static_assert<true>
    { };

    template<int>
    struct static_assert_dumclass
    {
      enum
	{
	  v = 1
	};
    };

    template<typename Type>
    struct type_to_type
    {
      typedef Type type;
    };
  } // namespace detail
} // namespace pb_ds

#endif


code:
// -*- C++ -*-

// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the terms
// of the GNU General Public License as published by the Free Software
// Foundation; either version 2, or (at your option) any later
// version.

// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this library; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
// MA 02111-1307, USA.

// As a special exception, you may use this file as part of a free
// software library without restriction.  Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to
// produce an executable, this file does not by itself cause the
// resulting executable to be covered by the GNU General Public
// License.  This exception does not however invalidate any other
// reasons why the executable file might be covered by the GNU General
// Public License.

// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.

// Permission to use, copy, modify, sell, and distribute this software
// is hereby granted without fee, provided that the above copyright
// notice appears in all copies, and that both that copyright notice
// and this permission notice appear in supporting documentation. None
// of the above authors, nor IBM Haifa Research Laboratories, make any
// representation about the suitability of this software for any
// purpose. It is provided "as is" without express or implied
// warranty.

/**
 * @file types_traits.hpp
 * Contains a traits class of types used by containers.
 */

#ifndef PB_DS_TYPES_TRAITS_HPP
#define PB_DS_TYPES_TRAITS_HPP

#include <ext/pb_ds/detail/basic_types.hpp>
#include <ext/pb_ds/detail/type_utils.hpp>
#include <utility>

namespace pb_ds
{
  namespace detail
  {
    template<typename Key, typename Mapped, typename Alloc, bool Store_Extra>
    struct vt_base_selector
    {
      typedef value_type_base<Key, Mapped, Alloc, Store_Extra> type;
    };

    template<typename Key, typename Mapped, typename Alloc, bool Store_Extra>
    struct types_traits
    : public vt_base_selector<Key, Mapped, Alloc, Store_Extra>::type
    {
      typedef typename Alloc::template rebind<Key>::other key_allocator;
      typedef typename key_allocator::value_type key_type;
      typedef typename key_allocator::pointer key_pointer;
      typedef typename key_allocator::const_pointer const_key_pointer;
      typedef typename key_allocator::reference key_reference;
      typedef typename key_allocator::const_reference const_key_reference;
      typedef typename Alloc::size_type size_type;

      // Extra value (used when the extra value is stored with each value).
      typedef std::pair<size_type, size_type> comp_hash;

      integral_constant<int, Store_Extra> m_store_extra_indicator;
      typename no_throw_copies<Key, Mapped>::indicator m_no_throw_copies_indicator;
    };
  } // namespace detail
} // namespace pb_ds

#endif


code:
// -*- C++ -*-

// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the terms
// of the GNU General Public License as published by the Free Software
// Foundation; either version 2, or (at your option) any later
// version.

// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this library; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
// MA 02111-1307, USA.

// As a special exception, you may use this file as part of a free
// software library without restriction.  Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to
// produce an executable, this file does not by itself cause the
// resulting executable to be covered by the GNU General Public
// License.  This exception does not however invalidate any other
// reasons why the executable file might be covered by the GNU General
// Public License.

// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.

// Permission to use, copy, modify, sell, and distribute this software
// is hereby granted without fee, provided that the above copyright
// notice appears in all copies, and that both that copyright notice
// and this permission notice appear in supporting documentation. None
// of the above authors, nor IBM Haifa Research Laboratories, make any
// representation about the suitability of this software for any
// purpose. It is provided "as is" without express or implied
// warranty.

/**
 * @file const_iterator.hpp
 * Contains an iterator class used for const ranging over the elements of the
 *     table.
 */

// Const range-type iterator.
class const_iterator_ :
  public const_point_iterator_

{

public:

  // Category.
  typedef std::forward_iterator_tag iterator_category;

  // Difference type.
  typedef typename Allocator::difference_type difference_type;

  // Iterator's value type.
  typedef value_type_ value_type;

  // Iterator's pointer type.
  typedef pointer_ pointer;

  // Iterator's const pointer type.
  typedef const_pointer_ const_pointer;

  // Iterator's reference type.
  typedef reference_ reference;

  // Iterator's const reference type.
  typedef const_reference_ const_reference;

public:

  // Default constructor.
  inline
  const_iterator_()

    : m_p_tbl(NULL)
  { }

  // Increments.
  inline const_iterator_&
  operator++()
  {
    m_p_tbl->inc_it_state(base_type::m_p_value, m_pos);

    return (*this);
  }

  // Increments.
  inline const_iterator_
  operator++(int)
  {
    const_iterator_ ret =* this;

    m_p_tbl->inc_it_state(base_type::m_p_value, m_pos);

    return (ret);
  }

protected:

  typedef const_point_iterator_ base_type;

protected:

  /**
   *  Constructor used by the table to initiate the generalized
   *      pointer and position (e.g., this is called from within a find()
   *      of a table.
   * */
  inline
  const_iterator_(const_pointer_ p_value,  PB_DS_GEN_POS pos,  const PB_DS_CLASS_C_DEC* p_tbl) : const_point_iterator_(p_value),
												 m_p_tbl(p_tbl),
												 m_pos(pos)
  { }

protected:

  /**
   *  Pointer to the table object which created the iterator (used for
   *      incrementing its position.
   * */
  const PB_DS_CLASS_C_DEC* m_p_tbl;

  PB_DS_GEN_POS m_pos;

  friend class PB_DS_CLASS_C_DEC;
};


code:
// -*- C++ -*-

// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the terms
// of the GNU General Public License as published by the Free Software
// Foundation; either version 2, or (at your option) any later
// version.

// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this library; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
// MA 02111-1307, USA.

// As a special exception, you may use this file as part of a free
// software library without restriction.  Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to
// produce an executable, this file does not by itself cause the
// resulting executable to be covered by the GNU General Public
// License.  This exception does not however invalidate any other
// reasons why the executable file might be covered by the GNU General
// Public License.

// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.

// Permission to use, copy, modify, sell, and distribute this software
// is hereby granted without fee, provided that the above copyright
// notice appears in all copies, and that both that copyright notice
// and this permission notice appear in supporting documentation. None
// of the above authors, nor IBM Haifa Research Laboratories, make any
// representation about the suitability of this software for any
// purpose. It is provided "as is" without express or implied
// warranty.

/**
 * @file const_point_iterator.hpp
 * Contains an iterator class returned by the tables' const find and insert
 *     methods.
 */

class point_iterator_;

// Const point-type iterator.
class const_point_iterator_
{

public:

  // Category.
  typedef trivial_iterator_tag iterator_category;

  // Difference type.
  typedef trivial_iterator_difference_type difference_type;

  // Iterator's value type.
  typedef value_type_ value_type;

  // Iterator's pointer type.
  typedef pointer_ pointer;

  // Iterator's const pointer type.
  typedef const_pointer_ const_pointer;

  // Iterator's reference type.
  typedef reference_ reference;

  // Iterator's const reference type.
  typedef const_reference_ const_reference;

public:

  inline
  const_point_iterator_(const_pointer p_value) : m_p_value(p_value)
  { }

  // Default constructor.
  inline
  const_point_iterator_()

    : m_p_value(NULL)
  { }

  // Copy constructor.
  inline
  const_point_iterator_(const const_point_iterator_& other)

    : m_p_value(other.m_p_value)
  { }

  // Copy constructor.
  inline
  const_point_iterator_(const point_iterator_& other)

    : m_p_value(other.m_p_value)
  { }

  // Access.
  inline const_pointer
  operator->() const
  {
    _GLIBCXX_DEBUG_ASSERT(m_p_value != NULL);

    return (m_p_value);
  }

  // Access.
  inline const_reference
  operator*() const
  {
    _GLIBCXX_DEBUG_ASSERT(m_p_value != NULL);

    return (*m_p_value);
  }

  // Compares content to a different iterator object.
  inline bool
  operator==(const point_iterator_& other) const
  {
    return (m_p_value == other.m_p_value);
  }

  // Compares content to a different iterator object.
  inline bool
  operator==(const const_point_iterator_& other) const
  {
    return (m_p_value == other.m_p_value);
  }

  // Compares content (negatively) to a different iterator object.
  inline bool
  operator!=(const point_iterator_& other) const
  {
    return (m_p_value != other.m_p_value);
  }

  // Compares content (negatively) to a different iterator object.
  inline bool
  operator!=(const const_point_iterator_& other) const
  {
    return (m_p_value != other.m_p_value);
  }

protected:
  const_pointer m_p_value;

  friend class point_iterator_;

  friend class PB_DS_CLASS_C_DEC;
};


code:
// -*- C++ -*-

// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the terms
// of the GNU General Public License as published by the Free Software
// Foundation; either version 2, or (at your option) any later
// version.

// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this library; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
// MA 02111-1307, USA.

// As a special exception, you may use this file as part of a free
// software library without restriction.  Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to
// produce an executable, this file does not by itself cause the
// resulting executable to be covered by the GNU General Public
// License.  This exception does not however invalidate any other
// reasons why the executable file might be covered by the GNU General
// Public License.

// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.

// Permission to use, copy, modify, sell, and distribute this software
// is hereby granted without fee, provided that the above copyright
// notice appears in all copies, and that both that copyright notice
// and this permission notice appear in supporting documentation. None
// of the above authors, nor IBM Haifa Research Laboratories, make any
// representation about the suitability of this software for any
// purpose. It is provided "as is" without express or implied
// warranty.

/**
 * @file iterator.hpp
 * Contains an iterator_ class used for ranging over the elements of the
 *    table.
 */

// Range-type iterator.
class iterator_ :
  public const_iterator_

{

public:

  // Category.
  typedef std::forward_iterator_tag iterator_category;

  // Difference type.
  typedef typename Allocator::difference_type difference_type;

  // Iterator's value type.
  typedef value_type_ value_type;

  // Iterator's pointer type.
  typedef pointer_ pointer;

  // Iterator's const pointer type.
  typedef const_pointer_ const_pointer;

  // Iterator's reference type.
  typedef reference_ reference;

  // Iterator's const reference type.
  typedef const_reference_ const_reference;

public:

  // Default constructor.
  inline
  iterator_()

    : const_iterator_(NULL, PB_DS_GEN_POS(), NULL)
  { }

  // Conversion to a point-type iterator.
  inline
  operator point_iterator_()
  {
    return (point_iterator_(
			    const_cast<pointer>(const_iterator_::m_p_value)));
  }

  // Conversion to a point-type iterator.
  inline
  operator const point_iterator_() const
  {
    return (point_iterator_(
			    const_cast<pointer>(const_iterator_::m_p_value)));
  }

  // Access.
  inline pointer
  operator->() const
  {
    _GLIBCXX_DEBUG_ASSERT(base_type::m_p_value != NULL);

    return (const_cast<pointer>(base_type::m_p_value));
  }

  // Access.
  inline reference
  operator*() const
  {
    _GLIBCXX_DEBUG_ASSERT(base_type::m_p_value != NULL);

    return (const_cast<reference>(*base_type::m_p_value));
  }

  // Increments.
  inline iterator_&
  operator++()
  {
    base_type::m_p_tbl->inc_it_state(base_type::m_p_value, base_type::m_pos);

    return (*this);
  }

  // Increments.
  inline iterator_
  operator++(int)
  {
    iterator_ ret =* this;

    base_type::m_p_tbl->inc_it_state(base_type::m_p_value, base_type::m_pos);

    return (ret);
  }

protected:
  typedef const_iterator_ base_type;

protected:

  /**
   *  Constructor used by the table to initiate the generalized
   *      pointer and position (e.g., this is called from within a find()
   *      of a table.
   * */
  inline
  iterator_(pointer p_value, PB_DS_GEN_POS pos, PB_DS_CLASS_C_DEC* p_tbl) : const_iterator_(p_value, pos, p_tbl)
  { }

  friend class PB_DS_CLASS_C_DEC;
};


code:
// -*- C++ -*-

// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the terms
// of the GNU General Public License as published by the Free Software
// Foundation; either version 2, or (at your option) any later
// version.

// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this library; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
// MA 02111-1307, USA.

// As a special exception, you may use this file as part of a free
// software library without restriction.  Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to
// produce an executable, this file does not by itself cause the
// resulting executable to be covered by the GNU General Public
// License.  This exception does not however invalidate any other
// reasons why the executable file might be covered by the GNU General
// Public License.

// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.

// Permission to use, copy, modify, sell, and distribute this software
// is hereby granted without fee, provided that the above copyright
// notice appears in all copies, and that both that copyright notice
// and this permission notice appear in supporting documentation. None
// of the above authors, nor IBM Haifa Research Laboratories, make any
// representation about the suitability of this software for any
// purpose. It is provided "as is" without express or implied
// warranty.

/**
 * @file point_iterator.hpp
 * Contains an iterator class returned by the tables' find and insert
 *     methods.
 */

// Find type iterator.
class point_iterator_
{

public:

  // Category.
  typedef trivial_iterator_tag iterator_category;

  // Difference type.
  typedef trivial_iterator_difference_type difference_type;

  // Iterator's value type.
  typedef value_type_ value_type;

  // Iterator's pointer type.
  typedef pointer_ pointer;

  // Iterator's const pointer type.
  typedef const_pointer_ const_pointer;

  // Iterator's reference type.
  typedef reference_ reference;

  // Iterator's const reference type.
  typedef const_reference_ const_reference;

public:

  // Default constructor.
  inline
  point_iterator_()

    : m_p_value(NULL)
  { }

  // Copy constructor.
  inline
  point_iterator_(const point_iterator_& other)

    : m_p_value(other.m_p_value)
  { }

  // Access.
  inline pointer
  operator->() const
  {
    _GLIBCXX_DEBUG_ASSERT(m_p_value != NULL);

    return (m_p_value);
  }

  // Access.
  inline reference
  operator*() const
  {
    _GLIBCXX_DEBUG_ASSERT(m_p_value != NULL);

    return (*m_p_value);
  }

  // Compares content to a different iterator object.
  inline bool
  operator==(const point_iterator_& other) const
  {
    return (m_p_value == other.m_p_value);
  }

  // Compares content to a different iterator object.
  inline bool
  operator==(const const_point_iterator_& other) const
  {
    return (m_p_value == other.m_p_value);
  }

  // Compares content to a different iterator object.
  inline bool
  operator!=(const point_iterator_& other) const
  {
    return (m_p_value != other.m_p_value);
  }

  // Compares content (negatively) to a different iterator object.
  inline bool
  operator!=(const const_point_iterator_& other) const
  {
    return (m_p_value != other.m_p_value);
  }

  inline
  point_iterator_(pointer p_value) : m_p_value(p_value)
  { }

protected:
  friend class const_point_iterator_;

  friend class PB_DS_CLASS_C_DEC;

protected:
  pointer m_p_value;
};


code:
// -*- C++ -*-

// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the terms
// of the GNU General Public License as published by the Free Software
// Foundation; either version 2, or (at your option) any later
// version.

// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this library; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
// MA 02111-1307, USA.

// As a special exception, you may use this file as part of a free
// software library without restriction.  Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to
// produce an executable, this file does not by itself cause the
// resulting executable to be covered by the GNU General Public
// License.  This exception does not however invalidate any other
// reasons why the executable file might be covered by the GNU General
// Public License.

// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.

// Permission to use, copy, modify, sell, and distribute this software
// is hereby granted without fee, provided that the above copyright
// notice appears in all copies, and that both that copyright notice
// and this permission notice appear in supporting documentation. None
// of the above authors, nor IBM Haifa Research Laboratories, make any
// representation about the suitability of this software for any
// purpose. It is provided "as is" without express or implied
// warranty.

/**
 * @file exception.hpp
 * Contains exception classes.
 */

#ifndef PB_DS_EXCEPTION_HPP
#define PB_DS_EXCEPTION_HPP

#include <stdexcept>

namespace pb_ds
{
  // Base class for exceptions.
  struct container_error : public std::logic_error
  {
    container_error()
    : std::logic_error(__N("pb_ds::container_error")) { }
  };

  // An entry cannot be inserted into a container object for logical
  // reasons (not, e.g., if memory is unabvailable, in which case
  // the allocator's exception will be thrown).
  struct insert_error : public container_error { };

  // A join cannot be performed logical reasons (i.e., the ranges of
  // the two container objects being joined overlaps.
  struct join_error : public container_error { };

  // A container cannot be resized.
  struct resize_error : public container_error { };

#if __EXCEPTIONS
  void
  __throw_container_error(void)
  { throw container_error(); }

  void
  __throw_insert_error(void)
  { throw insert_error(); }

  void
  __throw_join_error(void)
  { throw join_error(); }

  void
  __throw_resize_error(void)
  { throw resize_error(); }
#else
  void
  __throw_container_error(void)
  { std::abort(); }

  void
  __throw_insert_error(void)
  { std::abort(); }

  void
  __throw_join_error(void)
  { std::abort(); }

  void
  __throw_resize_error(void)
  { std::abort(); }
#endif
} // namespace pb_ds

#endif


code:
// -*- C++ -*-

// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the terms
// of the GNU General Public License as published by the Free Software
// Foundation; either version 2, or (at your option) any later
// version.

// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this library; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
// MA 02111-1307, USA.

// As a special exception, you may use this file as part of a free
// software library without restriction.  Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to
// produce an executable, this file does not by itself cause the
// resulting executable to be covered by the GNU General Public
// License.  This exception does not however invalidate any other
// reasons why the executable file might be covered by the GNU General
// Public License.

// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.

// Permission to use, copy, modify, sell, and distribute this software
// is hereby granted without fee, provided that the above copyright
// notice appears in all copies, and that both that copyright notice
// and this permission notice appear in supporting documentation. None
// of the above authors, nor IBM Haifa Research Laboratories, make any
// representation about the suitability of this software for any
// purpose. It is provided "as is" without express or implied
// warranty.

/**
 * @file hash_policy.hpp
 * Contains hash-related policies.
 */

#ifndef PB_DS_HASH_POLICY_HPP
#define PB_DS_HASH_POLICY_HPP

#include <algorithm>
#include <vector>
#include <cmath>
#include <ext/pb_ds/exception.hpp>
#include <ext/pb_ds/detail/type_utils.hpp>
#include <ext/pb_ds/detail/hash_fn/mask_based_range_hashing.hpp>
#include <ext/pb_ds/detail/hash_fn/mod_based_range_hashing.hpp>
#include <ext/pb_ds/detail/resize_policy/hash_load_check_resize_trigger_size_base.hpp>

namespace pb_ds
{
  // A null hash function, indicating that the combining hash function
  // is actually a ranged hash function.
  struct null_hash_fn
  { };

  // A null probe function, indicating that the combining probe
  // function is actually a ranged probe function.
  struct null_probe_fn
  { };

#define PB_DS_CLASS_T_DEC template<typename Size_Type>
#define PB_DS_CLASS_C_DEC linear_probe_fn<Size_Type>

  // A probe sequence policy using fixed increments.
  template<typename Size_Type = size_t>
  class linear_probe_fn
  {
  public:
    typedef Size_Type size_type;

    void
    swap(PB_DS_CLASS_C_DEC& other);

  protected:
    // Returns the i-th offset from the hash value.
    inline size_type
    operator()(size_type i) const;
  };

#include <ext/pb_ds/detail/hash_fn/linear_probe_fn_imp.hpp>

#undef PB_DS_CLASS_T_DEC
#undef PB_DS_CLASS_C_DEC

#define PB_DS_CLASS_T_DEC template<typename Size_Type>
#define PB_DS_CLASS_C_DEC quadratic_probe_fn<Size_Type>

  // A probe sequence policy using square increments.
  template<typename Size_Type = size_t>
  class quadratic_probe_fn
  {
  public:
    typedef Size_Type size_type;

    void
    swap(PB_DS_CLASS_C_DEC& other);

  protected:
    // Returns the i-th offset from the hash value.
    inline size_type
    operator()(size_type i) const;
  };

#include <ext/pb_ds/detail/hash_fn/quadratic_probe_fn_imp.hpp>

#undef PB_DS_CLASS_T_DEC
#undef PB_DS_CLASS_C_DEC

#define PB_DS_CLASS_T_DEC template<typename Size_Type>
#define PB_DS_CLASS_C_DEC direct_mask_range_hashing<Size_Type>

  // A mask range-hashing class (uses a bit-mask).
  template<typename Size_Type = size_t>
  class direct_mask_range_hashing
  : public detail::mask_based_range_hashing<Size_Type>
  {
  private:
    typedef detail::mask_based_range_hashing<Size_Type> mask_based_base;

  public:
    typedef Size_Type size_type;

    void
    swap(PB_DS_CLASS_C_DEC& other);

  protected:
    void
    notify_resized(size_type size);

    // Transforms the __hash value hash into a ranged-hash value
    // (using a bit-mask).
    inline size_type
    operator()(size_type hash) const;
  };

#include <ext/pb_ds/detail/hash_fn/direct_mask_range_hashing_imp.hpp>

#undef PB_DS_CLASS_T_DEC
#undef PB_DS_CLASS_C_DEC

#define PB_DS_CLASS_T_DEC template<typename Size_Type>
#define PB_DS_CLASS_C_DEC direct_mod_range_hashing<Size_Type>

  // A mod range-hashing class (uses the modulo function).
  template<typename Size_Type = size_t>
  class direct_mod_range_hashing
  : public detail::mod_based_range_hashing<Size_Type>
  {
  public:
    typedef Size_Type size_type;

    void
    swap(PB_DS_CLASS_C_DEC& other);

  protected:
    void
    notify_resized(size_type size);

    // Transforms the __hash value hash into a ranged-hash value
    // (using a modulo operation).
    inline size_type
    operator()(size_type hash) const;

  private:
    typedef detail::mod_based_range_hashing<size_type> mod_based_base;
  };

#include <ext/pb_ds/detail/hash_fn/direct_mod_range_hashing_imp.hpp>

#undef PB_DS_CLASS_T_DEC
#undef PB_DS_CLASS_C_DEC

#define PB_DS_CLASS_T_DEC template<bool External_Load_Access, typename Size_Type>
#define PB_DS_CLASS_C_DEC hash_load_check_resize_trigger<External_Load_Access, Size_Type>
#define PB_DS_SIZE_BASE_C_DEC detail::hash_load_check_resize_trigger_size_base<Size_Type, External_Load_Access>

  // A resize trigger policy based on a load check. It keeps the
  // load factor between some load factors load_min and load_max.
  template<bool External_Load_Access = false, typename Size_Type = size_t>
  class hash_load_check_resize_trigger : private PB_DS_SIZE_BASE_C_DEC
  {
  public:
    typedef Size_Type size_type;

    enum
      {
	external_load_access = External_Load_Access
      };

    // Default constructor, or constructor taking load_min and
    // load_max load factors between which this policy will keep the
    // actual load.
    hash_load_check_resize_trigger(float load_min = 0.125,
				   float load_max = 0.5);

    void
    swap(hash_load_check_resize_trigger& other);

    virtual
    ~hash_load_check_resize_trigger();

    // Returns a pair of the minimal and maximal loads, respectively.
    inline std::pair<float, float>
    get_loads() const;

    // Sets the loads through a pair of the minimal and maximal
    // loads, respectively.
    void
    set_loads(std::pair<float, float> load_pair);

  protected:
    inline void
    notify_insert_search_start();

    inline void
    notify_insert_search_collision();

    inline void
    notify_insert_search_end();

    inline void
    notify_find_search_start();

    inline void
    notify_find_search_collision();

    inline void
    notify_find_search_end();

    inline void
    notify_erase_search_start();

    inline void
    notify_erase_search_collision();

    inline void
    notify_erase_search_end();

    // Notifies an element was inserted. The total number of entries
    // in the table is num_entries.
    inline void
    notify_inserted(size_type num_entries);

    inline void
    notify_erased(size_type num_entries);

    // Notifies the table was cleared.
    void
    notify_cleared();

    // Notifies the table was resized as a result of this object's
    // signifying that a resize is needed.
    void
    notify_resized(size_type new_size);

    void
    notify_externally_resized(size_type new_size);

    inline bool
    is_resize_needed() const;

    inline bool
    is_grow_needed(size_type size, size_type num_entries) const;

  private:
    virtual void
    do_resize(size_type new_size);

    typedef PB_DS_SIZE_BASE_C_DEC size_base;

#ifdef _GLIBCXX_DEBUG
    void
    assert_valid() const;
#endif

    float 	m_load_min;
    float 	m_load_max;
    size_type 	m_next_shrink_size;
    size_type 	m_next_grow_size;
    bool 	m_resize_needed;
  };

#include <ext/pb_ds/detail/resize_policy/hash_load_check_resize_trigger_imp.hpp>

#undef PB_DS_CLASS_T_DEC
#undef PB_DS_CLASS_C_DEC
#undef PB_DS_SIZE_BASE_C_DEC

#define PB_DS_CLASS_T_DEC template<bool External_Load_Access, typename Size_Type>
#define PB_DS_CLASS_C_DEC cc_hash_max_collision_check_resize_trigger<External_Load_Access, Size_Type>

  // A resize trigger policy based on collision checks. It keeps the
  // simulated load factor lower than some given load factor.
  template<bool External_Load_Access = false, typename Size_Type = size_t>
  class cc_hash_max_collision_check_resize_trigger
  {
  public:
    typedef Size_Type size_type;

    enum
      {
	external_load_access = External_Load_Access
      };

    // Default constructor, or constructor taking load, a __load
    // factor which it will attempt to maintain.
    cc_hash_max_collision_check_resize_trigger(float load = 0.5);

    void
    swap(PB_DS_CLASS_C_DEC& other);

    // Returns the current load.
    inline float
    get_load() const;

    // Sets the load; does not resize the container.
    void
    set_load(float load);

  protected:
    inline void
    notify_insert_search_start();

    inline void
    notify_insert_search_collision();

    inline void
    notify_insert_search_end();

    inline void
    notify_find_search_start();

    inline void
    notify_find_search_collision();

    inline void
    notify_find_search_end();

    inline void
    notify_erase_search_start();

    inline void
    notify_erase_search_collision();

    inline void
    notify_erase_search_end();

    inline void
    notify_inserted(size_type num_entries);

    inline void
    notify_erased(size_type num_entries);

    void
    notify_cleared();

    // Notifies the table was resized as a result of this object's
    // signifying that a resize is needed.
    void
    notify_resized(size_type new_size);

    void
    notify_externally_resized(size_type new_size);

    inline bool
    is_resize_needed() const;

    inline bool
    is_grow_needed(size_type size, size_type num_entries) const;

  private:
    void
    calc_max_num_coll();

    inline void
    calc_resize_needed();

    float 	m_load;
    size_type 	m_size;
    size_type 	m_num_col;
    size_type 	m_max_col;
    bool 	m_resize_needed;
  };

#include <ext/pb_ds/detail/resize_policy/cc_hash_max_collision_check_resize_trigger_imp.hpp>

#undef PB_DS_CLASS_T_DEC
#undef PB_DS_CLASS_C_DEC

#define PB_DS_CLASS_T_DEC template<typename Size_Type>
#define PB_DS_CLASS_C_DEC hash_exponential_size_policy<Size_Type>

  // A size policy whose sequence of sizes form an exponential
  // sequence (typically powers of 2.
  template<typename Size_Type = size_t>
  class hash_exponential_size_policy
  {
  public:
    typedef Size_Type size_type;

    // Default constructor, or onstructor taking a start_size, or
    // constructor taking a start size and grow_factor. The policy
    // will use the sequence of sizes start_size, start_size*
    // grow_factor, start_size* grow_factor^2, ...
    hash_exponential_size_policy(size_type start_size = 8,
				 size_type grow_factor = 2);

    void
    swap(PB_DS_CLASS_C_DEC& other);

  protected:
    size_type
    get_nearest_larger_size(size_type size) const;

    size_type
    get_nearest_smaller_size(size_type size) const;

  private:
    size_type m_start_size;
    size_type m_grow_factor;
  };

#include <ext/pb_ds/detail/resize_policy/hash_exponential_size_policy_imp.hpp>

#undef PB_DS_CLASS_T_DEC
#undef PB_DS_CLASS_C_DEC

#define PB_DS_CLASS_T_DEC
#define PB_DS_CLASS_C_DEC hash_prime_size_policy

  // A size policy whose sequence of sizes form a nearly-exponential
  // sequence of primes.
  class hash_prime_size_policy
  {
  public:
    // Size type.
    typedef size_t size_type;

    // Default constructor, or onstructor taking a start_size The
    // policy will use the sequence of sizes approximately
    // start_size, start_size* 2, start_size* 2^2, ...
    hash_prime_size_policy(size_type start_size = 8);

    inline void
    swap(PB_DS_CLASS_C_DEC& other);

  protected:
    size_type
    get_nearest_larger_size(size_type size) const;

    size_type
    get_nearest_smaller_size(size_type size) const;

  private:
    size_type m_start_size;
  };

#include <ext/pb_ds/detail/resize_policy/hash_prime_size_policy_imp.hpp>

#undef PB_DS_CLASS_T_DEC
#undef PB_DS_CLASS_C_DEC

#define PB_DS_CLASS_T_DEC template<typename Size_Policy, typename Trigger_Policy, bool External_Size_Access, typename Size_Type>

#define PB_DS_CLASS_C_DEC hash_standard_resize_policy<Size_Policy, Trigger_Policy, External_Size_Access, Size_Type>

  // A resize policy which delegates operations to size and trigger policies.
  template<typename Size_Policy = hash_exponential_size_policy<>,
	   typename Trigger_Policy = hash_load_check_resize_trigger<>,
	   bool External_Size_Access = false,
	   typename Size_Type = size_t>
  class hash_standard_resize_policy
  : public Size_Policy, public Trigger_Policy
  {
  public:
    typedef Size_Type 		size_type;
    typedef Trigger_Policy 	trigger_policy;
    typedef Size_Policy 	size_policy;

    enum
      {
	external_size_access = External_Size_Access
      };

    // Default constructor.
    hash_standard_resize_policy();

    // constructor taking some policies r_size_policy will be copied
    // by the Size_Policy object of this object.
    hash_standard_resize_policy(const Size_Policy& r_size_policy);

    // constructor taking some policies. r_size_policy will be
    // copied by the Size_Policy object of this
    // object. r_trigger_policy will be copied by the Trigger_Policy
    // object of this object.
    hash_standard_resize_policy(const Size_Policy& r_size_policy,
				const Trigger_Policy& r_trigger_policy);

    virtual
    ~hash_standard_resize_policy();

    inline void
    swap(PB_DS_CLASS_C_DEC& other);

    // Access to the Size_Policy object used.
    Size_Policy&
    get_size_policy();

    // Const access to the Size_Policy object used.
    const Size_Policy&
    get_size_policy() const;

    // Access to the Trigger_Policy object used.
    Trigger_Policy&
    get_trigger_policy();

    // Access to the Trigger_Policy object used.
    const Trigger_Policy&
    get_trigger_policy() const;

    // Returns the actual size of the container.
    inline size_type
    get_actual_size() const;

    // Resizes the container to suggested_new_size, a suggested size
    // (the actual size will be determined by the Size_Policy
    // object).
    void
    resize(size_type suggested_new_size);

  protected:
    inline void
    notify_insert_search_start();

    inline void
    notify_insert_search_collision();

    inline void
    notify_insert_search_end();

    inline void
    notify_find_search_start();

    inline void
    notify_find_search_collision();

    inline void
    notify_find_search_end();

    inline void
    notify_erase_search_start();

    inline void
    notify_erase_search_collision();

    inline void
    notify_erase_search_end();

    inline void
    notify_inserted(size_type num_e);

    inline void
    notify_erased(size_type num_e);

    void
    notify_cleared();

    void
    notify_resized(size_type new_size);

    inline bool
    is_resize_needed() const;

    // Queries what the new size should be, when the container is
    // resized naturally. The current __size of the container is
    // size, and the number of used entries within the container is
    // num_used_e.
    size_type
    get_new_size(size_type size, size_type num_used_e) const;

  private:
    // Resizes to new_size.
    virtual void
    do_resize(size_type new_size);

    typedef Trigger_Policy trigger_policy_base;

    typedef Size_Policy size_policy_base;

    size_type m_size;
  };

#include <ext/pb_ds/detail/resize_policy/hash_standard_resize_policy_imp.hpp>

#undef PB_DS_CLASS_T_DEC
#undef PB_DS_CLASS_C_DEC

} // namespace pb_ds

#endif


code:
// -*- C++ -*-

// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the terms
// of the GNU General Public License as published by the Free Software
// Foundation; either version 2, or (at your option) any later
// version.

// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this library; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
// MA 02111-1307, USA.

// As a special exception, you may use this file as part of a free
// software library without restriction.  Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to
// produce an executable, this file does not by itself cause the
// resulting executable to be covered by the GNU General Public
// License.  This exception does not however invalidate any other
// reasons why the executable file might be covered by the GNU General
// Public License.

// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.

// Permission to use, copy, modify, sell, and distribute this software
// is hereby granted without fee, provided that the above copyright
// notice appears in all copies, and that both that copyright notice
// and this permission notice appear in supporting documentation. None
// of the above authors, nor IBM Haifa Research Laboratories, make any
// representation about the suitability of this software for any
// purpose. It is provided "as is" without express or implied
// warranty.

/**
 * @file list_update_policy.hpp
 * Contains policies for list update containers.
 */

#ifndef PB_DS_LU_POLICY_HPP
#define PB_DS_LU_POLICY_HPP

#include <ext/pb_ds/detail/list_update_policy/counter_lu_metadata.hpp>

namespace pb_ds
{
  // A null type that means that each link in a list-based container
  // does not actually need metadata.
  struct null_lu_metadata
  { };

#define PB_DS_CLASS_T_DEC template<typename Allocator>
#define PB_DS_CLASS_C_DEC move_to_front_lu_policy<Allocator>

  // A list-update policy that unconditionally moves elements to the
  // front of the list.
  template<typename Allocator = std::allocator<char> >
  class move_to_front_lu_policy
  {
  public:
    typedef Allocator allocator;

    // Metadata on which this functor operates.
    typedef null_lu_metadata metadata_type;

    // Reference to metadata on which this functor operates.
    typedef typename allocator::template rebind<metadata_type>::other metadata_rebind;
    typedef typename metadata_rebind::reference metadata_reference;

    // Creates a metadata object.
    metadata_type
    operator()() const;

    // Decides whether a metadata object should be moved to the front
    // of the list.
    inline bool
    operator()(metadata_reference r_metadata) const;

  private:
    static null_lu_metadata s_metadata;
  };

#include <ext/pb_ds/detail/list_update_policy/mtf_lu_policy_imp.hpp>

#undef PB_DS_CLASS_T_DEC
#undef PB_DS_CLASS_C_DEC

#define PB_DS_CLASS_T_DEC template<size_t Max_Count, class Allocator>
#define PB_DS_CLASS_C_DEC counter_lu_policy<Max_Count, Allocator>

  // A list-update policy that moves elements to the front of the list
  // based on the counter algorithm.
  template<size_t Max_Count = 5, typename Allocator = std::allocator<char> >
  class counter_lu_policy
  : private detail::counter_lu_policy_base<typename Allocator::size_type>
  {
  public:
    typedef Allocator allocator;

    enum
      {
	max_count = Max_Count
      };

    typedef typename allocator::size_type size_type;

    // Metadata on which this functor operates.
    typedef detail::counter_lu_metadata<size_type> metadata_type;

    // Reference to metadata on which this functor operates.
    typedef typename Allocator::template rebind<metadata_type>::other metadata_rebind;
    typedef typename metadata_rebind::reference metadata_reference;

    // Creates a metadata object.
    metadata_type
    operator()() const;

    // Decides whether a metadata object should be moved to the front
    // of the list.
    bool
    operator()(metadata_reference r_metadata) const;

  private:
    typedef detail::counter_lu_policy_base<typename Allocator::size_type> base_type;
  };

#include <ext/pb_ds/detail/list_update_policy/counter_lu_policy_imp.hpp>

#undef PB_DS_CLASS_T_DEC
#undef PB_DS_CLASS_C_DEC

} // namespace pb_ds

#endif