Check-in [c84097a743]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Implement HashMap.put().
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: c84097a743f5877d928b45f0ee23e8c76c5474ad
User & Date: stephanie.gawroriski 2019-07-15 11:14:34
Context
2019-07-15
11:31
Add static and runtime constant pools to the class. check-in: 84d7bf5ed1 user: stephanie.gawroriski tags: trunk
11:14
Implement HashMap.put(). check-in: c84097a743 user: stephanie.gawroriski tags: trunk
11:03
Implement MiniClassAccessor.baseInstanceSize(). check-in: fda1b54bff user: stephanie.gawroriski tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to runt/klib/supervisor/java/util/HashMap.java.

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
..
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
..
87
88
89
90
91
92
93

94
95
96
97










































98
99
100
101
102
103
104
		// Find existing bucket
		__Bucket__ bucket = this._buckets[hash & _BUCKET_MASK];
		if (bucket == null)
			return false;
		
		// Go through items
		for (__Item__ i : bucket._items)
			if (i._hash == hash && HashMap.__equals(__k, i._key))
				return true;
		
		// Not found
		return false;
	}
	
	/**
................................................................................
		// Find existing bucket
		__Bucket__ bucket = this._buckets[hash & _BUCKET_MASK];
		if (bucket == null)
			return null;
		
		// Go through items
		for (__Item__ i : bucket._items)
			if (i._hash == hash && HashMap.__equals(__k, i._key))
				return (V)i._value;
		
		// Not found
		return null;
	}
	
	/**
................................................................................
	 * Puts an item into the map.
	 *
	 * @param __k The key.
	 * @param __v The value.
	 * @return The old item, if any.
	 * @since 2019/06/24
	 */

	public V put(K __k, V __v)
	{
		Assembly.breakpoint();
		throw new todo.TODO();










































	}
	
	/**
	 * Returns if two objects are equal to each other.
	 *
	 * @param __a The first object.
	 * @param __b The second object.







|







 







|







 







>


<
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
..
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
..
87
88
89
90
91
92
93
94
95
96

97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
		// Find existing bucket
		__Bucket__ bucket = this._buckets[hash & _BUCKET_MASK];
		if (bucket == null)
			return false;
		
		// Go through items
		for (__Item__ i : bucket._items)
			if (i != null && i._hash == hash && HashMap.__equals(__k, i._key))
				return true;
		
		// Not found
		return false;
	}
	
	/**
................................................................................
		// Find existing bucket
		__Bucket__ bucket = this._buckets[hash & _BUCKET_MASK];
		if (bucket == null)
			return null;
		
		// Go through items
		for (__Item__ i : bucket._items)
			if (i != null && i._hash == hash && HashMap.__equals(__k, i._key))
				return (V)i._value;
		
		// Not found
		return null;
	}
	
	/**
................................................................................
	 * Puts an item into the map.
	 *
	 * @param __k The key.
	 * @param __v The value.
	 * @return The old item, if any.
	 * @since 2019/06/24
	 */
	@SuppressWarnings({"unchecked"})
	public V put(K __k, V __v)
	{

		// Calculate hash code
		int hash = __k.hashCode(),
			bkid = hash & _BUCKET_MASK;
		
		// Find existing bucket, create if missing
		__Bucket__ bucket = this._buckets[bkid];
		if (bucket == null)
			this._buckets[bkid] = (bucket = new __Bucket__());
		
		// Check if it already exists in the map
		__Item__[] items = bucket._items;
		int n = items.length,
			empty = -1;
		for (int i = 0; i < n; i++)
		{
			__Item__ it = items[i];
			
			// Is empty slot, use it
			if (it == null)
				empty = i;
			
			// Matching key, replace
			else if (it._hash == hash && HashMap.__equals(__k, it._key))
			{
				V rv = (V)it._value;
				it._value = __v;
				return rv;
			}
		}
		
		// Add new item at the end
		__Item__[] newitems = new __Item__[n + 1];
		for (int i = 0; i < n; i++)
			newitems[i] = items[i];
		__Item__ it = new __Item__(hash, __k);
		newitems[n] = it;
		it._value = __v;
		
		// Store new items
		bucket._items = newitems;
		
		// There was no previous old value
		return null;
	}
	
	/**
	 * Returns if two objects are equal to each other.
	 *
	 * @param __a The first object.
	 * @param __b The second object.