~kamstrup/dee/sorted

« back to all changes in this revision

Viewing changes to src/dee-model.c

  • Committer: Mikkel Kamstrup Erlandsen
  • Date: 2012-01-09 09:39:24 UTC
  • Revision ID: mikkel.kamstrup@gmail.com-20120109093924-p97kducelxu2ntn9
Add methods dee_model_find_sorted() and dee_model_insert_sorted() to the DeeModel interface; with default (linear) implementations in DeeSerializableModel.

Show diffs side-by-side

added added

removed removed

Lines of Context:
885
885
}
886
886
 
887
887
/**
 
888
 * dee_model_insert_row_sorted:
 
889
 * @self: The model to do a sorted insert on
 
890
 * @row_members: (array zero-terminated=1): An array of
 
891
 *       #GVariants with type signature matching those of the
 
892
 *       column schemas of @self. If any of the variants have floating
 
893
 *       references they will be consumed.
 
894
 * @cmp_func: (scope call): Callback used for comparison or rows
 
895
 * @user_data: (closure): Arbitrary pointer passed to @cmp_func during search
 
896
 *
 
897
 * Inserts a row in @self according to the sorting specified by @cmp_func.
 
898
 * If you use this method for insertion you should not use other methods as this
 
899
 * method assumes the model to be already sorted by @cmp_func.
 
900
 *
 
901
 * Returns: (transfer none) (type Dee.ModelIter): A #DeeModelIter pointing to the new row
 
902
 */
 
903
DeeModelIter*
 
904
dee_model_insert_row_sorted (DeeModel           *self,
 
905
                             GVariant          **row_members,
 
906
                             DeeCompareRowFunc   cmp_func,
 
907
                             gpointer            user_data)
 
908
{
 
909
  DeeModelIface *iface;
 
910
 
 
911
  g_return_val_if_fail (DEE_IS_MODEL (self), NULL);
 
912
 
 
913
  CHECK_SCHEMA (self, return NULL);
 
914
 
 
915
  iface = DEE_MODEL_GET_IFACE (self);
 
916
 
 
917
  return (* iface->insert_row_sorted) (self, row_members, cmp_func, user_data);
 
918
}
 
919
 
 
920
/**
 
921
 * dee_model_insert_sorted:
 
922
 * @self: The model to do a sorted insert on
 
923
 * @cmp_func: (scope call): Callback used for comparison or rows
 
924
 * @user_data: (closure): Arbitrary pointer passed to @cmp_func during search
 
925
 * @VarArgs: Specifies the row to insert. A collection of #GVariant<!-- -->s
 
926
 *           matching the number of columns @self
 
927
 *
 
928
 * Convenience function for calling dee_model_insert_row_sorted().
 
929
 * Inserts a row in @self according to the sorting specified by @cmp_func.
 
930
 * If you use this method for insertion you should not use other methods as this
 
931
 * method assumes the model to be already sorted by @cmp_func.
 
932
 *
 
933
 * Returns: (transfer none) (type Dee.ModelIter): A #DeeModelIter pointing to the new row
 
934
 */
 
935
DeeModelIter*
 
936
dee_model_insert_sorted (DeeModel           *self,
 
937
                         DeeCompareRowFunc   cmp_func,
 
938
                         gpointer            user_data,
 
939
                         ...)
 
940
{
 
941
  DeeModelIface  *iface;
 
942
  DeeModelIter   *iter;
 
943
  GVariant      **row_members;
 
944
  gsize           slice_size;
 
945
  va_list         args;
 
946
 
 
947
  g_return_val_if_fail (DEE_IS_MODEL (self), NULL);
 
948
 
 
949
  CHECK_SCHEMA (self, return NULL);
 
950
 
 
951
  iface = DEE_MODEL_GET_IFACE (self);
 
952
 
 
953
  /* We use the slice allocator for this and not just g_new() since we
 
954
   * can expect a lot of allocations of the same size */
 
955
  slice_size = dee_model_get_n_columns (self) * sizeof(gpointer);
 
956
  row_members = g_slice_alloc (slice_size);
 
957
  va_start (args, user_data);
 
958
  dee_model_build_row_valist (self, row_members, &args);
 
959
  va_end (args);
 
960
 
 
961
  iter = (* iface->insert_row_sorted) (self, row_members, cmp_func, user_data);
 
962
  g_slice_free1 (slice_size, row_members);
 
963
  return iter;
 
964
}
 
