diff options
author | buraq <buraq@epfl.ch> | 2004-03-12 20:08:38 +0000 |
---|---|---|
committer | buraq <buraq@epfl.ch> | 2004-03-12 20:08:38 +0000 |
commit | c1df3809c679310c5acd6316ef7092792ae1f31c (patch) | |
tree | d2aec241404a9f8013ae4d2c4e00108204d400d1 /sources | |
parent | 315baae74dfccd8fb895fc4aa51812aa76d00194 (diff) | |
download | scala-c1df3809c679310c5acd6316ef7092792ae1f31c.tar.gz scala-c1df3809c679310c5acd6316ef7092792ae1f31c.tar.bz2 scala-c1df3809c679310c5acd6316ef7092792ae1f31c.zip |
*** empty log message ***
Diffstat (limited to 'sources')
-rw-r--r-- | sources/scala/Iterable.scala | 15 | ||||
-rw-r--r-- | sources/scala/Iterator.scala | 15 | ||||
-rw-r--r-- | sources/scala/Seq.scala | 15 | ||||
-rw-r--r-- | sources/scala/tools/dtd2scala/template/ObjectTemplate.scala.xml | 75 | ||||
-rw-r--r-- | sources/scala/xml/nobinding/NoBindingFactoryAdapter.scala | 17 |
5 files changed, 88 insertions, 49 deletions
diff --git a/sources/scala/Iterable.scala b/sources/scala/Iterable.scala index 58949bae9f..88ac6d0035 100644 --- a/sources/scala/Iterable.scala +++ b/sources/scala/Iterable.scala @@ -90,4 +90,19 @@ trait Iterable[+A] { elements foreach { elem => res = elem :: res; } res.reverse } + + def similar(x:Any) = { + x match { + case that:Iterable[A] => + val ita = this.elements; + val itb = that.elements; + var res = true; + while( ita.hasNext && itb.hasNext && res ) { + res = ( ita.next == itb.next ); + }; + !ita.hasNext && !itb.hasNext && res + case _ => + false + } + } } diff --git a/sources/scala/Iterator.scala b/sources/scala/Iterator.scala index 40528e9d58..0345ebe4af 100644 --- a/sources/scala/Iterator.scala +++ b/sources/scala/Iterator.scala @@ -193,4 +193,19 @@ trait Iterator[+a] with Iterable[a] { ahead = new Partner; Pair(ahead, new Partner) } + + /** converts a prefix of this iterator to a sequence + */ + def toSeq( len:int ):Seq[a] = new Seq[a] { + def length: Int = len; + def elements = Iterator.this; + def apply( i:int ) = { + val it = Iterator.this; + var j = 0; while( i<i ) it.next; + it.next + } + } + + def toSeq:Seq[a] = toList; + } diff --git a/sources/scala/Seq.scala b/sources/scala/Seq.scala index f5e8d54577..e3534ce27e 100644 --- a/sources/scala/Seq.scala +++ b/sources/scala/Seq.scala @@ -17,7 +17,19 @@ package scala; * @author Matthias Zenger * @version 1.0, 16/07/2003 */ -trait Seq[+A] with PartialFunction[Int, A] with Iterable[A] { +trait Seq[+A] with PartialFunction[Int, A] with Iterable[A] with Similarity { + + /** Returns true if the elements in this sequence are equal + * to the elements in another sequence + */ + override def similar( x:Any ):boolean = { + x.match { + case that:Seq[A] => + ( that.length == this.length ) && super[Iterable].similar( that ) + case _ => + false + }; + } /** Returns the length of the sequence. * @@ -45,4 +57,5 @@ trait Seq[+A] with PartialFunction[Int, A] with Iterable[A] { } res + ")" } + } diff --git a/sources/scala/tools/dtd2scala/template/ObjectTemplate.scala.xml b/sources/scala/tools/dtd2scala/template/ObjectTemplate.scala.xml index 6caeebd54b..20e859edc5 100644 --- a/sources/scala/tools/dtd2scala/template/ObjectTemplate.scala.xml +++ b/sources/scala/tools/dtd2scala/template/ObjectTemplate.scala.xml @@ -23,58 +23,53 @@ object <string ref="objectName"/> { + import scala.collection.mutable.HashMap; + type childrenT = scala.Seq[scala.xml.Node]; type attribMap = scala.collection.immutable.Map[String,String]; - type attribMapT = scala.collection.mutable.HashMap[String,String]; + type attribMapT = HashMap[String,String]; type NodeConstructorType = (childrenT,attribMapT) => scala.xml.Node; <elementBinding> def constr_&ccElementName;( ch:childrenT, attrs:attribMapT ) = new &ccElementName;(ch:_*) { - val aMap = scala.collection.immutable.ListMap.Empty[String,String].incl( attrs.toList ); - override def attributes : attribMap = aMap; - <attributeBinding> - override val &cAttributeName; = attrs.get(&qAttributeName;); - </attributeBinding> - val attribHashCode: int = attrs.toList.hashCode() ; + private val hmap = attrs; + override def attribute = attrs.elements.toSeq( hmap.size ); + }; - case class &ccElementName;( ch:scala.xml.Node* ) extends scala.xml.AttributedNode { - def label = &qElementName;; - def children = ch; - - override def attributes : attribMap = { - var m = scala.collection.immutable.ListMap.Empty[String,String]; - <attributeBinding> - this.&cAttributeName; match { - case scala.Some( x ) => m.update( &qAttributeName;, x ); - case scala.None =>; - }; - </attributeBinding> - m - } + case class &ccElementName;( ch:scala.xml.Node* ) extends scala.xml.Node { + + def label = &qElementName;; + def child = ch; + private val hmap = new HashMap[String,String]; + override def attribute:Seq[Pair[String,String]] = Nil; <attributeBinding> - val &cAttributeName; : scala.Option[String] = scala.None; /* o'ridden at parse time */ + def &cAttributeName; : scala.Option[String] = hmap.get(&qAttributeName;); </attributeBinding> + val attribHashCode: int = hmap.toList.hashCode() ; - def % (attrs:scala.List[Pair[String,String]]) = new &ccElementName;( children:_* ) { - - private var lmap = &ccElementName;.this.attributes.incl( attrs ); - override def attributes : attribMap = lmap; - <attributeBinding> - override val &cAttributeName; = lmap.get(&qAttributeName;); - </attributeBinding> - } - - def % (attr:Pair[String,String]) = new &ccElementName;( children:_* ) { - - private var lmap = &ccElementName;.this.attributes.incl( attr ); - override def attributes : attribMap = lmap; - <attributeBinding> - override val &cAttributeName; = lmap.get(&qAttributeName;); - </attributeBinding> - } - + /** returns a new &ccElementName; with updated attributes */ + final def %(attrs: Seq[Pair[String, String]]) = { + val newmap = new HashMap[String,String](); + for( val p <- attribute.elements ) { newmap += p._1 -> p._2 }; + for( val p <- attrs ) { newmap += p._1 -> p._2 }; + new &ccElementName;( child:_* ) { + private val hmap = newmap; + override def attribute = newmap.elements.toSeq( newmap.size ); + } + } + + /** returns a new &ccElementName; with updated attribute */ + final def %(attr: Pair[String, String]) = { + val newmap = new HashMap[String,String](); + for( val p <- attribute.elements ) { newmap += p._1 -> p._2 }; + newmap += attr._1 -> attr._2; + new &ccElementName;( child:_* ) { + private val hmap = newmap; + override def attribute = newmap.elements.toSeq( newmap.size ); + } + } } </elementBinding> diff --git a/sources/scala/xml/nobinding/NoBindingFactoryAdapter.scala b/sources/scala/xml/nobinding/NoBindingFactoryAdapter.scala index 7f841043d8..d8649ab438 100644 --- a/sources/scala/xml/nobinding/NoBindingFactoryAdapter.scala +++ b/sources/scala/xml/nobinding/NoBindingFactoryAdapter.scala @@ -36,21 +36,22 @@ class NoBindingFactoryAdapter extends FactoryAdapter { val elHashCode = Utility.hashCode( label, attrs, children ) ; val attrList = attrs.toList; - cache.get( elHashCode ).match{ case Some(cachedElem) => - //System.err.println("[using cached elem +"+cachedElem.toXML+"!]"); - cachedElem + //System.err.println("[using cached elem +"+cachedElem.toXML+"!]"); //DEBUG + cachedElem case None => val el = if( children.isEmpty ) { new Elem( label ) { - override def attributes = ListMap.Empty[String,String].incl( attrList ); - override def hashCode() = Utility.hashCode( label, attrList.hashCode(), children ); + private val hmap = attrs; + override def attribute = attrs.elements.toSeq( attrs.size ); + override def hashCode() = elHashCode; }; } else { new Elem( label, children:_* ) { - override def attributes = ListMap.Empty[String,String].incl( attrList ); - override def hashCode() = Utility.hashCode( label, attrList.hashCode(), children ); - }; + private val hmap = attrs; + override def attribute = attrs.elements.toSeq( attrs.size ); + override def hashCode() = elHashCode; + }; } cache.update( elHashCode, el ); el |