@@ -13,6 +13,7 @@ use std::ops::{Deref, DerefMut};
13
13
// pub type DictContentType = HashMap<usize, Vec<(PyObjectRef, PyObjectRef)>>;
14
14
// pub type DictContentType = HashMap<String, PyObjectRef>;
15
15
pub type DictContentType = HashMap < String , ( PyObjectRef , PyObjectRef ) > ;
16
+ // pub type DictContentType = HashMap<String, Vec<(PyObjectRef, PyObjectRef)>>;
16
17
17
18
pub fn new ( dict_type : PyObjectRef ) -> PyObjectRef {
18
19
PyObject :: new (
@@ -56,20 +57,62 @@ pub fn set_item_in_content(
56
57
// XXX: Currently, we only support String keys, so we have to unwrap the
57
58
// PyObject (and ensure it is a String).
58
59
60
+ // TODO: invoke __hash__ function here!
59
61
let needle_str = objstr:: get_value ( needle) ;
60
62
elements. insert ( needle_str, ( needle. clone ( ) , value. clone ( ) ) ) ;
61
63
}
62
64
63
65
pub fn get_key_value_pairs ( dict : & PyObjectRef ) -> Vec < ( PyObjectRef , PyObjectRef ) > {
64
66
let dict_elements = get_elements ( dict) ;
67
+ get_key_value_pairs_from_content ( & dict_elements)
68
+ }
69
+
70
+ pub fn get_key_value_pairs_from_content (
71
+ dict_content : & DictContentType ,
72
+ ) -> Vec < ( PyObjectRef , PyObjectRef ) > {
65
73
let mut pairs: Vec < ( PyObjectRef , PyObjectRef ) > = Vec :: new ( ) ;
66
- for ( _str_key, pair) in dict_elements . iter ( ) {
74
+ for ( _str_key, pair) in dict_content . iter ( ) {
67
75
let ( key, obj) = pair;
68
76
pairs. push ( ( key. clone ( ) , obj. clone ( ) ) ) ;
69
77
}
70
78
pairs
71
79
}
72
80
81
+ pub fn get_item ( dict : & PyObjectRef , key : & PyObjectRef ) -> Option < PyObjectRef > {
82
+ let needle_str = objstr:: get_value ( key) ;
83
+ get_key_str ( dict, & needle_str)
84
+ }
85
+
86
+ // Special case for the case when requesting a str key from a dict:
87
+ pub fn get_key_str ( dict : & PyObjectRef , key : & str ) -> Option < PyObjectRef > {
88
+ let elements = get_elements ( dict) ;
89
+ content_get_key_str ( & elements, key)
90
+ }
91
+
92
+ /// Retrieve a key from dict contents:
93
+ pub fn content_get_key_str ( elements : & DictContentType , key : & str ) -> Option < PyObjectRef > {
94
+ // TODO: let hash: usize = key;
95
+ match elements. get ( key) {
96
+ Some ( v) => Some ( v. 1 . clone ( ) ) ,
97
+ None => None ,
98
+ }
99
+ }
100
+
101
+ pub fn contains_key_str ( dict : & PyObjectRef , key : & str ) -> bool {
102
+ let elements = get_elements ( dict) ;
103
+ content_contains_key_str ( & elements, key)
104
+ }
105
+
106
+ pub fn content_contains_key_str ( elements : & DictContentType , key : & str ) -> bool {
107
+ // TODO: let hash: usize = key;
108
+ match elements. get ( key) {
109
+ Some ( _) => true ,
110
+ None => false ,
111
+ }
112
+ }
113
+
114
+ // Python dict methods:
115
+
73
116
fn dict_new ( _vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
74
117
Ok ( new ( args. args [ 0 ] . clone ( ) ) )
75
118
}
0 commit comments