965
 
 
966
/**
 
967
 * dee_model_find_row_sorted:
 
968
 * @self: The model to do a sorted insert on
 
969
 * @row_spec: (array zero-terminated=1): An array of
 
970
 *       #GVariants with type signature matching those of the
 
971
 *       column schemas of @self. No references will be taken on the variants.
 
972
 * @cmp_func: (scope call): Callback used for comparison or rows
 
973
 * @user_data: (closure): Arbitrary pointer passed to @cmp_func during search
 
974
 * @out_was_found: (out): A place to store a boolean value that will be set when
 
975
 *                 this method returns. If %TRUE then an exact match was found.
 
976
 *                 If %FALSE then the returned iter points to a row just after
 
977
 *                 where @row_spec would have been inserted.
 
978
 *                 Pass %NULL to ignore.
 
979
 *
 
980
 * Finds a row in @self according to the sorting specified by @cmp_func.
 
981
 * This method will assume that @self is already sorted by @cmp_func.
 
982
 *
 
983
 * If you use this method for searching you should only use
 
984
 * dee_model_insert_row_sorted() to insert rows in the model.
 
985
 *
 
986
 * Returns: (transfer none) (type Dee.ModelIter): If @out_was_found is set to
 
987
 *           %TRUE then a #DeeModelIter pointing to the first matching row.
 
988
 *           If it is %FALSE then the iter pointing to the row just after where
 
989
 *           @row_spec_would have been inserted.
 
990
 */
 
991
DeeModelIter*
 
992
dee_model_find_row_sorted (DeeModel           *self,
 
993
                           GVariant          **row_spec,
 
994
                           DeeCompareRowFunc   cmp_func,
 
995
                           gpointer            user_data,
 
996
                           gboolean           *out_was_found)
 
997
{
 
998
  DeeModelIface *iface;
 
999
 
 
1000
  g_return_val_if_fail (DEE_IS_MODEL (self), NULL);
 
1001
 
 
1002
  CHECK_SCHEMA (self, return NULL);
 
1003
 
 
1004
  iface = DEE_MODEL_GET_IFACE (self);
 
1005
 
 
1006
  return (* iface->find_row_sorted) (self, row_spec, cmp_func,
 
1007
                                     user_data, out_was_found);
 
1008
}
 
1009
 
 
1010
DeeModelIter*
 
1011
dee_model_find_sorted (DeeModel           *self,
 
1012
                       DeeCompareRowFunc   cmp_func,
 
1013
                       gpointer            user_data,
 
1014
                       gboolean           *out_was_found,
 
1015
                       ...)
 
1016
{
 
1017
  DeeModelIface  *iface;
 
1018
  DeeModelIter   *iter;
 
1019
  GVariant      **row_members;
 
1020
  gsize           slice_size;
 
1021
  va_list         args;
 
1022
 
 
1023
  g_return_val_if_fail (DEE_IS_MODEL (self), NULL);
 
1024
 
 
1025
  CHECK_SCHEMA (self, return NULL);
 
1026
 
 
1027
  iface = DEE_MODEL_GET_IFACE (self);
 
1028
 
 
1029
  /* We use the slice allocator for this and not just g_new() since we
 
1030
   * can expect a lot of allocations of the same size */
 
1031
  slice_size = dee_model_get_n_columns (self) * sizeof(gpointer);
 
1032
  row_members = g_slice_alloc (slice_size);
 
1033
  va_start (args, out_was_found);
 
1034
  dee_model_build_row_valist (self, row_members, &args);
 
1035
  va_end (args);
 
1036
 
 
1037
  iter = (* iface->find_row_sorted) (self, row_members, cmp_func,
 
1038
                                     user_data, out_was_found);
 
1039
  g_slice_free1 (slice_size, row_members);
 
1040
  return iter;
 
1041
}
 
1042
 
 
1043
/**
888
1044
 * dee_model_remove:
889
1045
 * @self: a #DeeModel
890
1046
 * @iter: a #DeeModelIter pointing to the row to remove