SF.net SVN: jython:[7240] trunk/jython/src/org/python/core/PyTuple.java

[email protected] Mon, 14 Mar 2011 23:09:29 +0000
Newsgroups gmane.comp.lang.jython.cvs
Message-ID <[email protected]>
Revision: 7240
          http://jython.svn.sourceforge.net/jython/?rev=7240&view=rev
Author:   pjenvey
Date:     2011-03-14 23:09:29 +0000 (Mon, 14 Mar 2011)

Log Message:
-----------
add tuple.index

Modified Paths:
--------------
    trunk/jython/src/org/python/core/PyTuple.java

Modified: trunk/jython/src/org/python/core/PyTuple.java
===================================================================
--- trunk/jython/src/org/python/core/PyTuple.java	2011-03-14 22:47:30 UTC (rev 7239)
+++ trunk/jython/src/org/python/core/PyTuple.java	2011-03-14 23:09:29 UTC (rev 7240)
@@ -471,21 +471,51 @@
         }
     }
 
-    public int count(PyObject obj) {
-        return tuple_count(obj);
+    public int count(PyObject value) {
+        return tuple_count(value);
     }
 
     @ExposedMethod(doc = BuiltinDocs.tuple_count_doc)
-    final int tuple_count(PyObject obj) {
+    final int tuple_count(PyObject value) {
         int count = 0;
         for (PyObject item : array) {
-            if (item.equals(obj)) {
+            if (item.equals(value)) {
                 count++;
             }
         }
         return count;
     }
 
+    public int index(PyObject value) {
+        return index(value, 0);
+    }
+
+    public int index(PyObject value, int start) {
+        return index(value, start, size());
+    }
+
+    public int index(PyObject value, int start, int stop) {
+        return tuple_index(value, start, stop);
+    }
+
+    @ExposedMethod(defaults = {"null", "null"}, doc = BuiltinDocs.tuple_index_doc)
+    final int tuple_index(PyObject value, PyObject start, PyObject stop) {
+        int startInt = start == null ? 0 : PySlice.calculateSliceIndex(start);
+        int stopInt = stop == null ? size() : PySlice.calculateSliceIndex(stop);
+        return tuple_index(value, startInt, stopInt);
+    }
+
+    final int tuple_index(PyObject value, int start, int stop) {
+        int validStart = boundToSequence(start);
+        int validStop = boundToSequence(stop);
+        for (int i = validStart; i < validStop; i++) {
+            if (array[i].equals(value)) {
+                return i;
+            }
+        }
+        throw Py.ValueError("tuple.index(x): x not in list");
+    }
+
     @Override
     public boolean equals(Object other) {
         if (this == other) {


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.

------------------------------------------------------------------------------
Colocation vs. Managed Hosting
A question and answer guide to determining the best fit
for your organization - today and in the future.
http://p.sf.net/sfu/internap-sfd2d