This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[libstdc++] Separate distance(i,j,n) extension from internal routines.


This has been tossed around on the libstdc++ list lately.  This particular
patch was tested on i686-linux; the general idea met with approval from
everybody.  Paolo is waiting on this one so he can continue with other
things, so I'm checking it in before I leave for the day.

Summary:  the standard version of std::distance takes two arguments and
returns a value.  HP/SGI had an overloaded version taking three arguments
and returning nothing.  As part of moving such extensions out of the main
code body and into the ext/* directory, we discovered that their core STL
code happened to use these extensions.

This cleans up distance(i,j,n) into n=distance(i,j) and moves the extension
into a new file.  Other functions are also being used in core code; we're
handling them on a case-by-case basis.


2001-12-31  Phil Edwards  <pme@gcc.gnu.org>

	* include/bits/stl_bvector.h:  Change calls to 3-argument distance()
	into standard 2-argument version.
	* include/bits/stl_deque.h:  Likewise.
	* include/bits/stl_tempbuf.h:  Likewise.
	* include/bits/stl_tree.h:  Likewise.
	* include/bits/stl_vector.h:  Likewise.
	* include/ext/stl_hashtable.h:  Likewise.
	* include/bits/stl_iterator_base_funcs.h:  Move distance() extension...
	* include/ext/iterator:  to here.  New file.
	* include/Makefile.am (ext_headers):  Add new file, alphabatize.
	* include/Makefile.in:  Regenerate.


Index: include/Makefile.am
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/include/Makefile.am,v
retrieving revision 1.15
diff -u -3 -p -r1.15 Makefile.am
--- Makefile.am	2001/12/28 18:46:53	1.15
+++ Makefile.am	2001/12/31 16:05:40
@@ -173,14 +173,15 @@ ext_srcdir = ${glibcpp_srcdir}/include/e
 ext_builddir = ./ext
 ext_headers = \
 	${ext_srcdir}/algorithm \
+	${ext_srcdir}/hash_map \
+	${ext_srcdir}/hash_set \
+	${ext_srcdir}/iterator \
 	${ext_srcdir}/rope \
 	${ext_srcdir}/ropeimpl.h \
-	${ext_srcdir}/stl_rope.h \
 	${ext_srcdir}/slist \
-	${ext_srcdir}/hash_map \
-	${ext_srcdir}/hash_set \
+	${ext_srcdir}/stl_hash_fun.h \
 	${ext_srcdir}/stl_hashtable.h \
-	${ext_srcdir}/stl_hash_fun.h
+	${ext_srcdir}/stl_rope.h
 
 # This is the common subset of files that all three "C" header models use.
 c_base_srcdir = @C_INCLUDE_DIR@/bits
Index: include/bits/stl_bvector.h
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/include/bits/stl_bvector.h,v
retrieving revision 1.9
diff -u -3 -p -r1.9 stl_bvector.h
--- stl_bvector.h	2001/12/19 21:57:42	1.9
+++ stl_bvector.h	2001/12/31 16:05:43
@@ -416,8 +416,7 @@ template <typename _Alloc> 
     template <class _ForwardIterator>
     void _M_initialize_range(_ForwardIterator __first, _ForwardIterator __last,
                              forward_iterator_tag) {
-      size_type __n = 0;
-      distance(__first, __last, __n);
+      size_type __n = distance(__first, __last);
       _M_initialize(__n);
       copy(__first, __last, _M_start);
     }
@@ -437,8 +436,7 @@ template <typename _Alloc> 
                          _ForwardIterator __first, _ForwardIterator __last,
                          forward_iterator_tag) {
       if (__first != __last) {
-        size_type __n = 0;
-        distance(__first, __last, __n);
+        size_type __n = distance(__first, __last);
         if (capacity() - size() >= __n) {
           copy_backward(__position, end(), _M_finish + difference_type(__n));
           copy(__first, __last, __position);
@@ -600,8 +598,7 @@ template <typename _Alloc> 
     template <class _ForwardIterator>
     void _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
                        forward_iterator_tag) {
-      size_type __len = 0;
-      distance(__first, __last, __len);
+      size_type __len = distance(__first, __last);
       if (__len < size())
         erase(copy(__first, __last, begin()), end());
       else {
Index: include/bits/stl_deque.h
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/include/bits/stl_deque.h,v
retrieving revision 1.16
diff -u -3 -p -r1.16 stl_deque.h
--- stl_deque.h	2001/12/31 14:53:47	1.16
+++ stl_deque.h	2001/12/31 16:05:47
@@ -695,8 +695,7 @@ private:                        // helpe
   template <class _ForwardIterator>
   void _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
                      forward_iterator_tag) {
-    size_type __len = 0;
-    distance(__first, __last, __len);
+    size_type __len = distance(__first, __last);
     if (__len > size()) {
       _ForwardIterator __mid = __first;
       advance(__mid, size());
@@ -1097,8 +1096,7 @@ void deque<_Tp,_Alloc>::_M_range_initial
                                             _ForwardIterator __last,
                                             forward_iterator_tag)
 {
-  size_type __n = 0;
-  distance(__first, __last, __n);
+  size_type __n = distance(__first, __last);
   _M_initialize_map(__n);
 
   _Map_pointer __cur_node;
@@ -1237,8 +1235,7 @@ void
 deque<_Tp,_Alloc>::insert(iterator __pos,
                           _ForwardIterator __first, _ForwardIterator __last,
                           forward_iterator_tag) {
-  size_type __n = 0;
-  distance(__first, __last, __n);
+  size_type __n = distance(__first, __last);
   if (__pos._M_cur == _M_start._M_cur) {
     iterator __new_start = _M_reserve_elements_at_front(__n);
     try {
Index: include/bits/stl_iterator_base_funcs.h
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/include/bits/stl_iterator_base_funcs.h,v
retrieving revision 1.11
diff -u -3 -p -r1.11 stl_iterator_base_funcs.h
--- stl_iterator_base_funcs.h	2001/12/06 20:29:31	1.11
+++ stl_iterator_base_funcs.h	2001/12/31 16:05:47
@@ -71,40 +71,6 @@
 
 namespace std
 {
-
-  // There are two signatures for distance.  In addition to the one taking
-  // two iterators and returning a result, there is another taking two
-  // iterators and a reference-to-result variable, and returning nothing.
-  // The latter seems to be an SGI extension.   -- pedwards
-  template<typename _InputIterator, typename _Distance>
-    inline void
-    __distance(_InputIterator __first, _InputIterator __last,
-	       _Distance& __n, input_iterator_tag)
-    {
-      // concept requirements
-      __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
-      while (__first != __last) { ++__first; ++__n; }
-    }
-
-  template<typename _RandomAccessIterator, typename _Distance>
-    inline void
-    __distance(_RandomAccessIterator __first, _RandomAccessIterator __last, 
-	       _Distance& __n, random_access_iterator_tag)
-    {
-      // concept requirements
-      __glibcpp_function_requires(_RandomAccessIteratorConcept<_RandomAccessIterator>)
-      __n += __last - __first;
-    }
-
-  template<typename _InputIterator, typename _Distance>
-    inline void
-    distance(_InputIterator __first, _InputIterator __last,
-             _Distance& __n)
-    {
-      // concept requirements -- taken care of in __distance
-      __distance(__first, __last, __n, __iterator_category(__first));
-    }
-
   template<typename _InputIterator>
     inline typename iterator_traits<_InputIterator>::difference_type
     __distance(_InputIterator __first, _InputIterator __last, input_iterator_tag)
Index: include/bits/stl_tempbuf.h
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/include/bits/stl_tempbuf.h,v
retrieving revision 1.10
diff -u -3 -p -r1.10 stl_tempbuf.h
--- stl_tempbuf.h	2001/12/19 21:57:42	1.10
+++ stl_tempbuf.h	2001/12/31 16:05:47
@@ -137,8 +137,7 @@ public:
             _Trivial;
 
     try {
-      _M_len = 0;
-      distance(__first, __last, _M_len);
+      _M_len = distance(__first, __last);
       _M_allocate_buffer();
       if (_M_len > 0)
         _M_initialize_buffer(*__first, _Trivial());
Index: include/bits/stl_tree.h
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/include/bits/stl_tree.h,v
retrieving revision 1.11
diff -u -3 -p -r1.11 stl_tree.h
--- stl_tree.h	2001/12/06 20:29:31	1.11
+++ stl_tree.h	2001/12/31 16:05:50
@@ -1022,8 +1022,7 @@ typename _Rb_tree<_Key,_Value,_KeyOfValu
 _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>::erase(const _Key& __x)
 {
   pair<iterator,iterator> __p = equal_range(__x);
-  size_type __n = 0;
-  distance(__p.first, __p.second, __n);
+  size_type __n = distance(__p.first, __p.second);
   erase(__p.first, __p.second);
   return __n;
 }
@@ -1139,8 +1138,7 @@ _Rb_tree<_Key,_Value,_KeyOfValue,_Compar
   ::count(const _Key& __k) const
 {
   pair<const_iterator, const_iterator> __p = equal_range(__k);
-  size_type __n = 0;
-  distance(__p.first, __p.second, __n);
+  size_type __n = distance(__p.first, __p.second);
   return __n;
 }
 
Index: include/bits/stl_vector.h
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/include/bits/stl_vector.h,v
retrieving revision 1.15
diff -u -3 -p -r1.15 stl_vector.h
--- stl_vector.h	2001/12/12 03:22:24	1.15
+++ stl_vector.h	2001/12/31 16:05:51
@@ -462,8 +462,7 @@ protected:
   void _M_range_initialize(_ForwardIterator __first,
                            _ForwardIterator __last, forward_iterator_tag)
   {
-    size_type __n = 0;
-    distance(__first, __last, __n);
+    size_type __n = distance(__first, __last);
     _M_start = _M_allocate(__n);
     _M_end_of_storage = _M_start + __n;
     _M_finish = uninitialized_copy(__first, __last, _M_start);
@@ -583,8 +582,7 @@ template <class _Tp, class _Alloc> templ
 void
 vector<_Tp, _Alloc>::_M_assign_aux(_ForwardIter __first, _ForwardIter __last,
                                    forward_iterator_tag) {
-  size_type __len = 0;
-  distance(__first, __last, __len);
+  size_type __len = distance(__first, __last);
 
   if (__len > capacity()) {
     pointer __tmp(_M_allocate_and_copy(__len, __first, __last));
@@ -752,8 +750,7 @@ vector<_Tp, _Alloc>::_M_range_insert(ite
                                      forward_iterator_tag)
 {
   if (__first != __last) {
-    size_type __n = 0;
-    distance(__first, __last, __n);
+    size_type __n = distance(__first, __last);
     if (size_type(_M_end_of_storage - _M_finish) >= __n) {
       const size_type __elems_after = end() - __position;
       iterator __old_finish(_M_finish);
Index: include/ext/iterator
===================================================================
RCS file: iterator
diff -N iterator
--- /dev/null	Tue May  5 13:32:27 1998
+++ iterator	Mon Dec 31 08:05:51 2001
@@ -0,0 +1,106 @@
+// HP/SGI iterator extensions -*- C++ -*-
+
+// Copyright (C) 2001 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) 1994
+ * Hewlett-Packard Company
+ *
+ * Permission to use, copy, modify, distribute and sell this software
+ * and its documentation for any purpose is hereby granted without fee,
+ * provided that the above copyright notice appear in all copies and
+ * that both that copyright notice and this permission notice appear
+ * in supporting documentation.  Hewlett-Packard Company makes no
+ * representations about the suitability of this software for any
+ * purpose.  It is provided "as is" without express or implied warranty.
+ *
+ *
+ * Copyright (c) 1996-1998
+ * Silicon Graphics Computer Systems, Inc.
+ *
+ * Permission to use, copy, modify, distribute and sell this software
+ * and its documentation for any purpose is hereby granted without fee,
+ * provided that the above copyright notice appear in all copies and
+ * that both that copyright notice and this permission notice appear
+ * in supporting documentation.  Silicon Graphics makes no
+ * representations about the suitability of this software for any
+ * purpose.  It is provided "as is" without express or implied warranty.
+ */
+
+/*  @file dont_know_what_to_call_this_yet
+ *  This header file is an extension to the Standard C++ Library.  You should
+ *  use the "ext/" path prefix in your @c #include statements.
+ */
+
+#ifndef _EXT_ITERATOR
+#define _EXT_ITERATOR
+
+#pragma GCC system_header
+#include <bits/concept_check.h>
+
+namespace __gnu_cxx
+{
+
+  // There are two signatures for distance.  In addition to the one taking
+  // two iterators and returning a result, there is another taking two
+  // iterators and a reference-to-result variable, and returning nothing.
+  // The latter seems to be an SGI extension.   -- pedwards
+  template<typename _InputIterator, typename _Distance>
+    inline void
+    __distance(_InputIterator __first, _InputIterator __last,
+	       _Distance& __n, input_iterator_tag)
+    {
+      // concept requirements
+      __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
+      while (__first != __last) { ++__first; ++__n; }
+    }
+
+  template<typename _RandomAccessIterator, typename _Distance>
+    inline void
+    __distance(_RandomAccessIterator __first, _RandomAccessIterator __last, 
+	       _Distance& __n, random_access_iterator_tag)
+    {
+      // concept requirements
+      __glibcpp_function_requires(_RandomAccessIteratorConcept<_RandomAccessIterator>)
+      __n += __last - __first;
+    }
+
+  template<typename _InputIterator, typename _Distance>
+    inline void
+    distance(_InputIterator __first, _InputIterator __last,
+             _Distance& __n)
+    {
+      // concept requirements -- taken care of in __distance
+      __distance(__first, __last, __n, __iterator_category(__first));
+    }
+
+} // namespace __gnu_cxx
+
+#endif /* _EXT_ITERATOR */
+
Index: include/ext/stl_hashtable.h
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/include/ext/stl_hashtable.h,v
retrieving revision 1.12
diff -u -3 -p -r1.12 stl_hashtable.h
--- stl_hashtable.h	2001/12/31 10:22:00	1.12
+++ stl_hashtable.h	2001/12/31 16:05:53
@@ -422,8 +422,7 @@ public:
   void insert_unique(_ForwardIterator __f, _ForwardIterator __l,
                      forward_iterator_tag)
   {
-    size_type __n = 0;
-    distance(__f, __l, __n);
+    size_type __n = distance(__f, __l);
     resize(_M_num_elements + __n);
     for ( ; __n > 0; --__n, ++__f)
       insert_unique_noresize(*__f);
@@ -433,8 +432,7 @@ public:
   void insert_equal(_ForwardIterator __f, _ForwardIterator __l,
                     forward_iterator_tag)
   {
-    size_type __n = 0;
-    distance(__f, __l, __n);
+    size_type __n = distance(__f, __l);
     resize(_M_num_elements + __n);
     for ( ; __n > 0; --__n, ++__f)
       insert_equal_noresize(*__f);


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]