001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU Lesser General Public License (LGPL): http://www.gnu.org/licenses/
004    
005    /*
006     * BaseEdge.java
007     *
008     * Created on November 13, 2004, 8:21 PM
009     */
010    
011    package graphlab.library;
012    
013    
014    /**
015     * The base class for all edges. By default each vertex has two integer
016     * properties, color and weight.
017     *
018     * @author Omid Aladini
019     */
020    
021    public class BaseEdge<VertexType extends BaseVertex>
022            implements Comparable<BaseEdge<VertexType>> {
023    
024    
025        protected BaseEdgeProperties prop;
026    
027        public final VertexType source;
028        public final VertexType target;
029    
030        /**
031         * Number of times edge iteration is called. This will be set as a temporary flag
032         * in order to reduce running time of edge iteration back to O(n^2).
033         * Don't touch this please, if you like your iterations work well.
034         */
035        int edgeIterationIndex = 0;
036    
037        public BaseEdge(VertexType source, VertexType target) {
038            this.source = source;
039            this.target = target;
040            this.prop = new BaseEdgeProperties(0, 0, false);
041        }
042    
043        public BaseEdge(VertexType source, VertexType target, BaseEdgeProperties prop) {
044            this.source = source;
045            this.target = target;
046            this.prop = prop;
047        }
048    
049    
050        public BaseEdge getCopy(VertexType v1, VertexType v2) {
051            return new BaseEdge(v1, v2, prop);
052        }
053    
054        /**
055         * Returns the color of the edge.
056         *
057         * @return The color associated with the edge.
058         */
059        public int getColor() {
060            return getProp().color;
061        }
062    
063        /**
064         * Sets the color of the edge.
065         *
066         * @param color Sets col as the color of the edge.
067         */
068        public void setColor(int color) {
069            getProp().color = color;
070        }
071    
072        /**
073         * Returns the weight of the edge.
074         *
075         * @return The weight associated with the edge.
076         */
077        public int getWeight() {
078            return getProp().weight;
079        }
080    
081        /**
082         * Sets the weight of the edge.
083         *
084         * @param weight Sets w as the color of the edge.
085         */
086        public void setWeight(int weight) {
087            getProp().weight = weight;
088        }
089    
090        /**
091         * Returns the mark of the edge.
092         *
093         * @return The mark associated with the edge.
094         */
095        public boolean getMark() {
096            return getProp().mark;
097        }
098    
099        /**
100         * Sets the mark of the edge.
101         *
102         * @param m Sets m as the mark of the edge.
103         */
104        public void setMark(boolean m) {
105            getProp().mark = m;
106        }
107    
108        /**
109         * Sets properties object for this edge; Overwrites the existing.
110         *
111         * @param prop The property object to set.
112         */
113        public void setProp(BaseEdgeProperties prop) {
114            this.prop = prop;
115        }
116    
117        /**
118         * Returns property object for this edge.
119         *
120         * @return Returns property object for this edge.
121         */
122        public BaseEdgeProperties getProp() {
123            return prop;
124        }
125    
126        @Override
127        public String toString() {
128            return "Edge:" + source + "->" + target;
129        }
130    
131        /**
132         * Compares two edges according to their wrights.
133         *
134         * @param o Edge to compare.
135         * @return 0 if two objects are equal, -1 if this object is less than and
136         *         1 if this object is greater than the supplied object.
137         */
138        public int compareTo(BaseEdge<VertexType> o) {
139            if (o.prop.weight == prop.weight)
140                return 0;
141    
142            if (o.prop.weight > prop.weight)
143                return -1;
144    
145            return 1;
146        }
147    }